Vertec Python module “vtcweb”

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 http://<host>/api/webservice/<webservicename>/<method-name>/additional/path/parameters for example:

self.path_parameters=("additional", "path", "parameters")
headers

A dictionary containing the headers of the WebRequest in the form "Header-Name": "Header-Value". The header Authorization is not returned. For duplicate headers, only the first value is returned.

query

A dictionary containing the query parameters of the WebRequest in the form "Query-Name": "Query-Value". The apiToken parameter is not returned. For duplicate query parameters, only the first value is returned.

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 application/json or x-www-form-urlencoded), this method can deserialize the request body and write the values as attributes to a helper object where they can be retrieved directly.

For JSON, the type of the values is the same as the type. For HTML, all values are returned as a string.

{
  "abbreviation": "AD",  
  "level": 5270,    
}

result = self.parse_request_body()
result.abbreviation = "AD" # string
result.level = 5270 # int

Keys that are not valid Python identifiers are ignored and a corresponding message is logged.

expected_attributes

The optional expected_attributes parameter can be used to validate and process the request body. This only works with JSON bodies.

As expected_attributes, a dictionary is passed whose keys are the expected attributes in the body and whose values are their type.

 expected_attributes = {
    "abbreviation": "str",
    "level": "int"
    }

Possible types are:

  • str
  • int
  • float
  • bool
  • datetime
  • array(typ): If a list is expected, array can specify one of the other types as the object type, for example array(int) for a list of integers.

When calling parse_request_body(expected_attributes) it is ensured that all specified attributes are present and can be converted to the specified type. Otherwise the WebRequest is answered with Statuscode 400 Bad Request and a corresponding message.

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 "5270" could be returned as “level” because it can be converted to an integer. Date values must be returned according to Iso standard 8601 in order to be identified and converted.

The returned object then contains all attributes converted to the expected type.

Attributes not included in expected_attributes are processed as described above.

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:

  • status_code: status code (integer) of the web response, e.g. 200 for OK.
  • body: The body of the web response, <empty> by default.
  • headers: A dictionary of response headers.
as_json(content): response

This method serializes the passed content as a JSON string and returns a Response object with the following parameters:

  • status_code = 200
  • body = Content as JSON string. If the content passed is already a string, it is assumed that it is a valid JSON string, which is written directly to the body.
  • headers[“Content-Type”] = "application/json"
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 Webservices and the log level Debug / 10 (see also Debug Categories). To see these entries in the log file, you need to include parameter DebugCategories=Vertec.Webservices under [Log] in the Vertec.ini configuration file.

In addition, the object is also written to the output with print. You will see this output in the Web service entry in the Access log field if the Track requests option is activated.

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:

  • @get(“<alias>”)
  • @post(“<alias>”)
  • @put(“<alias>”)
  • @delete(“<alias>”)
  • @http(“<alias>”)

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.

Class WebException

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.

Netherlands

United Kingdom