pysnow¶
Python library for the ServiceNow REST API
pysnow.Client
— Creates a new client¶
-
class
pysnow.
Client
(instance=None, host=None, user=None, password=None, raise_on_empty=True, request_params=None, use_ssl=True, session=None)[source]¶ Creates a client ready to handle requests
Parameters: - instance – instance name, used to construct host
- host – host can be passed as an alternative to instance
- user – username
- password – password
- raise_on_empty – whether or not to raise an exception on 404 (no matching records)
- request_params – request params to send with requests
- use_ssl – Enable or disable SSL
- session – a requests session object
pysnow.OAuthClient
— Creates a new client with OAuth extras¶
-
class
pysnow.
OAuthClient
(client_id=None, client_secret=None, token_updater=None, *args, **kwargs)[source]¶ Pysnow
Client
with extras for oauth session and token handling.This API exposes two extra public methods:
- generate_token(user, pass)
- This method takes user and password credentials to generate a new OAuth token that can be stored outside the context of pysnow, e.g. in a session or database.
- set_token(token)
- Takes an OAuth token (dict) and internally creates a new pysnow-compatible session, enabling pysnow.OAuthClient to create requests.
Parameters: - client_id – client_id from ServiceNow
- client_secret – client_secret from ServiceNow
- token_updater – callback function called when a token has been refreshed
- instance – instance name, used to construct host
- host – host can be passed as an alternative to instance
- raise_on_empty – whether or not to raise an exception on 404 (no matching records)
- request_params – request params to send with requests
- use_ssl – Enable or disable SSL
pysnow.QueryBuilder
— Creates a new QueryBuilder object¶
-
class
pysnow.
QueryBuilder
[source]¶ Query builder - used for building complex queries
-
field
(field)[source]¶ Sets the field to operate on
Parameters: field – field (str) to operate on Returns: self
-
equals
(data)[source]¶ Query records with the given field equalling either: - the value passed (str) - any of the values passed (list)
-
pysnow.Request
— Creates a new request object¶
-
class
pysnow.
Request
(method, table, **kwargs)[source]¶ Takes arguments used to perform a HTTP request
Parameters: - method – HTTP request method
- table – table to operate on
-
last_response
¶ Return _last_response after making sure an inner
requests.request
has been performedRaise: NoRequestExecuted: If no request has been executed Returns: last response
-
count
¶ Returns the number of records the query would yield
-
status_code
¶ Return last_response.status_code after making sure an inner
requests.request
has been performedReturns: status_code of last_response
-
get_multiple
(fields=[], limit=None, order_by=[], offset=None)[source]¶ Wrapper method that takes whatever was returned by the _all_inner() generators and chains it in one result
The response can be sorted by passing a list of fields to order_by.
Example: get_multiple(order_by=[‘category’, ‘-created_on’]) would sort the category field in ascending order, with a secondary sort by created_on in descending order.
Parameters: - fields – List of fields to return in the result
- limit – Limits the number of records returned
- order_by – Sort response based on certain fields
- offset – A number of records to skip before returning records (for pagination)
Returns: Iterable chain object
-
get_one
(fields=[])[source]¶ Convenience function for queries returning only one result. Validates response before returning.
Parameters: fields – List of fields to return in the result
Raise: MultipleResults: if more than one match is found Returns: Record content
-
insert
(payload)[source]¶ Inserts a new record with the payload passed as an argument
Parameters: payload – The record to create (dict) Returns: Created record
-
delete
()[source]¶ Deletes the queried record and returns response content after response validation
Raise: NoResults: if query returned no results NotImplementedError: if query returned more than one result (currently not supported) Returns: Delete response content (Generally always {‘Success’: True})
-
update
(payload)[source]¶ Updates the queried record with
payload
and returns the updated record after validating the responseParameters: payload – Payload to update the record with
Raise: NoResults: if query returned no results MultipleResults: if query returned more than one result (currently not supported) Returns: The updated record
-
clone
(reset_fields=[])[source]¶ Clones the queried record
Parameters: reset_fields – Fields to reset
Raise: NoResults: if query returned no results MultipleResults: if query returned more than one result (currently not supported) UnexpectedResponse: informs the user about what likely went wrong Returns: The cloned record
-
attach
(file)[source]¶ Attaches the queried record with
file
and returns the response after validating the responseParameters: file – File to attach to the record
Raise: NoResults: if query returned no results MultipleResults: if query returned more than one result (currently not supported) Returns: The attachment record metadata
-
exception
pysnow.exceptions.
UnexpectedResponse
(code_expected, code_actual, http_method, error_summary, error_details)[source]¶ Informs the user about what went wrong when interfacing with the API
Parameters: - code_expected – Expected HTTP status code
- code_actual – Actual HTTP status code
- http_method – HTTP method used
- error_summary – Summary of what went wrong
- error_details – Details about the error
Installation¶
$ pip install pysnow
Compatibility¶
Python 2 and 3. Tested: Python 2.6+ and Python 3.3+