The Vertec Python module “vtcweb” for working with web services
From version 6.8.0.17 there is the Python module vtcweb with the base class vtcweb.WebService. All web service classes must be derived from this class. On the base class, the service object self has the following methods and attributes:
| Methods / Attributes | description |
|---|---|
self.http_method |
Contains the HTTP method used to call the web service. Possible values are GET, POST, PUT & DELETE. |
self.path_parameters |
A tuple containing the path parameters of the URL after the method name: self.path_parameters=(“additional,” “path,” “parameters”) |
self.headers |
A dictionary that contains the headers of the WebRequest in the form of |
self.query |
A dictionary that contains the query parameters of the WebRequest in the form of |
self.querystring |
The original query string used to call the web service, without the leading question mark. |
self.body |
The original request body as a string used to call the web service. |
self.response |
A response object that is automatically used to respond to the WebRequest if the method itself does not have a return value. The following values can be set: self.response.status_code (The status code of the web response is 200 OK) self.response.body (The body of the web response and by default as <empty>) self.response.headers[“...”] (A dictionary of Response Headers) |
self.as_json(<some_object>) |
This method serializes the passed object as a JSON string, so it sets the Body to self.response.body. It also sets self.response.statuscode = 200 and self.response.headers[“Content-Type”] = application/json. |
self.log(log_object) |
Writes the passed object to the log. Can handle strings, but also other objects.
|
| Methods / Attributes | description | Example |
|---|---|---|
self.parse_request_body(expected_attributes=None) |
The method simplifies the work with request bodies. The request must use the content type |
For JSON bodies, the body is deserialized. The values are written as attributes to a helper object and are thus directly retrievable. The type of the values corresponds to the type in JSON.
Body: result = self.parse_request_body() Keys that are not valid Python identifiers are ignored and a corresponding message is logged.
result = self.parse_request_body() $release_date and 1line are not valid identifiers in Python and are therefore ignored. For result = self.parse_request_body() |
expected_attributes |
This attribute validates and converts the request body of the method above. This only works with JSON bodies. A dictionary is expected whose keys are the attributes in the body and the values of the type. Further attributes that are not contained in |
Possible types are:
The type Body: expected_attributes = result = self.parse_request_body() Note: |
In the methods called via WebRequests, web responses can be created as follows:
| Replies | description |
|---|---|
| return <Integer> |
An integer is interpreted as a status code. The response contains no other values. |
| return “string” | The returned string is interpreted as a response body with the Statuscode 200 OK. In addition, the header Content-Type is set to text/plain . |
| return self.response | The response object (see above) is returned and interpreted, even if the method ends without a return value. |
| return self.as_json(response_object) | See above. |
| Class | description |
|---|---|
| raise WebException(<statusCode>,<body>=None,<contentType>=None) |
Throwing a WebException can interrupt the execution of the current call at any point. The WebException is converted into a web response according to the values passed. A status code must be passed, the body and content type are optional. If a body is passed but no content type, the fallback |