Query Module

exception pynautobot.core.query.AllocationError(message)

Bases: Exception

Allocation Exception

Used with available-ips/available-prefixes when there is no room for allocation and Nautobot returns 204 No Content.

__init__(message)
exception pynautobot.core.query.ContentError(message)

Bases: Exception

Content Exception

If the API URL does not point to a valid Nautobot API, the server may return a valid response code, but the content is not json. This exception is raised in those cases.

__init__(message)
class pynautobot.core.query.Request(base, http_session, filters=None, key=None, token=None, threading=False, api_version=None)

Bases: object

Creates requests to the Nautobot API

Responsible for building the url and making the HTTP(S) requests to Nautobot’s API

Parameters:
  • base – (str) Base URL passed in api() instantiation.

  • filters – (dict, optional) contains key/value pairs that correlate to the filters a given endpoint accepts. In (e.g. /api/dcim/devices/?name=’test’) ‘name’: ‘test’ would be in the filters dict.

__init__(base, http_session, filters=None, key=None, token=None, threading=False, api_version=None)

Instantiates a new Request object

Parameters:
  • base (string) – Base URL passed in api() instantiation.

  • filters (dict, optional) – contains key/value pairs that correlate to the filters a given endpoint accepts. In (e.g. /api/dcim/devices/?name=’test’) ‘name’: ‘test’ would be in the filters dict.

  • key (int, optional) – database id of the item being queried.

  • api_version (str, optional) – Set to override the default Nautobot REST API Version.

concurrent_get(ret, page_size, page_offsets)
delete()

Makes DELETE request.

Makes a DELETE request to Nautobot’s API.

Returns:

True if successful.

Raises:

RequestError if req.ok doesn't return True.

get(add_params=None)

Makes a GET request.

Makes a GET request to Nautobot’s API, and automatically recurses any paginated results.

Raises:

RequestError if req.ok returns false.

Raises:

ContentError if response is not json.

Returns:

List of Response objects returned from the endpoint.

get_count(*args, **kwargs)

Returns object count for query

Makes a query to the endpoint with limit=1 set and only returns the value of the “count” field.

Raises:

RequestError if req.ok returns false.

Raises:

ContentError if response is not json.

Returns:

Int of number of objects query returned.

get_openapi()

Gets the OpenAPI Spec

get_status()

Gets the status from /api/status/ endpoint in Nautobot.

Returns:

Dictionary as returned by Nautobot.

Raises:

RequestError if request is not successful.

get_version()

Gets the API version of Nautobot.

Issues a GET request to the base URL to read the API version from the response headers.

Raises:

RequestError if req.ok returns false.

Returns:

Version number as a string. Empty string if version is not

present in the headers.

normalize_url(url)

Builds a url for POST actions.

options()

Makes an OPTIONS request.

Makes an OPTIONS request to Nautobot’s API.

Raises:

RequestError if req.ok returns false.

Raises:

ContentError if response is not json.

Returns:

Dict containing the response from Nautobot’s API.

patch(data)

Makes PATCH request.

Makes a PATCH request to Nautobot’s API.

Parameters:

data – (dict) Contains a dict that will be turned into a json object and sent to the API.

Raises:

RequestError if req.ok returns false.

Raises:

ContentError if response is not json.

Returns:

Dict containing the response from Nautobot’s API.

post(data)

Makes POST request.

Makes a POST request to Nautobot’s API.

Parameters:

data – (dict) Contains a dict that will be turned into a json object and sent to the API.

Raises:

RequestError if req.ok returns false.

Raises:

AllocationError if req.status_code is 204 (No Content) as with available-ips and available-prefixes when there is no room for the requested allocation.

Raises:

ContentError if response is not json.

Returns:

Dict containing the response from Nautobot’s API.

put(data)

Makes PUT request.

Makes a PUT request to Nautobot’s API.

Parameters:

data – (dict) Contains a dict that will be turned into a json object and sent to the API.

Raises:

RequestError if req.ok returns false.

Raises:

ContentError if response is not json.

Returns:

Dict containing the response from Nautobot’s API.

exception pynautobot.core.query.RequestError(message)

Bases: Exception

Basic Request Exception

More detailed exception that returns the original requests object for inspection. Along with some attributes with specific details from the requests object. If return is json we decode and add it to the message.

Example:

>>> try:
...   nb.dcim.devices.create(name="destined-for-failure")
... except pynautobot.RequestError as e:
...   print(e.error)
__init__(message)
exception pynautobot.core.query.RequestErrorFromException

Bases: Exception

RequestErrorFromException is raised from exception.

pynautobot.core.query.calc_pages(limit, count)

Calculate number of pages required for full results set.