vercel-headers 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import urllib.parse
|
|
4
|
+
from collections.abc import Mapping
|
|
5
|
+
from contextvars import ContextVar
|
|
6
|
+
from typing import Protocol, TypedDict
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"ip_address",
|
|
10
|
+
"geolocation",
|
|
11
|
+
"Geo",
|
|
12
|
+
"set_headers",
|
|
13
|
+
"get_headers",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
_cv_headers: ContextVar[Mapping[str, str] | None] = ContextVar("vercel_headers", default=None)
|
|
18
|
+
|
|
19
|
+
# Header constants (same as TS names)
|
|
20
|
+
CITY_HEADER_NAME = "x-vercel-ip-city"
|
|
21
|
+
COUNTRY_HEADER_NAME = "x-vercel-ip-country"
|
|
22
|
+
IP_HEADER_NAME = "x-real-ip"
|
|
23
|
+
LATITUDE_HEADER_NAME = "x-vercel-ip-latitude"
|
|
24
|
+
LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude"
|
|
25
|
+
REGION_HEADER_NAME = "x-vercel-ip-country-region"
|
|
26
|
+
POSTAL_CODE_HEADER_NAME = "x-vercel-ip-postal-code"
|
|
27
|
+
REQUEST_ID_HEADER_NAME = "x-vercel-id"
|
|
28
|
+
|
|
29
|
+
EMOJI_FLAG_UNICODE_STARTING_POSITION = 127397
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def set_headers(headers: Mapping[str, str] | None) -> None:
|
|
33
|
+
_cv_headers.set(headers)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def get_headers() -> Mapping[str, str] | None:
|
|
37
|
+
return _cv_headers.get()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class _HeadersLike(Protocol):
|
|
41
|
+
def get(self, name: str) -> str | None: ...
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class _RequestLike(Protocol):
|
|
45
|
+
headers: _HeadersLike
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Geo(TypedDict, total=False):
|
|
49
|
+
city: str | None
|
|
50
|
+
country: str | None
|
|
51
|
+
flag: str | None
|
|
52
|
+
region: str | None
|
|
53
|
+
countryRegion: str | None
|
|
54
|
+
latitude: str | None
|
|
55
|
+
longitude: str | None
|
|
56
|
+
postalCode: str | None
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _get_header(headers: _HeadersLike, key: str) -> str | None:
|
|
60
|
+
return headers.get(key)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _get_header_decode(req: _RequestLike, key: str) -> str | None:
|
|
64
|
+
raw = _get_header(req.headers, key)
|
|
65
|
+
return urllib.parse.unquote(raw) if raw is not None else None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _get_flag(country_code: str | None) -> str | None:
|
|
69
|
+
if not country_code or len(country_code) != 2 or not country_code.isalpha():
|
|
70
|
+
return None
|
|
71
|
+
return "".join(chr(EMOJI_FLAG_UNICODE_STARTING_POSITION + ord(c)) for c in country_code.upper())
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def ip_address(input: _RequestLike | _HeadersLike) -> str | None:
|
|
75
|
+
headers = input.headers if hasattr(input, "headers") else input
|
|
76
|
+
return _get_header(headers, IP_HEADER_NAME)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _region_from_request_id(request_id: str | None) -> str | None:
|
|
80
|
+
if request_id is None:
|
|
81
|
+
return "dev1"
|
|
82
|
+
return request_id.split(":")[0]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def geolocation(request: _RequestLike) -> Geo:
|
|
86
|
+
headers = request.headers
|
|
87
|
+
return {
|
|
88
|
+
"city": _get_header_decode(request, CITY_HEADER_NAME),
|
|
89
|
+
"country": _get_header(headers, COUNTRY_HEADER_NAME),
|
|
90
|
+
"flag": _get_flag(_get_header(headers, COUNTRY_HEADER_NAME)),
|
|
91
|
+
"countryRegion": _get_header(headers, REGION_HEADER_NAME),
|
|
92
|
+
"region": _region_from_request_id(_get_header(headers, REQUEST_ID_HEADER_NAME)),
|
|
93
|
+
"latitude": _get_header(headers, LATITUDE_HEADER_NAME),
|
|
94
|
+
"longitude": _get_header(headers, LONGITUDE_HEADER_NAME),
|
|
95
|
+
"postalCode": _get_header(headers, POSTAL_CODE_HEADER_NAME),
|
|
96
|
+
}
|
vercel/headers/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.6.0"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vercel-headers
|
|
3
|
+
Version: 0.6.0
|
|
4
|
+
Summary: Request header helpers for Vercel Python applications
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# Headers
|
|
11
|
+
|
|
12
|
+
`vercel.headers` stores request headers for Vercel Function helpers and exposes
|
|
13
|
+
IP address and geolocation helpers.
|
|
14
|
+
|
|
15
|
+
Register request headers once per request:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from vercel.headers import set_headers
|
|
19
|
+
|
|
20
|
+
set_headers(request.headers)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
OIDC and cache helpers read the same registered header context.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
vercel/headers/__init__.py,sha256=n01mpxW9e8FIEzBN_Lu7R-TY_3flBAyWAXruyFaypHY,2851
|
|
2
|
+
vercel/headers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
vercel/headers/version.py,sha256=cID1jLnC_vj48GgMN6Yb1FA3JsQ95zNmCHmRYE8TFhY,22
|
|
4
|
+
vercel_headers-0.6.0.dist-info/METADATA,sha256=xJ2fBhHf0KO1aFYmOrTepwNXqH0YVbPWC5_HfXgFPmY,552
|
|
5
|
+
vercel_headers-0.6.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
6
|
+
vercel_headers-0.6.0.dist-info/licenses/LICENSE,sha256=ZhFC5TwxPSu14bBV9cCjkAFFD_G14nuJ3EvH3ppjUso,1069
|
|
7
|
+
vercel_headers-0.6.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vercel, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|