KBEC-00383 - Using the Rest API through Python

Description:

The easiest way to start using Rest API through Python is to use a "Requests" module.

import requests

The requests module provides all required methods for Rest API calls. Official documentation for requests can be found here.

Before sending any request authentication and SSL certificate ignoring are required:

Authentication and login

Missing authentication will cause error in response - '{"error":{"where":"","message":"No credentials/session found in this request","details":"","code":"NoCredentials"}}'

There are at least two ways to authenticate:

1. Provide password and login information in each request

Firstly auth module from requests should be imported - from requests.auth import HTTPBasicAuth

So request will look like this:

requests.put('https://EFlowServer/rest/v1.0/properties/PythonProperty?projectName=Default&value=newvalue', auth=HTTPBasicAuth('user', 'secretpasswd'))

 

2. Login and save session id in headers

Logging in can be done by sending the next request:

requests.post('https://EFlowServer/rest/v1.0/sessions?password=changeme&userName=admin', verify=False)

A server will send a response something like this:

{
"sessionId": "4VOZBL8M9X5BJYI9",
"userName": "user"
}

"sessionId" should be stored in some property for use afterward in headers. Also, the content type can be set in headers(it should be set to 'application/json'):

myHeaders = {'Accept': 'application/json', "sessionId": "4VOZBL8M9X5BJYI9"}

So a request with added session details in the headers should look like this -

requests.get('https://EFlowServer/rest/v1.0/properties/PythonProperty?projectName=Default', verify=False, headers=myHeaders)

note: "myHeaders" will be used in below exaples of requests.

Ignoring SSL certificate 

To ignore SSL certificate option "verify" in the request should be set to "False".

verify=False

This will avoid the error of "SSL: CERTIFICATE_VERIFY_FAILED". 

 

Sending requests

After preparation was done all types of requests(GET, POST, PUT, DELETE) can be sent:

Get property

request:

requests.get('https://EFlowServer/rest/v1.0/properties/PythonProperty?projectName=Default', verify=False, headers=myHeaders)
 

response: 

{ "property": { "propertyId": "d527401a-d390-11e7-832b-005056330fc3", "propertyName": "PythonProperty", "counter": "0", "createTime": "2017-11-27T16:34:26.271Z", "description": "", "expandable": "1", "lastModifiedBy": "user", "modifyTime": "2017-11-27T16:34:26.271Z", "owner": "user", "tracked": "1", "value": "PythonRestApi" } }

Set property

request:

requests.put('https://EFlowServer/rest/v1.0/properties/PythonProperty?projectName=Default&value=UpdatedPython', auth=HTTPBasicAuth('user', 'superpasswd'))

response:

{ "property": { "propertyId": "d527401a-d390-11e7-832b-005056330fc3", "propertyName": "PythonProperty", "counter": "0", "createTime": "2017-11-27T16:34:26.271Z", "description": "", "expandable": "1", "lastModifiedBy": "user", "modifyTime": "2017-11-27T16:34:26.271Z", "owner": "user", "tracked": "1", "value": "PythonUpdate" } } 

Publish artifact version 

request:

requests.post('https://EFlowServer/rest/v1.0/artifacts?artifactKey=2&groupId=2', verify=False, auth=HTTPBasicAuth('admin', 'rootpasswd'))

response:

{"artifact":{"artifactId":"f21665c8-d530-11e7-9c3c-005056330fc3","artifactName":"2:2","artifactKey":"2","createTime":"2017-11-29T18:13:05.476Z","groupId":"2","lastModifiedBy":"admin","modifyTime":"2017-11-29T18:13:05.476Z","owner":"admin","propertySheetId":"f21665ca-d530-11e7-9c3c-005056330fc3"}}'  

 

All requests are using the same pattern of URI:

https://<EFlowServer>/rest/v1.0/<path>?<query>

For more information please see Rest API details available on your installed system:

https://EFlowServer/rest/doc/v1.0/

 

 

 

 

 

 

 

 

 

Have more questions? Submit a request

Comments

Powered by Zendesk