Skip to content

Commit

Permalink
✨ Feature: add option for disable rest api input body validation (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 10, 2024
1 parent e8f0bfb commit 8f1ffbb
Show file tree
Hide file tree
Showing 60 changed files with 3,647 additions and 2,313 deletions.
3 changes: 2 additions & 1 deletion codegen/templates/rest/_request.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ cookies = {
{{ name }} = data
{% endif %}
{% if request_body.type in ["json", "form"] %}
{{ name }} = type_validate_python({{ request_body.body_schema.get_type_string() }}, {{ name }})
if self._github.config.rest_api_body_validation:
{{ name }} = type_validate_python({{ request_body.body_schema.get_type_string() }}, {{ name }})
{{ name }} = model_dump({{ name }}) if isinstance({{ name }}, BaseModel) else {{ name }}
{% endif %}
{% endmacro %}
Expand Down
6 changes: 6 additions & 0 deletions docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ github = GitHub(
timeout=None,
http_cache=True,
auto_retry=True,
rest_api_body_validation=True,
)
```

Expand All @@ -32,6 +33,7 @@ config = Config(
timeout=httpx.Timeout(None),
http_cache=True,
auto_retry=RETRY_DEFAULT,
rest_api_body_validation=True,
)

github = GitHub(config=config)
Expand Down Expand Up @@ -70,3 +72,7 @@ The `http_cache` option enables the http caching feature powered by [Hishel](htt
### `auto_retry`

The `auto_retry` option enables request retrying when rate limit exceeded and server error encountered. See [Auto Retry](./auto-retry.md) for more infomation.

### `rest_api_body_validation`

The `rest_api_body_validation` option is used to enable or disable the rest API request body validation. By default, githubkit validates the input data against the GitHub API schema. If you do not want to validate the input data, you can set this option to `False`.
3 changes: 3 additions & 0 deletions githubkit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Config:
timeout: httpx.Timeout
http_cache: bool
auto_retry: Optional[RetryDecisionFunc]
rest_api_body_validation: bool

def dict(self) -> Dict[str, Any]:
return asdict(self)
Expand Down Expand Up @@ -83,6 +84,7 @@ def get_config(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
) -> Config:
return Config(
build_base_url(base_url),
Expand All @@ -92,4 +94,5 @@ def get_config(
build_timeout(timeout),
http_cache,
build_auto_retry(auto_retry),
rest_api_body_validation,
)
5 changes: 5 additions & 0 deletions githubkit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
): ...

# token auth without config
Expand All @@ -109,6 +110,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
): ...

# other auth strategies without config
Expand All @@ -125,6 +127,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
): ...

def __init__(
Expand All @@ -140,6 +143,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
):
auth = auth or UnauthAuthStrategy() # type: ignore
self.auth: A = ( # type: ignore
Expand All @@ -155,6 +159,7 @@ def __init__(
timeout,
http_cache,
auto_retry,
rest_api_body_validation,
)

self.__sync_client: ContextVar[Optional[httpx.Client]] = ContextVar(
Expand Down
3 changes: 3 additions & 0 deletions githubkit/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
): ...

# token auth without config
Expand All @@ -100,6 +101,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
): ...

# other auth strategies without config
Expand All @@ -116,6 +118,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
rest_api_body_validation: bool = True,
): ...

def __init__(self, *args, **kwargs): ...
Expand Down
Loading

0 comments on commit 8f1ffbb

Please sign in to comment.