From 9e8cfe4514949701c81a16fb2579ba9448731e92 Mon Sep 17 00:00:00 2001 From: netomi Date: Fri, 31 Mar 2023 19:17:07 +0200 Subject: [PATCH 1/3] Add a REST_API_VERSION to each rest client and add a X-Github-Api-Version header for requests. --- codegen/__init__.py | 6 +++++- codegen/config.py | 1 + codegen/templates/client/_request.py.jinja | 4 +++- codegen/templates/client/client.py.jinja | 2 ++ pyproject.toml | 3 ++- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/codegen/__init__.py b/codegen/__init__.py index bf25298f0..c8bbed643 100644 --- a/codegen/__init__.py +++ b/codegen/__init__.py @@ -72,7 +72,11 @@ def build_rest_api(data: OpenAPIData, config: Config): for tag, endpoints in data.endpoints_by_tag.items(): logger.info(f"Building endpoints for tag {tag}...") tag_path = client_path / f"{tag}.py" - tag_path.write_text(client_template.render(tag=tag, endpoints=endpoints)) + tag_path.write_text( + client_template.render( + tag=tag, endpoints=endpoints, rest_api_version=config.rest_api_version + ) + ) logger.info(f"Successfully built endpoints for tag {tag}!") logger.info("Successfully built endpoints!") diff --git a/codegen/config.py b/codegen/config.py index fba7f9357..beb3233aa 100644 --- a/codegen/config.py +++ b/codegen/config.py @@ -6,6 +6,7 @@ class Config(BaseModel): rest_description_source: str + rest_api_version: str webhook_schema_source: str class_overrides: Dict[str, str] = {} field_overrides: Dict[str, str] = {} diff --git a/codegen/templates/client/_request.py.jinja b/codegen/templates/client/_request.py.jinja index a8cb37ce4..40d245c01 100644 --- a/codegen/templates/client/_request.py.jinja +++ b/codegen/templates/client/_request.py.jinja @@ -68,7 +68,9 @@ params=exclude_unset(params), {{ name }}=exclude_unset({{ name }}), {% endif %} {% if endpoint.header_params %} -headers=exclude_unset(headers), +headers=exclude_unset({**headers, **{'X-Github-Api-Version': self._REST_API_VERSION}}), +{% else %} +headers={'X-Github-Api-Version': self._REST_API_VERSION}, {% endif %} {% if endpoint.cookie_params %} cookies=exclude_unset(cookies), diff --git a/codegen/templates/client/client.py.jinja b/codegen/templates/client/client.py.jinja index 4e03c29d3..20bb2dc2e 100644 --- a/codegen/templates/client/client.py.jinja +++ b/codegen/templates/client/client.py.jinja @@ -25,6 +25,8 @@ if TYPE_CHECKING: from githubkit.response import Response class {{ pascal_case(tag) }}Client: + _REST_API_VERSION = "{{ rest_api_version }}" + def __init__(self, github: "GitHubCore"): self._github = github diff --git a/pyproject.toml b/pyproject.toml index 77e409119..12417a699 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,7 +56,8 @@ reportPrivateImportUsage = false reportShadowedImports = false [tool.codegen] -rest-description-source = "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/api.github.com.json" +rest-description-source = "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/api.github.com.2022-11-28.json" +rest-api-version = "2022-11-28" webhook-schema-source = "https://unpkg.com/@octokit/webhooks-schemas/schema.json" client-output = "githubkit/rest/" webhooks-output = "githubkit/webhooks/models.py" From e2d6d3e99ec81d86f1d9677abe2fadca139ed0b6 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Sat, 1 Apr 2023 11:54:31 +0800 Subject: [PATCH 2/3] :recycle: update header dict --- codegen/templates/client/_request.py.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codegen/templates/client/_request.py.jinja b/codegen/templates/client/_request.py.jinja index 40d245c01..2b5d4e753 100644 --- a/codegen/templates/client/_request.py.jinja +++ b/codegen/templates/client/_request.py.jinja @@ -68,9 +68,9 @@ params=exclude_unset(params), {{ name }}=exclude_unset({{ name }}), {% endif %} {% if endpoint.header_params %} -headers=exclude_unset({**headers, **{'X-Github-Api-Version': self._REST_API_VERSION}}), +headers=exclude_unset({**headers, "X-Github-Api-Version": self._REST_API_VERSION}), {% else %} -headers={'X-Github-Api-Version': self._REST_API_VERSION}, +headers={"X-Github-Api-Version": self._REST_API_VERSION}, {% endif %} {% if endpoint.cookie_params %} cookies=exclude_unset(cookies), From 1d9ab5c532af9e8a053205a81b058cd667399714 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Sat, 1 Apr 2023 11:56:30 +0800 Subject: [PATCH 3/3] :pencil2: fix header typo --- codegen/templates/client/_request.py.jinja | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codegen/templates/client/_request.py.jinja b/codegen/templates/client/_request.py.jinja index 2b5d4e753..b901b160e 100644 --- a/codegen/templates/client/_request.py.jinja +++ b/codegen/templates/client/_request.py.jinja @@ -68,9 +68,9 @@ params=exclude_unset(params), {{ name }}=exclude_unset({{ name }}), {% endif %} {% if endpoint.header_params %} -headers=exclude_unset({**headers, "X-Github-Api-Version": self._REST_API_VERSION}), +headers=exclude_unset({**headers, "X-GitHub-Api-Version": self._REST_API_VERSION}), {% else %} -headers={"X-Github-Api-Version": self._REST_API_VERSION}, +headers={"X-GitHub-Api-Version": self._REST_API_VERSION}, {% endif %} {% if endpoint.cookie_params %} cookies=exclude_unset(cookies),