Skip to content

Commit b3faaad

Browse files
netomiyanyongyu
andauthored
Add support for REST-API-VERSION based on the used schema (#24)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
1 parent 96e83c8 commit b3faaad

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

codegen/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ def build_rest_api(data: OpenAPIData, config: Config):
7272
for tag, endpoints in data.endpoints_by_tag.items():
7373
logger.info(f"Building endpoints for tag {tag}...")
7474
tag_path = client_path / f"{tag}.py"
75-
tag_path.write_text(client_template.render(tag=tag, endpoints=endpoints))
75+
tag_path.write_text(
76+
client_template.render(
77+
tag=tag, endpoints=endpoints, rest_api_version=config.rest_api_version
78+
)
79+
)
7680
logger.info(f"Successfully built endpoints for tag {tag}!")
7781
logger.info("Successfully built endpoints!")
7882

codegen/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class Config(BaseModel):
88
rest_description_source: str
9+
rest_api_version: str
910
webhook_schema_source: str
1011
class_overrides: Dict[str, str] = {}
1112
field_overrides: Dict[str, str] = {}

codegen/templates/client/_request.py.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ params=exclude_unset(params),
6868
{{ name }}=exclude_unset({{ name }}),
6969
{% endif %}
7070
{% if endpoint.header_params %}
71-
headers=exclude_unset(headers),
71+
headers=exclude_unset({**headers, "X-GitHub-Api-Version": self._REST_API_VERSION}),
72+
{% else %}
73+
headers={"X-GitHub-Api-Version": self._REST_API_VERSION},
7274
{% endif %}
7375
{% if endpoint.cookie_params %}
7476
cookies=exclude_unset(cookies),

codegen/templates/client/client.py.jinja

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ if TYPE_CHECKING:
2525
from githubkit.response import Response
2626

2727
class {{ pascal_case(tag) }}Client:
28+
_REST_API_VERSION = "{{ rest_api_version }}"
29+
2830
def __init__(self, github: "GitHubCore"):
2931
self._github = github
3032

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ reportPrivateImportUsage = false
5656
reportShadowedImports = false
5757

5858
[tool.codegen]
59-
rest-description-source = "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/api.github.com.json"
59+
rest-description-source = "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/api.github.com.2022-11-28.json"
60+
rest-api-version = "2022-11-28"
6061
webhook-schema-source = "https://unpkg.com/@octokit/webhooks-schemas/schema.json"
6162
client-output = "githubkit/rest/"
6263
webhooks-output = "githubkit/webhooks/models.py"

0 commit comments

Comments
 (0)