Skip to content

python REST API notes

craigmcchesney edited this page Dec 26, 2019 · 1 revision

Here is an example URL for invoking the API to retrieve item with id 1. When the web service is running, you can enter this in the browser address bar since login is not required:

https://127.0.0.1:10232/cdb/items/1

For APIs where login is required, you need to use a program like curl. In this example, you first issue the login command with username/password:

curl -vk -d "" -u cdb:password -X POST https://127.0.0.1:10232/cdb/login

Make note of the session_id in the cookie. To call an api that requires authentication using cookie must DOUBLE base64 encode the new status value, e.g., used https://emn178.github.io/online-tools/base64_encode.html to translate “Ordered” to “VDNKa1pYSmxaQT09”, otherwise get “invalid padding” error with no details.

To update status (where VDNKa1pYSmxaQT09 is double base64 encoded Ordered”):

curl -v -k -d "" -X POST -H "Content-Type: application/json" --cookie "session_id=7fe6560459db095b0c261014c280c3aa962b04e6" https://127.0.0.1:10232/cdb/items/domain/inventory/status/4/VDNKa1pYSmxaQT09

To change location (where VEc5allYUnBiMjQ9 is base64 double encoded “Location”:

curl -v -k -d "" -X POST -H "Content-Type: application/json" --cookie "session_id=891cdf5326d7a626f43ae8d8c1531cc815ff9e58" https://127.0.0.1:10232/cdb/items/4/1/addItemElementRelationship/VEc5allYUnBiMjQ9

To add a new API, follow the pattern in this example:

  • Added “getNames” to itemRouteDescriptor.py with path “%s/items/domain/name”. Also added APIs to get names by type e.g., getNamesSystem, getNamesSubsystem, getNamesDevice, getNamesSignal with paths like “%s/items/domain/name/type/system”.
  • Added itemController.py::getNames(), getNamesSystem(), getNamesSubsystem(), getNamesDevice(), getNamesSignal(). Added namesListToJson() to format the query result as JSON for return to the client.
  • Added itemControllerImpl.py::getNames() and getNamesByType(). The latter is used to fulfill each of the itemController methods where the type is passed as a parameter.
  • added itemDbApi::getNamesByType() which is used to fulfill all the queries with optional type parameter.
  • added itemHandler::getNamesByType() which has the sqlalchemy code to support all the queries with optional type parameter.