Connection

The mooshak2 connection defines how you connect to a mooshak2 instance. The easiest way of getting a client instance is to call mooshak2api.login with your instance details.

mooshak2api.login(endpoint, username, password, contest=None, admin=False)[source]

Creates an authenticated client to interact with the Mooshak 2 API

Parameters:
  • endpoint – The API Endpoint, ending in a slash. E.g, https://mooshak2.dcc.fc.up.pt/mooshak-test/api/
  • username – A username for the connection.rst, E.g. admin
  • password – A password for the connection.rst, E.g. admin
  • contest – The contest for the (non admin) user to interact with
  • admin – If this is an admin account, and you want to administrate
Returns:

A Client Object

This client should then be passed to any calls you make to other functions.

It should be noted that by default, a mooshak2 session only lasts for 1 hour. As a result, you should check yourself if the session has expired, and if so, login again.

class mooshak2api.client.Client(endpoint)[source]

A bundling of a mooshak2 endpoint, and a User. In most cases you should use mooshak2api.login rather than initiating this class yourself.

You should add a User to this Object manually, via using self.user. Included are default headers that should be used for most JSON based messages.

If you want to test that the endpoint is accessible, then you can use test

mooshak2api.client.Client.test(self)

Tests the connection.rst to the server

Returns:returns True if a connection.rst could be made

If you want to make a connection, you can use headers_with_auth

mooshak2api.client.Client.headers_with_auth(self) → dict

Returns common headers with the JWT Header included. Ensure that self.user is a User Object, and that the user has been logged in with the .login() method.

Returns:returns a dict containing headers

It’s important that you periodically call refresh. This ensures that your connection to Mooshak stays active. This could be done via a scheduler, or via integrating it into your applications event loop

mooshak2api.client.Client.refresh(self)

Refreshes this users token. Should be called every x seconds, in order to ensure that the user stays logged in