Skip to content

Commit 37e1b02

Browse files
authored
Fix token authorization for get_version (#734)
1 parent 78d4903 commit 37e1b02

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

pynetbox/core/query.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ def get_version(self):
295295
RequestError if req.ok returns false.
296296
"""
297297
headers = {"Content-Type": "application/json"}
298-
if self.token:
299-
headers["authorization"] = "Token {}".format(self.token)
298+
self._add_auth_header(headers)
300299
req = self.http_session.get(
301300
self.normalize_url(self.base),
302301
headers=headers,
@@ -316,11 +315,7 @@ def get_status(self):
316315
RequestError if request is not successful.
317316
"""
318317
headers = {"Content-Type": "application/json"}
319-
if self.token:
320-
if _is_v2_token(self.token):
321-
headers["authorization"] = "Bearer {}".format(self.token)
322-
else:
323-
headers["authorization"] = "Token {}".format(self.token)
318+
self._add_auth_header(headers)
324319
req = self.http_session.get(
325320
"{}status/".format(self.normalize_url(self.base)),
326321
headers=headers,
@@ -337,6 +332,18 @@ def normalize_url(self, url):
337332

338333
return url
339334

335+
def _add_auth_header(self, headers):
336+
"""Add authorization header to headers dict if token is present.
337+
338+
## Parameters
339+
* **headers** (dict): Headers dictionary to update with authorization.
340+
"""
341+
if self.token:
342+
if _is_v2_token(self.token):
343+
headers["authorization"] = "Bearer {}".format(self.token)
344+
else:
345+
headers["authorization"] = "Token {}".format(self.token)
346+
340347
def _make_call(self, verb="get", url_override=None, add_params=None, data=None):
341348
# Extract any file-like objects from data
342349
files = None
@@ -361,11 +368,7 @@ def _make_call(self, verb="get", url_override=None, add_params=None, data=None):
361368
if should_be_json_body:
362369
headers["Content-Type"] = "application/json"
363370

364-
if self.token:
365-
if _is_v2_token(self.token):
366-
headers["authorization"] = "Bearer {}".format(self.token)
367-
else:
368-
headers["authorization"] = "Token {}".format(self.token)
371+
self._add_auth_header(headers)
369372

370373
params = {}
371374
if not url_override:

0 commit comments

Comments
 (0)