Skip to content

Commit 522a3b8

Browse files
author
Zach Moody
authored
Merge pull request #255 from digitalocean/docs
Docs
2 parents 21b40ff + 5f8c0f2 commit 522a3b8

File tree

4 files changed

+89
-8
lines changed

4 files changed

+89
-8
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+

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Instantiate the :py:class:`.Api`. Use the methods available on :py:class:`.Endpo
1414
API
1515
===
1616

17-
.. autoclass:: pynetbox.api.Api
17+
.. autoclass:: pynetbox.core.api.Api
1818
:members:
1919

2020
App
2121
===
2222

23-
.. autoclass:: pynetbox.api.App
23+
.. autoclass:: pynetbox.core.app.App
2424
:members:
2525

2626
Indices and tables

pynetbox/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pkg_resources import get_distribution, DistributionNotFound
22

33
from pynetbox.core.query import RequestError, AllocationError, ContentError
4-
from pynetbox.api import Api as api
4+
from pynetbox.core.api import Api as api
55

66
try:
77
__version__ = get_distribution(__name__).version

pynetbox/api.py renamed to pynetbox/core/api.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
class Api(object):
25-
""" The API object is the point of entry to pynetbox.
25+
"""The API object is the point of entry to pynetbox.
2626
2727
After instantiating the Api() with the appropriate named arguments
2828
you can specify which app and endpoint you wish to interact with.
@@ -39,6 +39,13 @@ class Api(object):
3939
Calling any of these attributes will return
4040
:py:class:`.App` which exposes endpoints as attributes.
4141
42+
**Additional Attributes**:
43+
* **http_session(requests.Session)**:
44+
Override the default session with your own. This is used to control
45+
a number of HTTP behaviors such as SSL verification, custom headers,
46+
retires, and timeouts.
47+
See `custom sessions <advanced.html#custom-sessions>`__ for more info.
48+
4249
:param str url: The base URL to the instance of NetBox you
4350
wish to connect to.
4451
:param str token: Your NetBox token.
@@ -77,8 +84,7 @@ def __init__(
7784
self.http_session = requests.Session()
7885
if threading and sys.version_info.major == 2:
7986
raise NotImplementedError(
80-
"Threaded pynetbox calls not supported \
81-
in Python 2"
87+
"Threaded pynetbox calls not supported in Python 2"
8288
)
8389
self.threading = threading
8490

@@ -97,7 +103,7 @@ def __init__(
97103

98104
@property
99105
def version(self):
100-
""" Gets the API version of NetBox.
106+
"""Gets the API version of NetBox.
101107
102108
Can be used to check the NetBox API version if there are
103109
version-dependent features or syntaxes in the API.
@@ -121,7 +127,7 @@ def version(self):
121127
return version
122128

123129
def openapi(self):
124-
""" Returns the OpenAPI spec.
130+
"""Returns the OpenAPI spec.
125131
126132
Quick helper function to pull down the entire OpenAPI spec.
127133

0 commit comments

Comments
 (0)