Vertec Python module “vtcweb”

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:

URL: http://<host>/api/webservice/<webservicename>/<method-name>/additional/path/parameters

self.path_parameters=(“additional,” “path,” “parameters”)

self.headers

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

self.query

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

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.

If some_object is already a string, it is assumed to be a valid JSON string. It is written directly to self.response.body.

self.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. The Category Webservices and the logo level Debug / 10 used. To see these entries in the log file, you must set the value Vertec.Webservices into the DebugCategories in the Vertec.ini configuration file.


In addition, the object is also written in the output using print. You will see this output in the web service entry in the field Anfrageprotokoll, if the check mark is set to Track requests.

 

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 application/json or x-www-form-urlencoded.

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.


Example:

Body:

{

 

result = self.parse_request_body()
result.title = “The Gunslinger” # string
result.release_date = “1982-07-10” # string
result.amazon_rating = 4.1 # float
result.pages = 224 # int
       

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


Example:

Body:

{
 

result = self.parse_request_body()
result.title = “The Gunslinger” # string

$release_date and 1line are not valid identifiers in Python and are therefore ignored.
       

For x-www-form-urlencoded Bodies always return the values as a string.

Example:

Body: title=The Gunslinger&release_date=1982-07-10&amazon_rating=4.1&pages=224

result = self.parse_request_body()
result.title = “The Gunslinger” # string
result.release_date = “1982-07-10” # string
result.amazon_rating = “4.1” # string
result.pages = “224” # string

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.

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 will be responded with Statuscode 400 Bad Request and a corresponding message. The returned object then contains all attributes converted to the expected type.

Further attributes that are not contained in expected_attributes are processed as described above.

Possible types are:

  • str
  • int
  • float
  • bool
  • datetime
  • array

The type array can optionally define one of the other types as object type, e.g. array(int) for arrays of integers.

Body:


{


 

expected_attributes =



{

 

result = self.parse_request_body()
result.title = “The Gunslinger” # string
result.release_date = datetime(1982, 7, 10) # datetime
result.amazon_rating = 4.1 # float
result.pages = 224 # int
result.buyable = True # bool
result.editions = [1982, 2003] # list with int values
       

Note:

Note that the values do not necessarily have to be of the type specified. They only have to be convertable to this type. For example, the string “224” could also be delivered as pages, since this can be converted to an integer. However, the strings «200+» or «200-250» would result in an error as they are not valid integers

Date values must be provided in accordance with ISO standard 8601 in order to be identified and converted.

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 WebException

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 text/plain is used.

Netherlands

United Kingdom