syncano.models.custom_response¶
- class CustomResponseHandler[source]¶
Bases: object
A helper class which allows to define and maintain custom response handlers.
Consider an example: Script code:
set_response(HttpResponse(status_code=200, content='{"one": 1}', content_type='application/json'))
When suitable ScriptTrace is used:
trace = ScriptTrace.please.get(id=<code_box_trace_id>, script=<script_id>)
- Then trace object will have a content attribute, which will be a dict created from json (simple: json.loads under
- the hood);
So this is possible:
trace.content['one']
And the trace.content is equal to:
{'one': 1}
The handler can be easily overwrite:
def custom_handler(response): return json.loads(response['response']['content'])['one'] trace.response_handler.overwrite_handler('application/json', custom_handler)
or globally:
ScriptTrace.response_handler.overwrite_handler('application/json', custom_handler)
- Then trace.content is equal to::
- 1
- Currently supported content_types (but any handler can be defined):
- application/json
- text/plain
- class CustomResponseMixin[source]¶
Bases: object
- A mixin which extends the Script and ScriptEndpoint traces (and any other Model - if used) with following fields:
- content - This is the response data if set_response is used in Script code, otherwise it is the ‘stdout’ field;
- content_type - The content_type specified by the user in Script code;
- status_code - The status_code specified by the user in Script code;
- error - An error which can occur when code is executed: the stderr response field;
To process the content based on content_type this Mixin uses the CustomResponseHandler - see the docs there.
- response_handler = <syncano.models.custom_response.CustomResponseHandler object at 0x1d37a90>¶