Skip to content

The "List devices" example generated from codegen is broken for python. #4

@drypatrick

Description

@drypatrick

Running the following code generated in the README of the sdk, result in an error.

Code:

from __future__ import print_statement
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# Configure API key authorization: fiware_token
swagger_client.configuration.api_key['X-Auth-Token'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# swagger_client.configuration.api_key_prefix['X-Auth-Token'] = 'Bearer'

# create an instance of the API class
api_instance = swagger_client.EntitiesApi()
id = 'id_example' # str | A comma-separated list of elements. Retrieve entities whose ID matches one of the elements in the list. Incompatible with idPattern. (optional)
type = 'type_example' # str | comma-separated list of elements. Retrieve entities whose type matches one of the elements in the list. Incompatible with `typePattern`. (optional)
id_pattern = 'id_pattern_example' # str | A correctly formated regular expression. Retrieve entities whose ID matches the regular expression. Incompatible with id. (optional)
type_pattern = 'type_pattern_example' # str | A correctly formated regular expression. Retrieve entities whose type matches the regular expression. Incompatible with `type`. (optional)
q = 'q_example' # str | A query expression, composed of a list of statements separated by `;`, i.e., q=statement;statements;statement. See [Simple Query Language specification](#simple_query_language). (optional)
mq = 'mq_example' # str | A query expression for attribute metadata, composed of a list of statements separated by `;`, i.e., mq=statement1;statement2;statement3. See [Simple Query Language specification](#simple_query_language). (optional)
georel = 'georel_example' # str | Spatial relationship between matching entities and a reference shape. See [Geographical Queries](#geographical_queries). (optional)
geometry = 'geometry_example' # str | Geografical area to which the query is restricted. See [Geographical Queries](#geographical_queries). (optional)
coords = 'coords_example' # str | List of latitude-longitude pairs of coordinates separated by ';'. See [Geographical Queries](#geographical_queries). (optional)
limit = 1.2 # float | Limits the number of entities to be retrieved (optional)
offset = 1.2 # float | Establishes the offset from where entities are retrieved (optional)
attrs = 'attrs_example' # str | Comma-separated list of attribute names whose data are to be included in the response. The attributes are retrieved in the order specified by this parameter. If this parameter is not included, the attributes are retrieved in arbitrary order. (optional)
order_by = 'order_by_example' # str | Criteria for ordering results. See \"Ordering Results\" section for details. (optional)
options = 'options_example' # str | Options dictionary (optional)

try: 
    api_response = api_instance.list_entities(id=id, type=type, id_pattern=id_pattern, type_pattern=type_pattern, q=q, mq=mq, georel=georel, geometry=geometry, coords=coords, limit=limit, offset=offset, attrs=attrs, order_by=order_by, options=options)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling EntitiesApi->list_entities: %s\n" % e)

Error:

Exception when calling EntitiesApi->list_entities: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Server': 'nginx/1.19.3', 'Date': 'Sat, 21 May 2022 06:32:11 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '25', 'Connection': 'keep-alive', 'X-Powered-By': 'Express', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'HEAD, 
POST, PUT, GET, OPTIONS, DELETE', 'Access-Control-Allow-Headers': 'origin, content-type, X-Auth-Token, Tenant-ID, Authorization', 'ETag': 'W/"19-Ou7FRKJwk6C28Bij3l3WOA"'})
HTTP response body: User token not authorized

I'm struggling to understand:

  • How to generate a valid X-Auth-Token
  • Why if I can send some valid REST API call with curl without an auth-token, I can't do the same with the SDK

Curl call
curl -v http://localhost:4001/iot/devices -H "fiware-service: opcua_car" -H "fiware-servicepath: /demo"

Sending Headers where X-Auth-Token is not defined:

 *   Trying 127.0.0.1:4001...
 * Connected to localhost (127.0.0.1) port 4001 (#0)      
 > GET /iot/devices HTTP/1.1
 > Host: localhost:4001
 > User-Agent: curl/7.79.1
 > Accept: */*
 > fiware-service: opcua_car
 > fiware-servicepath: /demo

Valid answer:

< HTTP/1.1 200 OK
< X-Powered-By: Express
< Fiware-Correlator: 06418f55-1b52-45de-abaa-9af8d0e56b58
< Content-Type: application/json; charset=utf-8
< Content-Length: 24
< ETag: W/"18-90KiBjq8YRQpT/NsVf7vo89XXWw"
< Date: Sat, 21 May 2022 06:22:17 GMT
< Connection: keep-alive

{"count":0,"devices":[]}* Connection #0 to host localhost left intact

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions