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.
Successfull 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
meansProjectTask
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
only
moco_wrapper.util.response.JsonResponse
andmoco_wrapper.util.response.ListingResponse
are object to this conversion.Note
Error responses will be converted into actual exceptions.
Note
if the method
get_class_name_from_request_url()
that is used to find the right class for conversion, returnsNone
, no conversion of objects will take place
-
get_class_name_from_request_url
(url)¶ finds the class name by analysing the request url
Parameters: url – 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 theclass_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.
-