Response¶
pynautobot.core.response
¶
pynautobot.core.response.JsonField
¶
Bases: object
Explicit field type for values that are not to be converted to a Record object.
pynautobot.core.response.Record
¶
Bases: object
Create Python objects from Nautobot API responses.
Creates an object from a Nautobot response passed as values
.
Nested dicts that represent other endpoints are also turned
into Record objects. All fields are then assigned to the
object's attributes. If a missing attribute is requested
(e.g., requesting a field that's only present on a full response on
a Record made from a nested response), the pynautobot will make a
request for the full object and return the requested value.
Examples:
Default representation of the object usually contains object's class, object's name, and its location in memory
>>> x = nb.dcim.devices.get(1)
>>> x
<pynautobot.models.dcim.Devices ('test1-switch1') at 0x1953d821250>
>>>
Querying a string field.
Querying a field on a nested object.
Casting the object as a dictionary.
>>> from pprint import pprint
>>> pprint(dict(x))
{'asset_tag': None,
'cluster': None,
'comments': '',
'config_context': {},
'created': '2018-04-01',
'custom_fields': {},
'device_role': {...},
'device_type': {...},
'display_name': 'test1-switch1',
'face': {'label': 'Rear', 'value': 1},
'id': 1,
'name': 'test1-switch1',
'parent_device': None,
'platform': {...},
'position': 1,
'primary_ip': {...},
'primary_ip4': {...},
'primary_ip6': None,
'rack': {...},
'serial': 'ABC123',
'site': {...},
'status': {...},
'tags': [],
'tenant': None,
'vc_position': None,
'vc_priority': None,
'virtual_chassis': None}
Iterating over a Record object.
>>> for i in x:
... print(i)
...
('id', 1)
('name', 'test1-switch1')
('display_name', 'test1-switch1')
>>>
__getattr__(k)
¶
Default behavior for missing attributes.
We'll call full_details()
if we're asked for an attribute
we don't have.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k |
str
|
The name of the attribute. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
The value of the requested attribute. |
Raises:
Type | Description |
---|---|
AttributeError
|
If the attribute is not found. |
Note
In order to prevent non-explicit behavior, k='keys'
is
excluded because casting to dict() calls this attribute.
delete()
¶
full_details()
¶
Queries the hyperlinked endpoint if 'url' is defined.
This method will populate the attributes from the detail
endpoint when it's called. Sets the class-level has_details
attribute when it's called to prevent being called more
than once.
Returns:
Type | Description |
---|---|
True |
save()
¶
Saves changes to an existing object.
Takes a diff between the object's current state and its state at initialization and sends them as a dictionary to Request.patch().
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the PATCH request was successful. |
Examples:
serialize(nested=False, init=False)
¶
Serializes the object.
Pulls all the attributes in an object and creates a dictionary that can be turned into the JSON format expected by Nautobot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nested |
bool
|
Whether to include nested objects in the serialization. Default is False. |
False
|
init |
bool
|
Whether to include initialization attributes in the serialization. Default is False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary representation of the serialized object. |
Note
Using this method to get a dictionary representation of the record is discouraged. It's probably better to cast to dict() instead. See the Record docstring for an example.
update(data)
¶
Update an object with a dictionary.
Accepts a dictionary and uses it to update the record and call save(). For nested and choice fields you'd pass an int the same as if you were modifying the attribute and calling save().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
Dictionary containing the key-value pairs to update the record object with. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the PATCH request was successful. |
Examples:
updates()
¶
Compiles changes for an existing object into a dictionary.
Takes a diff between the object's current state and its state at initialization and returns them as a dictionary. Returns an empty dictionary if no changes have been made.
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary containing the changes made to the object. |
Examples:
pynautobot.core.response.get_return(lookup, return_fields=None)
¶
Returns simple representations for items passed to lookup.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lookup |
The object or collection to retrieve representations for. |
required | |
return_fields |
list
|
A list of fields to reference when calling values on lookup. |
None
|
Returns:
Type | Description |
---|---|
The simple representation of the object or collection. |
Note
Used to return a "simple" representation of objects and collections sent to it via lookup. Otherwise, we look to see if lookup is a "choices" field (dict with only 'id' and 'value') or a nested_return. Finally, we check if it's a Record, if so simply return a string. Order is important due to nested_return being self-referential.