Default Objector

class moco_wrapper.util.objector.DefaultObjector

This is the default class for handling the modification ob response objects that the requestor classes generated and were pushed to the objector.

Successful responses will have their data converted into actual python objects, while error responses will be converted into exceptions and raised at a later stage.

Note

If you do not want exceptions to be raised see moco_wrapper.util.objector.NoErrorObjector

class_map = None

Dictionary used to find the appropriate classes from url-part-path created in get_class_name_from_request_url()

For example the path project=>tasks means ProjectTask is the responsible class. The dictionary contains the following:

"projects": {
    "base" => "Project",
    "tasks" => "ProjectTask"
}
convert(requestor_response)

Converts the data of a response object (for example json) into a python object.

Parameters:requestor_response – response object (see Responses)
Returns:modified response object

Note

The data of an error response response (moco_wrapper.util.response.ErrorResponse) will be converted into an actual exception that later can be raised

Note

if the method get_class_name_from_request_url() that is used to find the right class for conversion, returns None, no conversion of objects will take place

error_class_map = None

Dictionary used to convert http status codes into the appropriate exceptions

self.error_class_map = {
    404: "NotFoundException",
    ..
}
get_class_name_from_request_url(url) → str

Finds the class name by analysing the request url.

Parameters:url (str) – url to analyse

This function works as follows:

The url will look something like this https://test.mocoapp.com/api/v1/projects/1234/tasks?page=1. We split the url on /api/v1/.

[https://test.mocoapp.com", "projects/1234/tasks?page=1"]

After that we throw away the first part and split the second part on the slash character:

["projects", 1234, "tasks?page=1"]

Then we remove all query string parameters:

["projects", 1234, "tasks"]

Then we remove all parts that are ids(digits):

["projects", "tasks"]

Now that we have our path projects=>tasks, we use the class_map to find the right classname.

The map is a dictionary that looks something like this:

class_map = {
    "activities" => {
        "base" => "Activity"
        "disregard" => None
    },
    "projects": {
        "base" => "Project",
        "tasks" => "ProjectTask"
    },
    "users" => { .. },
    "companies" => { .. }
}

We use the path we generated and walk our class_map until we get the entry at the end of the path. In our case that would be ProjectTask. As this value is a string that is our final classname.

Note

if the final value is a dictionary, the base case will be returned. For example if path was projects, the value at the end of our path is a dictionary. If that is the case the base key will be used.

get_error_class_name_from_response_status_code(status_code) → str

Get the class name of the exception class based on the given http status code

Parameters:status_code (int) – Http status code of the response
Returns:class name of the exception

Warning

The status_code parameter must be a key in error_class_map