RestOC.Services module

Services Module

Tools to create and communicate with Micro Services

RestOC.Services.Effect

alias of RestOC.Services.Response

RestOC.Services.EffectException

alias of RestOC.Services.ResponseException

class RestOC.Services.Error(code, msg=None)

Bases: RestOC.Services.Response

Shorthand form of Response(error=)

__init__(code, msg=None)

Constructor

Initialises a new Response instance

Parameters
  • code (uint) – The error code

  • msg (mixed) – Optional message for more info on the error

Returns

Error

class RestOC.Services.Response(data=None, error=None, warning=None)

Bases: object

Represents a standard result from any/all requests

__init__(data=None, error=None, warning=None)

Constructor

Initialises a new Response instance

Parameters
  • data (mixed) – If a request returns data this should be set

  • error (mixed) – If a request has an error, this can be filled with a code and message string

  • warning (mixed) – If a request returns a warning this should be set

Raises

ValueError

Returns

Response

__str__()

str

Python magic method to return a string from the instance

Returns

str

dataExists()

Data Exists

Returns True if there is data in the Response

Returns

bool

errorExists()

Error Exists

Returns True if there is an error in the Response

Returns

bool

classmethod fromDict(val)

From Dict

Converts a dict back into an Response

Parameters

val (dict) – A valid dict

Returns

Response

classmethod fromJSON(val)

From JSON

Tries to convert a string made from str() back into an Response

Parameters

val (str) – A valid JSON string

Returns

Response

warningExists()

Warning Exists

Returns True if there is a warning in the Response

Returns

bool

exception RestOC.Services.ResponseException(data=None, error=None, warning=None)

Bases: Exception

Response Exception

Stupid python won’t let you raise anything that doesn’t extend BaseException

__init__(data=None, error=None, warning=None)

Constructor

Dumb dumb python

Parameters
  • data (mixed) – If a request returns data this should be set

  • error (mixed) – If a request has an error, this can be filled with a code and message string

  • warning (mixed) – If a request returns a warning this should be set

Returns

ResponseException

class RestOC.Services.Service

Bases: object

The object to build all Services from

create(path, data, sesh=None, environ=None)

Create

Create a new object

Parameters
  • path (str) – The path passed to the request

  • data (mixed) – The data sent with the request

  • sesh (Sesh._Session) – The session passed to the request

  • environ (dict) – Info related to the request

Returns

Response

delete(path, data, sesh=None, environ=None)

Delete

Delete an existing object

Parameters
  • path (str) – The path passed to the request

  • data (mixed) – The data sent with the request

  • sesh (Sesh._Session) – The session passed to the request

  • environ (dict) – Info related to the request

Returns

Response

initialise()

Initialise

Initialises the instance and returns itself for chaining

Returns

Service

classmethod install()

Install

Installs any necessary DBs, configs, etc, needed by the Service when it is first installed

Raises

NotImplementedError

Returns

bool

static pathToMethod(path, append='')

Path to Method

Takes a path and converts it to the standard naming for Service methods

Parameters
  • path (str) – The path to parse

  • append (str) – If set, appended to method name

Returns

str

read(path, data={}, sesh=None, environ=None)

Read

Read an existing object

Parameters
  • path (str) – The path passed to the request

  • data (mixed) – The data sent with the request

  • sesh (Sesh._Session) – The session passed to the request

  • environ (dict) – Info related to the request

Returns

Response

update(path, data, sesh=None, environ=None)

Update

Update an existing object

Parameters
  • path (str) – The path passed to the request

  • data (mixed) – The data sent with the request

  • sesh (Sesh._Session) – The session passed to the request

  • environ (dict) – Info related to the request

Returns

Response

RestOC.Services.create(service, path, data, sesh=None, environ=None)

Create

Make a POST request

Parameters
  • service (str) – The service to call

  • path (str) – The path on the service

  • data (mixed) – The data to pass to the request

  • {Sesh._Session} (sesh) – The optional session to send with the request

  • environ (dict) – Info related to the request

Returns

Response

RestOC.Services.delete(service, path, data, sesh=None, environ=None)

Delete

Make a DELETE request

Parameters
  • service (str) – The service to call

  • path (str) – The path on the service

  • data (mixed) – The data to pass to the request

  • {Sesh._Session} (sesh) – The optional session to send with the request

  • environ (dict) – Info related to the request

Returns

Response

RestOC.Services.internalKey(key=None)

Internal Key

Generates or validates an internal key so services can communicate with each other

Parameters

key (str) – Passed to validate a key

Returns

bool

RestOC.Services.read(service, path, data, sesh=None, environ=None)

Read

Make a GET request

Parameters
  • service (str) – The service to call

  • path (str) – The path on the service

  • data (mixed) – The data to pass to the request

  • {Sesh._Session} (sesh) – The optional session to send with the request

  • environ (dict) – Info related to the request

Returns

Response

RestOC.Services.register(services, restconf, salt)

Register

Takes a dictionary of services to their instances, or None for remote services which will be found via the config

Parameters
  • services (dict) – Services being registered

  • restconf (dict) – Configuration variables for remote services

  • salt (str) – The salt used for internal key generation

Raises

ValueError

Returns

None

RestOC.Services.update(service, path, data, sesh=None, environ=None)

Update

Make a PUT request

Parameters
  • service (str) – The service to call

  • path (str) – The path on the service

  • data (mixed) – The data to pass to the request

  • {Sesh._Session} (sesh) – The optional session to send with the request

  • environ (dict) – Info related to the request

Returns

Response

RestOC.Services.verbose(flag=True)

Verbose

Puts Services in verbose mode for easy tracking of requests

Parameters

flag (bool) – defaults to True

Returns

None