Using the CloudBees CD REST API via python

Article ID:360033188811
2 minute readKnowledge base

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'):

// NOTE: "myHeaders" will be used in below examples of requests. 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)

Ignoring SSL certificate

To ignore SSL certificates, the 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:///rest/v1.0/?

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

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