Skip to content

Commit 7e6f445

Browse files
authored
🧑‍💻 add pat support for codegen
1 parent 34c26d2 commit 7e6f445

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

codegen/source.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import httpx
77
from jsonpointer import JsonPointer
88

9+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
910
REPO_COMMIT = os.getenv(
1011
"REPO_COMMIT",
1112
"https://api.github.com/repos/github/rest-api-description/commits/main",
@@ -52,6 +53,13 @@ def __truediv__(self, other: str | int) -> "Source":
5253
return self.resolve_ref(str(httpx.URL(fragment=fragment)))
5354

5455

56+
@cache
57+
def _get_auth_header() -> dict[str, str]:
58+
if GITHUB_TOKEN:
59+
return {"Authorization": f"Bearer {GITHUB_TOKEN}"}
60+
return {}
61+
62+
5563
@cache
5664
def get_content(source: str | httpx.URL) -> tuple[httpx.URL, dict]:
5765
if isinstance(source, str):
@@ -60,6 +68,7 @@ def get_content(source: str | httpx.URL) -> tuple[httpx.URL, dict]:
6068
headers={
6169
"User-Agent": "GitHubKit Codegen",
6270
"Accept": "application/vnd.github.sha",
71+
**_get_auth_header(),
6372
},
6473
timeout=httpx.Timeout(10.0),
6574
)
@@ -70,7 +79,9 @@ def get_content(source: str | httpx.URL) -> tuple[httpx.URL, dict]:
7079
source_link = source
7180

7281
response = httpx.get(
73-
source_link, headers={"User-Agent": "GitHubKit Codegen"}, follow_redirects=True
82+
source_link,
83+
headers={"User-Agent": "GitHubKit Codegen", **_get_auth_header()},
84+
follow_redirects=True,
7485
)
7586
response.raise_for_status()
7687
uri = response.url

docs/contributing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ Generate latest models and apis from GitHub's OpenAPI schema:
2424

2525
This may use about **400M** memory and take a long time.
2626

27-
Please make sure you have activated the virtual environment.
28-
2927
```bash
3028
uv run bash ./scripts/run-codegen.sh
3129
```
3230

31+
If you encounter rate limit error, you can set `GITHUB_TOKEN` environment variable to a GitHub personal access token.
32+
3333
### Patch Schema
3434

35-
If you encounter an schema error, you can patch the schema by modifying the `pyproject.toml` file.
35+
If you encounter schema error, you can patch the schema by modifying the `pyproject.toml` file.
3636

3737
In the `[tool.codegen.overrides.schema_overrides]` section, you can modify the schema using json pointer. The value will override the original schema.
3838

0 commit comments

Comments
 (0)