Skip to content

Commit

Permalink
Merge pull request #113 from packethost/add-user-agent
Browse files Browse the repository at this point in the history
add user-agent headers to client requests
  • Loading branch information
displague authored May 20, 2021
2 parents a99ea66 + d21e81f commit 8cdc676
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __pycache__/
# C extensions
*.so
.idea/
.vscode/

# Distribution / packaging
.Python
Expand Down
12 changes: 11 additions & 1 deletion packet/baseapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging
import requests

from packet import __version__


class Error(Exception): # pragma: no cover
"""Base exception class for this module"""
Expand Down Expand Up @@ -47,12 +49,19 @@ class BaseAPI(object):
Basic api class for
"""

def __init__(self, auth_token, consumer_token):
def __init__(self, auth_token, consumer_token, user_agent=""):
self.auth_token = auth_token
self.consumer_token = consumer_token
self.end_point = "api.packet.net"
self._user_agent_prefix = user_agent
self._log = logging.getLogger(__name__)

@property
def user_agent(self):
return "{}packet-python/{} {}".format(
self._user_agent_prefix, __version__, requests.utils.default_user_agent()
).strip()

def call_api(self, method, type="GET", params=None): # noqa
if params is None:
params = {}
Expand All @@ -63,6 +72,7 @@ def call_api(self, method, type="GET", params=None): # noqa
"X-Auth-Token": self.auth_token,
"X-Consumer-Token": self.consumer_token,
"Content-Type": "application/json",
"User-Agent": self.user_agent,
}

# remove token from log
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def get_version(rel_path):
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/packethost/packet-python",
author="Packet Developers",
author="Equinix Metal Developers",
author_email="support@equinixmetal.com",
license="LGPL v3",
keywords="packet api client",
packages=["packet"],
Expand Down

0 comments on commit 8cdc676

Please # to comment.