clams.restify package

Module contents

class clams.restify.ClamsHTTPApi(cla_instance: ClamsApp)[source]

Bases: Resource

ClamsHTTPApi provides mapping from HTTP verbs to Python API defined in ClamsApp.

Constructor takes an instance of ClamsApp.

get() Response[source]

Maps HTTP GET verb to appmetadata().

Returns:

Returns app metadata in a HTTP response.

static json_to_response(json_str: str, status=200) Response[source]

Helper method to convert JSON output from a ClamsApp to a HTTP response.

Parameters:
  • json_str – a serialized JSON .

  • status – a numerical HTTP code to respond.

Returns:

A HTTP response ready to send.

methods: t.ClassVar[t.Collection[str] | None] = {'GET', 'POST', 'PUT'}[source]

The methods this view is registered for. Uses the same default (["GET", "HEAD", "OPTIONS"]) as route and add_url_rule by default.

post() Response[source]

Maps HTTP POST verb to annotate(). Note that for now HTTP PUT verbs is also mapped to annotate().

Returns:

Returns MMIF output from a ClamsApp in a HTTP response.

put() Response[source]

Maps HTTP POST verb to annotate(). Note that for now HTTP PUT verbs is also mapped to annotate().

Returns:

Returns MMIF output from a ClamsApp in a HTTP response.

class clams.restify.ParameterCaster(param_spec: Dict[str, Tuple[int | float | bool | str, bool]])[source]

Bases: object

A helper class to convert parameters passed by HTTP query strings to proper python data types.

Parameters:

param_spec – A specification of a data types of parameters

static bool_param(value)[source]

Helper function to convert string values to bool type.

cast(args: Dict[str, List[str]]) Dict[str, int | float | bool | str | List[int | float | bool | str]][source]

Given parameter specification, tries to cast values of args to specified Python data types. Note that this caster deals with query strings, thus all keys and values in the input args are plain strings. Also note that the caster does not handle “unexpected” parameters came as an input. Handling (raising an exception or issuing a warning upon receiving) an unexpected runtime parameter must be done within the app itself. Thus, when a key is not found in the parameter specifications, it should just pass it as a vanilla string.

Parameters:

args – k-v pairs

Returns:

A new dictionary of type-casted args

static float_param(value)[source]

Helper function to convert string values to float type.

static int_param(value)[source]

Helper function to convert string values to int type.

static str_param(value)[source]

Helper function to convert string values to string type.

class clams.restify.Restifier(app_instance: ClamsApp, loopback: bool = False, port: int = 5000, debug: bool = True)[source]

Bases: object

Resitifier is a simple wrapper that takes a ClamsApp object and turn it into a flaks-based HTTP server. For mapping between Python API and HTTP API, it relies on ClamsHTTPApi class.

Constructor takes a ClamsApp instance and some options for flask configuration.

Parameters:
  • app_instance – A ClamsApp to wrap.

  • loopback – when True, the flask wrapper only listens to requests from localhost (used for debugging).

  • port – Port number for the flask app to listen (used for debugging).

  • debug – When True, the flask wrapper will run in debug mode.

run(**options)[source]

Starts a development server. See serve_development().

Parameters:

options – any additional options to pass to the web server.

serve_development(**options)[source]

Runs the CLAMS app as a flask webapp, using flask built-in development server (https://werkzeug.palletsprojects.com/en/2.0.x/).

Parameters:

options – any additional options to pass to the web server.

serve_production(**options)[source]

Runs the CLAMS app as a flask webapp, using a production-ready web server (gunicorn, https://docs.gunicorn.org/en/stable/#).

Parameters:

options – any additional options to pass to the web server.

test_client()[source]

Returns flask test client.