Skip to content

Commit 06e2638

Browse files
authored
Added option to override user agent string (#62)
1 parent 188b840 commit 06e2638

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "socketdev"
7-
version = "3.0.24"
7+
version = "3.0.25"
88
requires-python = ">= 3.9"
99
dependencies = [
1010
'requests',

socketdev/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353

5454
class socketdev:
55-
def __init__(self, token: Optional[str] = None, timeout: int = 1200, allow_unverified: bool = False):
55+
def __init__(self, token: Optional[str] = None, timeout: int = 1200, allow_unverified: bool = False, user_agent: Optional[str] = None):
5656
# Try to get token from environment variables if not provided
5757
if token is None:
5858
token = (
@@ -73,6 +73,8 @@ def __init__(self, token: Optional[str] = None, timeout: int = 1200, allow_unver
7373
self.api.encode_key(self.token)
7474
self.api.set_timeout(timeout)
7575
self.api.set_allow_unverified(allow_unverified)
76+
if user_agent is not None:
77+
self.api.set_user_agent(user_agent)
7678

7779
self.dependencies = Dependencies(self.api)
7880
self.export = Export(self.api)

socketdev/core/api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self):
2626
self.api_url = "https://api.socket.dev/v0"
2727
self.request_timeout = 30
2828
self.allow_unverified = False
29+
self.user_agent = None
2930

3031
def encode_key(self, token: str):
3132
self.encoded_key = base64.b64encode(token.encode()).decode("ascii")
@@ -36,6 +37,9 @@ def set_timeout(self, timeout: int):
3637
def set_allow_unverified(self, allow_unverified: bool):
3738
self.allow_unverified = allow_unverified
3839

40+
def set_user_agent(self, user_agent: str):
41+
self.user_agent = user_agent
42+
3943
def do_request(
4044
self,
4145
path: str,
@@ -48,9 +52,10 @@ def do_request(
4852
raise APIKeyMissing
4953

5054
if headers is None:
55+
user_agent_string = self.user_agent if self.user_agent is not None else f"SocketSDKPython/{__version__}"
5156
headers = {
5257
"Authorization": f"Basic {self.encoded_key}",
53-
"User-Agent": f"SocketSDKPython/{__version__}",
58+
"User-Agent": user_agent_string,
5459
"accept": "application/json",
5560
}
5661
url = f"{self.api_url}/{path}"

socketdev/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.24"
1+
__version__ = "3.0.25"

0 commit comments

Comments
 (0)