Skip to content

Commit 35a5245

Browse files
committed
feat: add useragent to graphql requests with caller info
1 parent 217409b commit 35a5245

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

libs/graphql/src/graphql/client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Optional
22
from urllib import request, error
33
import json
44
import logging
@@ -7,14 +7,15 @@
77

88

99
class GraphQLClient:
10-
def __init__(self, endpoint: str):
10+
def __init__(self, endpoint: str, useragent: Optional[str] = None):
1111
self.endpoint = endpoint
1212
self.token = None
13+
self.useragent = useragent
1314

1415
def set_token(self, token: str):
1516
self.token = token
1617

17-
def execute(self, query: str, variables: Union[dict, None] = None, timeout=30):
18+
def execute(self, query: str, variables: Optional[dict] = None, timeout=30):
1819
data = {"query": query, "variables": variables}
1920
headers = {"Accept": "application/json", "Content-Type": "application/json"}
2021

@@ -25,6 +26,8 @@ def execute(self, query: str, variables: Union[dict, None] = None, timeout=30):
2526
): # does it have a 'prefix' like Bearer already there?
2627
token = f"Bearer {token}"
2728
headers["Authorization"] = token
29+
if self.useragent:
30+
headers["User-Agent"] = self.useragent
2831

2932
body = json.dumps(data).encode("utf-8")
3033

plugins/hardcover/src/hardcover/provider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from .identifier import HardcoverIdentifier
1212
from .models import Book
13+
from ._version import __version__
1314

1415

1516
class HardcoverProvider:
@@ -19,7 +20,8 @@ class HardcoverProvider:
1920
def __init__(self, source):
2021
self.source = source
2122
self.prefs = source.prefs
22-
self.client = GraphQLClient(self.API_URL)
23+
useragent = f"hardcover-calibre-plugin/{__version__} (https://github.com/RobBrazier/calibre-plugins)"
24+
self.client = GraphQLClient(self.API_URL, useragent)
2325

2426
def get_book_url(self, identifiers) -> tuple[str, str, str] | None:
2527
hardcover_id: str | None = identifiers.get(self.ID_NAME, None)

0 commit comments

Comments
 (0)