Skip to content

Commit 3e1f8a7

Browse files
committed
updated docs
1 parent cd210bc commit 3e1f8a7

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ Request behavior can be modified by setting the `request_options` keyword argume
230230
>>> handler = ipinfo.getHandler(request_options={'timeout': 4})
231231
```
232232

233+
### Custom Headers
234+
235+
You can add custom headers or modify default headers by setting the `headers` keyword argument when initializing the handler. `headers` is a dictionary of `{'header': 'value'}` format.
236+
237+
```python
238+
>>> handler = ipinfo.getHandler(headers={'user-agent': 'My Custom User-agent', 'custom_header': 'yes'})
239+
```
240+
233241
### Internationalization
234242

235243
When looking up an IP address, the response object includes a `details.country_name`, `details.isEU`, `details.country_flag` and `details.country_currency` attributes which includes the country based on American English. It is possible to return the country name in other languages by setting the `countries_file`, remove or add EU countries by setting the keyword argument `eu_countries_file`, change the country flag emoji or unicode by setting the keyword argument `countries_flags_file` or change country's currency code or currency symbol by setting the `countries_currencies` when creating the `IPinfo` object. Moreover the response object includes a `details.continent` which includes continent code and name of IP. The default file can be changed by setting the `continent_file` while creating the `IPinfo` object.

ipinfo/handler_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def __init__(self, access_token=None, **kwargs):
9999
cache_options["ttl"] = CACHE_TTL
100100
self.cache = DefaultCache(**cache_options)
101101

102-
# setup custom headers
102+
# setup custom headers
103103
self.headers = kwargs.get("headers", None)
104104

105105
async def init(self):

ipinfo/handler_utils.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,21 @@
4545

4646

4747
def get_headers(access_token, custom_headers):
48-
headers = {}
49-
50-
if custom_headers:
51-
headers = custom_headers
52-
else:
53-
"""Build headers for request to IPinfo API."""
54-
headers = {
55-
"user-agent": "IPinfoClient/Python{version}/{sdk_version}".format(
56-
version=sys.version_info[0], sdk_version=SDK_VERSION
48+
"""Build headers for request to IPinfo API."""
49+
headers = {
50+
"user-agent": "IPinfoClient/Python{version}/{sdk_version}".format(
51+
version=sys.version_info[0], sdk_version=SDK_VERSION
5752
),
5853
"accept": "application/json",
59-
}
54+
}
55+
56+
if custom_headers:
57+
headers = {** headers, ** custom_headers}
6058

6159
if access_token:
6260
headers["authorization"] = "Bearer {}".format(access_token)
6361

62+
print(headers)
6463
return headers
6564

6665

tests/handler_async_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def test_init():
2121
@pytest.mark.asyncio
2222
async def test_headers():
2323
token = "mytesttoken"
24-
handler = AsyncHandler(token, headers={"user-agent": "test-agent", "accept": "application/json", "custom_field": "yes"})
24+
handler = AsyncHandler(token, headers={"custom_field": "yes"})
2525
headers = handler_utils.get_headers(token, handler.headers)
2626
await handler.deinit()
2727

tests/handler_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_init():
2020

2121
def test_headers():
2222
token = "mytesttoken"
23-
handler = Handler(token, headers={"user-agent": "test-agent", "accept": "application/json", "custom_field": "yes"})
23+
handler = Handler(token, headers={"custom_field": "yes"})
2424
headers = handler_utils.get_headers(token, handler.headers)
2525

2626
assert "user-agent" in headers

0 commit comments

Comments
 (0)