api.resources package

Submodules

api.resources.base module

api.resources.featurizer module

api.resources.security module

class api.resources.security.BaseUserResource[source]

Bases: flask_restful.Resource

Base class for the user authentication and authorization resources

model

alias of api.authentication.database.models.User

password_validator = <Regexp(regex=re.compile('^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*).{7,50}$'), error='Password must meet: a) contain at least 1 letter and at least 1 number, b) between 6-50 characters')>
sign_in_form_validation = {'password': <fields.String(dump_default=<marshmallow.missing>, attribute=None, validate=None, required=True, load_only=False, dump_only=False, load_default=<marshmallow.missing>, allow_none=False, error_messages={'required': 'Missing data for required field.', 'null': 'Field may not be null.', 'validator_failed': 'Invalid value.', 'invalid': 'Not a valid string.', 'invalid_utf8': 'Not a valid utf-8 string.'})>, 'username': <fields.String(dump_default=<marshmallow.missing>, attribute=None, validate=None, required=True, load_only=False, dump_only=False, load_default=<marshmallow.missing>, allow_none=False, error_messages={'required': 'Missing data for required field.', 'null': 'Field may not be null.', 'validator_failed': 'Invalid value.', 'invalid': 'Not a valid string.', 'invalid_utf8': 'Not a valid utf-8 string.'})>}
sign_up_form_validation = {'password': <fields.String(dump_default=<marshmallow.missing>, attribute=None, validate=<Regexp(regex=re.compile('^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*).{7,50}$'), error='Password must meet: a) contain at least 1 letter and at least 1 number, b) between 6-50 characters')>, required=True, load_only=False, dump_only=False, load_default=<marshmallow.missing>, allow_none=False, error_messages={'required': 'Missing data for required field.', 'null': 'Field may not be null.', 'validator_failed': 'Invalid value.', 'invalid': 'Not a valid string.', 'invalid_utf8': 'Not a valid utf-8 string.'})>, 'username': <fields.String(dump_default=<marshmallow.missing>, attribute=None, validate=None, required=True, load_only=False, dump_only=False, load_default=<marshmallow.missing>, allow_none=False, error_messages={'required': 'Missing data for required field.', 'null': 'Field may not be null.', 'validator_failed': 'Invalid value.', 'invalid': 'Not a valid string.', 'invalid_utf8': 'Not a valid utf-8 string.'})>}
class api.resources.security.LoginResource[source]

Bases: api.resources.security.BaseUserResource

Class implementing log-in API resource

methods = {'POST'}

A list of methods this view can handle.

post(username, password)[source]

Logs-in an existing user.

Parameters
  • username (str) – name of the existing user

  • password (str) – password of the user

Returns

log-in response data (username, access token, refresh token)

Return type

dict

Example

import requests

# Prepare the log-in data (example: existing user)
body = {
    "username": "user123",
    "password": "pAsSw0rd987!"
}

# Call the log-in endpoint (example: locally deployed API)
response = requests.post(
    "http://localhost:5000/login",
    json=body)

# Get the access and refresh tokens from the response
if response.ok:
    access_token = response.json().get("access_token")
    refresh_token = response.json().get("refresh_token")
class api.resources.security.RefreshAccessTokenResource[source]

Bases: flask_restful.Resource

Class implementing refreshing of the access token API resource

methods = {'POST'}

A list of methods this view can handle.

post()[source]

Refreshes the access token.

Returns

refresh response data (refreshed access token)

Return type

dict

Example

import requests

# Prepare the refresh headers
headers = {
    "Authorization": f"Bearer <refresh_token>"
}

# Call the refresh endpoint (example: locally deployed API)
response = requests.post(
    "http://localhost:5000/refresh",
    headers=headers)

# Get the refreshed access token
if response.ok:
    access_token = response.json().get("access_token")
class api.resources.security.SignupResource[source]

Bases: api.resources.security.BaseUserResource

Class implementing sign-up API resource

methods = {'POST'}

A list of methods this view can handle.

post(username, password)[source]

Signs-up a new user.

Parameters
  • username (str) – name of the new user

  • password (str) – password of the user (see: validation requirements)

Returns

sign-up response data (username)

Return type

dict

Example

import requests

# Prepare the sign-up data (example: new user)
body = {
    "username": "user123",
    "password": "pAsSw0rd987!"
}

# Call the sign-up endpoint (example: locally deployed API)
response = requests.post(
    "http://localhost:5000/signup",
    json=body)

# Check if the user was created
if response.ok:
    print(f"User {response.json().get('username')} created")

Module contents