vercel-headers 0.6.0__tar.gz

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,128 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *$py.class
4
+
5
+ # C extensions
6
+ *.so
7
+
8
+ # Distribution / packaging
9
+ .Python
10
+ build/
11
+ develop-eggs/
12
+ dist/
13
+ downloads/
14
+ eggs/
15
+ .eggs/
16
+ lib/
17
+ lib64/
18
+ parts/
19
+ sdist/
20
+ var/
21
+ wheels/
22
+ share/python-wheels/
23
+ *.egg-info/
24
+ .installed.cfg
25
+ *.egg
26
+ MANIFEST
27
+
28
+ # PyInstallerhave
29
+ *.manifest
30
+ *.spec
31
+
32
+ # Installer logs
33
+ pip-log.txt
34
+ pip-delete-this-directory.txt
35
+
36
+ # Unit test / coverage reports
37
+ htmlcov/
38
+ .tox/
39
+ .nox/
40
+ .coverage
41
+ .coverage.*
42
+ .cache
43
+ nosetests.xml
44
+ coverage.xml
45
+ *.cover
46
+ *.py,cover
47
+ .hypothesis/
48
+ .pytest_cache/
49
+ .ruff_cache/
50
+
51
+ # Translations
52
+ *.mo
53
+ *.pot
54
+
55
+ # Scrapy
56
+ .scrapy
57
+
58
+ # Sphinx documentation
59
+ docs/_build/
60
+
61
+ # PyBuilder
62
+ target/
63
+
64
+ # Jupyter Notebook
65
+ .ipynb_checkpoints
66
+
67
+ # IPython
68
+ profile_default/
69
+ ipython_config.py
70
+
71
+ # pyenv
72
+ .python-version
73
+
74
+ # pipenv
75
+ Pipfile.lock
76
+
77
+ # poetry
78
+ poetry.lock
79
+
80
+ # PDM
81
+ pdm.lock
82
+ .pdm.toml
83
+
84
+ # Hatch
85
+ .hatch/
86
+
87
+ # pyright/mypy
88
+ .mypy_cache/
89
+ .dmypy.json
90
+ dmypy.json
91
+
92
+ # pyre
93
+ .pyre/
94
+
95
+ # pytype
96
+ .pytype/
97
+
98
+ # Caches
99
+ __pypackages__/
100
+
101
+ # Editor/project files
102
+ .DS_Store
103
+ .idea/
104
+ .vscode/
105
+ *.swp
106
+ *.swo
107
+
108
+ # Virtual environments
109
+ .env
110
+ .venv
111
+ .venv.*
112
+ env/
113
+ venv/
114
+ **/.env
115
+ **/.venv
116
+ **/.venv.*
117
+ **/env/
118
+ **/venv/
119
+ ENV/
120
+ env.bak/
121
+ venv.bak/
122
+
123
+ # dotenv anywhere
124
+ **/.env
125
+ **/.env.*
126
+ **/*.env
127
+
128
+ .claude
@@ -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.
@@ -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,14 @@
1
+ # Headers
2
+
3
+ `vercel.headers` stores request headers for Vercel Function helpers and exposes
4
+ IP address and geolocation helpers.
5
+
6
+ Register request headers once per request:
7
+
8
+ ```python
9
+ from vercel.headers import set_headers
10
+
11
+ set_headers(request.headers)
12
+ ```
13
+
14
+ OIDC and cache helpers read the same registered header context.
@@ -0,0 +1,37 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.27.0,<2"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "vercel-headers"
7
+ dynamic = ["version"]
8
+ description = "Request header helpers for Vercel Python applications"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ license-files = ["LICENSE", "LICENSE.*"]
13
+ dependencies = []
14
+
15
+ [tool.hatch.version]
16
+ path = "vercel/headers/version.py"
17
+
18
+ [tool.hatch.build.targets.sdist]
19
+ include = [
20
+ "/vercel/headers/**/*.py",
21
+ "/vercel/headers/py.typed",
22
+ "/README.md",
23
+ "/pyproject.toml",
24
+ "/LICENSE",
25
+ ]
26
+ exclude = [
27
+ "/**/__pycache__",
28
+ ]
29
+
30
+ [tool.hatch.build.targets.wheel]
31
+ dev-mode-dirs = ["."]
32
+ only-include = [
33
+ "/vercel/headers",
34
+ ]
35
+ exclude = [
36
+ "/**/__pycache__",
37
+ ]
@@ -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
+ }
File without changes
@@ -0,0 +1 @@
1
+ __version__ = "0.6.0"