The Vertec Python module "vtcweb" for working with web services
Starting with 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:
from vtcweb import WebService class myService(WebService): ...
About the base class, the service object self has the following methods and attributes:
| Method / Attribute | description |
|---|---|
http_method |
Contains the HTTP method used to call the Web service. Possible values are GET, POST, PUT, and DELETE. |
path_parameters |
Contains the list of path parameters of the URL after the method name. Calling
self.path_parameters=("additional", "path", "parameters")
|
headers |
A dictionary containing the headers of the WebRequest in the form |
query |
A dictionary containing the query parameters of the WebRequest in the form |
querystring |
The query string used to invoke the Web service (without the leading question mark). |
body |
The original request body as a string used to call the web service. |
parse_request_body ([expected_attributes=None]) |
If the request is JSON or HTML (content-type For JSON, the type of the values is the same as the type. For HTML, all values are returned as a string.
Keys that are not valid Python identifiers are ignored and a corresponding message is logged. expected_attributesThe optional As
expected_attributes = {
"abbreviation": "str",
"level": "int"
}
Possible types are:
When calling The values do not necessarily have to be of the specified type, they just have to be convertable to that type. For example, the string The returned object then contains all attributes converted to the expected type. Attributes not included in |
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 to it:
|
as_json(content): response |
This method serializes the passed content as a JSON string and returns a Response object with the following parameters:
|
log(log_object) |
Writes the passed object to the log. Can handle strings, but also other objects. The object is written to the Vertec session log using the category In addition, the object is also written to the output with |
Within the webservice class, normal Vertec Python code can be written.
Methods that should be called externally must be provided with one of the five Python Decorators imported by the module:
from vtcweb import get, put, post, delete, http
They are declared as follows:
and correspond to the HTTP method with which they can be called. If a method has the @http decorator, it is available with all HTTP methods.
An alias (string) must always be specified through which the method can be called, e.g.
@get("objects") def objects_get(self): ...
This allows the method objects_get to be called via http://<host>/api/webservice/<webservicename>/objects.
The alias can also be a route/path:
@get("project/search") def search_project(self): ...
This responds to the project/search route and then calls the search_project method. The path is still provided in self.path_parameters as usual.
As return values of the methods thus called, 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 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(content) |
See above. |
Throwing a WebException can interrupt the execution of the current call at any point. The WebException is converted to a web response according to the values passed.
raise WebException(<statuscode>,<body>=None,<contenttype>=None)
A status code (integer) must be passed, body (string) and content type (string) are optional. If a body is passed but no content type, text/plain is used as fallback.