Skip to content

Commit 15921c9

Browse files
hiohiohios-meitoma
andauthored
feat: add user-agent header (#58)
Co-authored-by: s-meitoma <[email protected]>
1 parent 67b81dc commit 15921c9

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

jquantsapi/client.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import platform
34
import sys
45
from concurrent.futures import ThreadPoolExecutor, as_completed
56
from datetime import datetime
@@ -42,6 +43,8 @@ class Client:
4243

4344
JQUANTS_API_BASE = "https://api.jpx-jquants.com/v1"
4445
MAX_WORKERS = 5
46+
USER_AGENT = "jqapi-python"
47+
USER_AGENT_VERSION = "0.0.0"
4548
RAW_ENCODING = "utf-8"
4649

4750
def __init__(
@@ -165,7 +168,10 @@ def _base_headers(self) -> dict:
165168
J-Quants API にアクセスする際にヘッダーにIDトークンを設定
166169
"""
167170
id_token = self.get_id_token()
168-
headers = {"Authorization": f"Bearer {id_token}"}
171+
headers = {
172+
"Authorization": f"Bearer {id_token}",
173+
"User-Agent": f"{self.USER_AGENT}/{self.USER_AGENT_VERSION} p/{platform.python_version()}",
174+
}
169175
return headers
170176

171177
def _request_session(
@@ -249,7 +255,13 @@ def _post(
249255
"""
250256
s = self._request_session()
251257

252-
ret = s.post(url, data=data, json=json, headers=headers, timeout=30)
258+
base_headers = {
259+
"User-Agent": f"{self.USER_AGENT}/{self.USER_AGENT_VERSION} p/{platform.python_version()}",
260+
}
261+
if headers is not None:
262+
base_headers.update(headers)
263+
264+
ret = s.post(url, data=data, json=json, headers=base_headers, timeout=30)
253265
ret.raise_for_status()
254266
return ret
255267

0 commit comments

Comments
 (0)