Skip to content
This repository was archived by the owner on May 3, 2021. It is now read-only.

HelperObjectsDocs

Simon Olofsson edited this page Apr 9, 2020 · 2 revisions

Helper objects documentation

This file contains documentation for classes that are objects intended to make development simpler. They are not intended for inheritance but to use as a part of your toolbox when creating Features with CommandIntegrator.

logger

The @logger decorator above your methods allows simple, pragmatic and unified logging across your tech stack. It is a closure, also referred to as a wrapper method which will automatically log the arguments, keyword arguments and return of your method, including logging errors as ERROR log level if errors are encountered during the method execution.

Usage Example
from CommandIntegrator.logger import logger

@logger
def add_ints(a: int, b: int) -> int:
    return a + b

s = add_ints(a = 4, b = 4)
Result in runtime.log file located in execution directory

2020-04-02 09:41:32,185:DEBUG:CI Logger: Ran method "add" in __main__ with ARGS: () & KWARGS: {a: 4, b: 4} & RETURN: 8

RestApiHandle

class RestApiHandle(uri: str, standby_hours = 2)

This class is designed to make it as easy as possible to interact with REST API's through POST and GET. Simple ways to add headers to requests and automatic caching of results to act as a cusion between caller and the API, with a customizable lifespan for the cache makes it safe for paid API's where you want to limit traffic but still need to respond to callers.

Parameters

  • uri (Mandatory[str]) - the URI to the REST api
  • standby_hours (Optional[int]) - lifespan in hours for the cache inside the instance

(Property) - last_api_call

​ DateTime string, representing the last time the API was queried.

Type: str

(Property) - uri

​ the URI to the REST api

Type: str

(Property) - last_api_call

​ DateTime string, representing the last time the API was queried.

Type: str

(Method) - get()

Call the api and mutate the instance variable _cached_response
at the same time, if either none prior were made or the time 
expired and it needs to be refreshed. 

:returns:
	dict

(Method) - post(headers: dict)

Send a POST request to the API uri configured.
since the headers are ambigous for this class
they have to be specified by the caller. Only
dictionaries are accepted for this parameter.

:param headers:
	dictionary with POST request headers for
	the call you wish to make to the API.
	Example: {'language': 'english'}
:returns: 
	dict, response from the API response.