Skip to content

Commit 5f8c0f2

Browse files
author
Zach Moody
committed
Update docs
1 parent 2839e6d commit 5f8c0f2

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/advanced.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Custom Sessions
2+
===============
3+
4+
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/>`_.
5+
6+
Headers
7+
*******
8+
9+
To set a custom header on all requests. These headers are automatically merged with headers pynetbox sets itself.
10+
11+
:Example:
12+
13+
>>> import pynetbox
14+
>>> import requests
15+
>>> session = requests.Session()
16+
>>> session.headers = {"mycustomheader": "test"}
17+
>>> nb = pynetbox.api(
18+
... 'http://localhost:8000',
19+
... private_key_file='/path/to/private-key.pem',
20+
... token='d6f4e314a5b5fefd164995169f28ae32d987704f'
21+
... )
22+
>>> nb.http_session = session
23+
24+
25+
SSL Verification
26+
****************
27+
28+
To disable SSL verification. See `the docs <https://requests.readthedocs.io/en/stable/user/advanced/#ssl-cert-verification>`_.
29+
30+
:Example:
31+
32+
>>> import pynetbox
33+
>>> import requests
34+
>>> session = requests.Session()
35+
>>> session.verify = False
36+
>>> nb = pynetbox.api(
37+
... 'http://localhost:8000',
38+
... private_key_file='/path/to/private-key.pem',
39+
... token='d6f4e314a5b5fefd164995169f28ae32d987704f'
40+
... )
41+
>>> nb.http_session = session
42+
43+
44+
Timeouts
45+
********
46+
47+
Setting timeouts requires the use of Adapters.
48+
49+
:Example:
50+
51+
.. code-block:: python
52+
53+
from requests.adapters import HTTPAdapter
54+
55+
class TimeoutHTTPAdapter(HTTPAdapter):
56+
def __init__(self, *args, **kwargs):
57+
self.timeout = kwargs.get("timeout", 5)
58+
super().__init__(*args, **kwargs)
59+
60+
def send(self, request, **kwargs):
61+
kwargs['timeout'] = self.timeout
62+
return super().send(request, **kwargs)
63+
64+
adapter = TimeoutHTTPAdapter()
65+
session = requests.Session()
66+
session.mount("http://", adapter)
67+
session.mount("https://", adapter)
68+
69+
nb = pynetbox.api(
70+
'http://localhost:8000',
71+
private_key_file='/path/to/private-key.pem',
72+
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
73+
)
74+
nb.http_session = session
75+

0 commit comments

Comments
 (0)