quizmous_api package

Submodules

quizmous_api.common module

quizmous_api.common.extract_jwt(token: Union[str, bytes], serial: Union[str, bytes]) → Tuple[bool, dict]

Extracts jwt tokens

Parameters
  • token (Union[str, bytes]) – token from request

  • serial (Union[str, bytes]) – serial used to hash the token

Returns

Data saved in token

Return type

Tuple[bool, dict]

quizmous_api.common.extract_token(request: sanic.request.Request) → Union[bytes, str]

Finds token in request based on it’s type

Parameters

request (Request) – request from user

Returns

token

Return type

Union[bytes, str]

quizmous_api.common.parse_jwt(f)

Decorator that parses the token and returns it to decorated function

quizmous_api.db module

class quizmous_api.db.DB

Bases: object

DB - PostgreSQL database wrapper

static get_pool() → asyncpg.pool.Pool

Returns the database connection pool

Returns

database connection pool

Return type

asyncpg.pool.Pool

async static init(dsn, *args, **kwargs)

Initializes the database connection pool

Parameters

dsn (str) – dsn connection string

exception quizmous_api.db.UniqueModelConstraint(msg: str)

Bases: Exception

Exception that gets raised when model that is inserted and should be unique to the DB is already there

quizmous_api.db.get_data_for_query(model_cls)

Returns data needed to build db query

Parameters

model_cls (model class literal) – Model used in query

Returns

data needed to build db query

Return type

dict

async quizmous_api.db.insert_answer_to_db(answer, question_id: int)

Inserts answer to db

Parameters

question_id (int) – id of parent question model

async quizmous_api.db.insert_model_to_db(model, id: int = None)

Inserts model to database

Parameters
  • model – Model object to insert

  • id (int) – optional id of inserted model, defaults to None

Raises

ValueError – Gets raised when model is not supported by that function

Returns

id of inserted model

Return type

int

async quizmous_api.db.insert_question_to_db(question, quiz_id: int)

Inserts question to db

Parameters

quiz_id (int) – id of parent quiz model

async quizmous_api.db.insert_quiz_to_db(quiz)

Inserts quiz to db

async quizmous_api.db.insert_user_answers_to_db(answers, quiz_id)

Inserts user answers to db

Parameters
  • answers (List[Answer]) – List of answers from user

  • quiz_id (int) – Id of quiz

async quizmous_api.db.insert_user_quiz_taken(user, id)

Sets the flag that user has taken the quiz

Parameters
  • user (User model) – which user taken the quiz

  • id (int) – Id of user

async quizmous_api.db.insert_user_to_db(user)

Inserts user to db

Parameters

user (User model) – user to insert

Raises

UniqueModelConstraint – Gets raised when user already exists

Returns

id of user

Return type

int

async quizmous_api.db.perform_select_sql(model_cls, id: int = None)

Performs select query

Parameters
  • model_cls (model class literal) – model to select

  • id (int, optional) – id of selected model, defaults to None

Returns

rows of data

Return type

Row

quizmous_api.db.prepare_insert_sql(model)

Returns insert sql query for specific model

Parameters

model – Model used in query

Returns

insert sql query

Return type

str

quizmous_api.db.prepare_select_sql(model_cls, id: int = None)

Returns select sql query for specific model

Parameters
  • model_cls (model class literal) – Model used in query

  • id (int) – optional id of inserted model, defaults to None

Returns

select sql query

Return type

str

async quizmous_api.db.select_answer_from_db(question_id: int = None)

Performs select answer from db

Parameters

id (int, optional) – id of selected model, defaults to None

Returns

rows of data

Return type

Row

async quizmous_api.db.select_model_from_db(model_cls, id: int = None)

Selects model from the database

Parameters
  • model_cls (class literal) – class literal of model

  • id (int) – optional id of selected model, defaults to None

Raises

ValueError – Gets raised when model is not supported by that function

Returns

model object created using model_cls class literal

Return type

Model

async quizmous_api.db.select_question_from_db(quiz_id: int = None)

Performs select question from db

Parameters

id (int, optional) – id of selected model, defaults to None

Returns

rows of data

Return type

Row

async quizmous_api.db.select_quiz_from_db(id: int = None)

Performs select quiz from db

Parameters

id (int, optional) – id of selected model, defaults to None

Returns

rows of data

Return type

Row

async quizmous_api.db.select_user_from_db(id: int)

Selects user from db

Parameters

id (int) – Id of user

Returns

User model

Return type

User object

async quizmous_api.db.select_user_quiz_taken(user_nick, id)

Selects data is user has taken a quiz

Parameters
  • user_nick (str) – Nick of user

  • id (int) – Id of quiz

Returns

None if quiz has not been taken

Return type

None or Row

quizmous_api.response module

class quizmous_api.response.Responses

Bases: enum.Enum

An enumeration.

BAD_REQUEST = {'message': 'bad request', 'status': 400}
CREATED = {'message': 'created', 'status': 201}
INTERNAL = {'message': 'internal server error', 'status': 500}
OK = {'message': 'success', 'status': 200}
UNAUTHORIZED = {'message': 'user not authorized', 'status': 401}
UNIQUE_VIOLATED = {'message': 'the data provided to endpoint already exists in the system', 'status': 409}
quizmous_api.response.create_response(resp: quizmous_api.response.Responses, add: dict = {}) → sanic.response.HTTPResponse

Generates response

Parameters
  • resp (Responses) – Response enum

  • add (dict, optional) – additional response data, defaults to {}

Returns

Sanic HTTPResponse

Return type

response.HTTPResponse

quizmous_api.server module

async quizmous_api.server.main()

Main program

quizmous_api.server.run()

Runs main using asyncio

quizmous_api.version module

quizmous_api.version.get_api_version()

Returns api version

Returns

Api version and build number

Return type

json

Module contents