pyjen.utils.jenkins_api module

Base class for all objects that interact with the Jenkins REST API

class pyjen.utils.jenkins_api.JenkinsAPI(url, session)[source]

Bases: object

Abstraction around the raw Jenkins REST API

Parameters
  • url (str) – URL of the Jenkins API endpoint to manage

  • session (requests.Session) – HTTP session to use for interacting with the Jenkins REST API

clone(api_url)[source]

Creates a copy of this instance, for a new endpoint URL

Parameters

api_url (str) – URL for the new REST API endpoint to be managed

Returns

reference to the newly created API interface

Return type

JenkinsAPI

property crumb

unique “crumb” identifier required by all POST operations

Introduced in Jenkins v2

Output from this helper can be used directly in post operations as an HTTP header, something like this:

requests.post(... headers=self.crumb)

reference: https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API (see CSRF protection section)

Type

dict

get_api_data(target_url=None, query_params=None)[source]

retrieves the Jenkins API specific data from the specified URL

Parameters
  • target_url (str) – Full URL to the REST API endpoint to be queried. If not provided, data will be loaded from the default ‘url’ for this object

  • query_params (str) – optional set of query parameters to customize the returned data

Returns

The set of Jenkins attributes, converted to Python objects, associated with the given URL.

Return type

dict

get_api_xml(path=None, params=None)[source]

Gets api XML data from a given REST API endpoint

Parameters
  • path (str) – optional extension path to append to the root URL managed by this object when performing the get operation

  • params (dict) – optional query parameters to be passed to the request

Returns

parsed XML data

Return type

xml.etree.ElementTree.Element

get_text(path=None, params=None)[source]

gets the raw text data from a Jenkins URL

Parameters
  • path (str) – optional extension path to append to the root URL managed by this object when performing the get operation

  • params (dict) – optional query parameters to be passed to the request

Returns

the text loaded from this objects’ URL

Return type

str

property jenkins_headers

HTTP headers from the main Jenkins dashboard using the REST API

The dashboard headers contain metadata describing the Jenkins instance hosting the REST API, including details such as version number, current UI theme, and others.

Type

dict

property jenkins_version

version number of the Jenkins server hosting this REST API, parsed into a tuple of integers

Typically returns a 3 tuple with the major, minor and update digits of the version number

Type

tuple

post(target_url, args=None)[source]

sends data to or triggers an operation via a Jenkins URL

Parameters
  • target_url (str) – Full URL to sent post request to

  • args (dict) – optional set of data arguments to be sent with the post operation.

Returns

reference to the response data returned by the post request

Return type

requests.Response

property root_url

URL of the main Jenkins dashboard associated with the current object

NOTE: The URL returned by this property is guaranteed to end with a trailing slash character

Type

str

property url

the URL for the REST API endpoint managed by this object

NOTE: The URL returned by this property is guaranteed to end with a trailing slash character

Type

str