1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
# Advanced Usage
## Threading
PyNetBox supports multithreaded calls for `.filter()` and `.all()` queries to significantly improve performance when fetching large datasets.
!!! warning "NetBox Configuration Required"
It is **highly recommended** you have `MAX_PAGE_SIZE` in your NetBox installation set to anything *except* `0` or `None`. The default value of `1000` is usually a good value to use.
### Enabling Threading
Enable threading globally by passing `threading=True` to the API initialization:
```python
import pynetbox
nb = pynetbox.api(
'http://localhost:8000',
token='your-token',
threading=True
)
# Now all .all() and .filter() calls will use threading
devices = nb.dcim.devices.all() # Fetches pages in parallel
```
### How It Works
When threading is enabled:
- PyNetBox fetches multiple pages of results in parallel
- Significantly faster for large result sets
- Especially useful for `.all()` queries that span many pages
- Works automatically with pagination
### Example
```python
import pynetbox
import time
nb = pynetbox.api('http://localhost:8000', token='your-token')
# Without threading
start = time.time()
devices = list(nb.dcim.devices.all())
print(f"Without threading: {time.time() - start:.2f}s")
# With threading
nb_threaded = pynetbox.api(
'http://localhost:8000',
token='your-token',
threading=True
)
start = time.time()
devices = list(nb_threaded.dcim.devices.all())
print(f"With threading: {time.time() - start:.2f}s")
```
## Filter Validation
NetBox doesn't validate filters passed to GET API endpoints (`.get()` and `.filter()`). If a filter is incorrect, NetBox silently returns the entire database table content, which can be slow and unexpected.
PyNetBox can validate filter parameters against NetBox's OpenAPI specification before making the request, raising an exception if a parameter is invalid.
### Enabling Strict Filters Globally
```python
import pynetbox
nb = pynetbox.api(
'http://localhost:8000',
token='your-token',
strict_filters=True # Enable validation globally
)
# This will raise ParameterValidationError
try:
devices = nb.dcim.devices.filter(non_existing_filter='value')
except pynetbox.core.query.ParameterValidationError as e:
print(f"Invalid filter: {e}")
```
### Per-Request Validation
You can also enable or disable validation on a per-request basis:
```python
nb = pynetbox.api('http://localhost:8000', token='your-token')
# Enable for one request (when not globally enabled)
try:
devices = nb.dcim.devices.filter(
non_existing_filter='aaaa',
strict_filters=True
)
except pynetbox.core.query.ParameterValidationError as e:
print(f"Invalid filter: {e}")
# Disable for one request (when globally enabled)
nb_strict = pynetbox.api(
'http://localhost:8000',
token='your-token',
strict_filters=True
)
# This won't raise an exception, but returns entire table
devices = nb_strict.dcim.devices.filter(
non_existing_filter='aaaa',
strict_filters=False
)
```
### Benefits of Strict Filters
- **Catch typos early**: Find misspelled filter names before making requests
- **Prevent full table scans**: Avoid accidentally fetching entire tables
- **Better error messages**: Get clear feedback about invalid parameters
- **Development aid**: Helpful during development to ensure correct filter usage
### Example
```python
import pynetbox
nb = pynetbox.api(
'http://localhost:8000',
token='your-token',
strict_filters=True
)
# Valid filter - works fine
devices = nb.dcim.devices.filter(site='datacenter1')
# Invalid filter - raises exception
try:
devices = nb.dcim.devices.filter(iste='datacenter1') # Typo: 'iste' instead of 'site'
except pynetbox.core.query.ParameterValidationError as e:
print(f"Error: {e}")
# Error: 'iste' is not a valid filter parameter for dcim.devices
```
## Custom Sessions
Custom sessions can be used to modify the default HTTP behavior. Below are a few examples, most of them from [here](https://hodovi.ch/blog/advanced-usage-python-requests-timeouts-retries-hooks/).
### Headers
To set a custom header on all requests. These headers are automatically merged with headers pynetbox sets itself.
Example:
```python
import pynetbox
import requests
session = requests.Session()
session.headers = {"mycustomheader": "test"}
nb = pynetbox.api(
'http://localhost:8000',
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
)
nb.http_session = session
```
### SSL Verification
To disable SSL verification. See [the docs](https://requests.readthedocs.io/en/stable/user/advanced/#ssl-cert-verification).
Example:
```python
import pynetbox
import requests
session = requests.Session()
session.verify = False
nb = pynetbox.api(
'http://localhost:8000',
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
)
nb.http_session = session
```
### Timeouts
Setting timeouts requires the use of Adapters.
Example:
```python
from requests.adapters import HTTPAdapter
class TimeoutHTTPAdapter(HTTPAdapter):
def __init__(self, *args, **kwargs):
self.timeout = kwargs.get("timeout", 5)
super().__init__(*args, **kwargs)
def send(self, request, **kwargs):
kwargs['timeout'] = self.timeout
return super().send(request, **kwargs)
adapter = TimeoutHTTPAdapter()
session = requests.Session()
session.mount("http://", adapter)
session.mount("https://", adapter)
nb = pynetbox.api(
'http://localhost:8000',
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
)
nb.http_session = session
```
## File Uploads (Image Attachments)
Pynetbox supports file uploads for endpoints that accept them, such as image attachments. When you pass a file-like object (anything with a `.read()` method) to `create()`, pynetbox automatically detects it and uses multipart/form-data encoding instead of JSON.
### Creating an Image Attachment
```python
import pynetbox
nb = pynetbox.api(
'http://localhost:8000',
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
)
# Attach an image to a device
with open('/path/to/image.png', 'rb') as f:
attachment = nb.extras.image_attachments.create(
object_type='dcim.device',
object_id=1,
image=f,
name='rack-photo.png'
)
```
### Using io.BytesIO
You can also use in-memory file objects:
```python
import io
import pynetbox
nb = pynetbox.api(
'http://localhost:8000',
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
)
# Create image from bytes
image_data = b'...' # Your image bytes
file_obj = io.BytesIO(image_data)
file_obj.name = 'generated-image.png' # Optional: set filename
attachment = nb.extras.image_attachments.create(
object_type='dcim.device',
object_id=1,
image=file_obj
)
```
### Custom Filename and Content-Type
For more control, pass a tuple instead of a file object:
```python
with open('/path/to/image.png', 'rb') as f:
attachment = nb.extras.image_attachments.create(
object_type='dcim.device',
object_id=1,
image=('custom-name.png', f, 'image/png')
)
```
The tuple format is `(filename, file_object)` or `(filename, file_object, content_type)`.
## Multi-Format Responses
Some endpoints support multiple response formats. The rack elevation endpoint can return both JSON data and SVG diagrams.
### Getting Rack Elevation as JSON
By default, the elevation endpoint returns JSON data as a list of rack unit objects:
```python
import pynetbox
nb = pynetbox.api(
'http://localhost:8000',
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
)
rack = nb.dcim.racks.get(123)
# Returns list of RU objects (default JSON response)
units = rack.elevation.list()
for unit in units:
print(unit.id, unit.name)
```
### Getting Rack Elevation as SVG
Use the `render='svg'` parameter to get a graphical SVG diagram:
```python
rack = nb.dcim.racks.get(123)
# Returns raw SVG string
svg_diagram = rack.elevation.list(render='svg')
print(svg_diagram) # '<svg xmlns="http://www.w3.org/2000/svg">...</svg>'
# Save to file
with open('rack-elevation.svg', 'w') as f:
f.write(svg_diagram)
```
|