Skip to content

Commit

Permalink
Merge pull request #38 from sandbox-pokhara/patch-method
Browse files Browse the repository at this point in the history
Add patch method support
  • Loading branch information
phalt authored Sep 25, 2024
2 parents 374eaa4 + f8edac7 commit 7a0ce8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions clientele/generators/basic/templates/http_py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def put(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Re
return client.put(parse_url(url), json=data, headers=headers)


def patch(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response:
"""Issue an HTTP PATCH request"""
return client.patch(parse_url(url), json=data, headers=headers)


def delete(url: str, headers: typing.Optional[dict] = None) -> httpx.Response:
"""Issue an HTTP DELETE request"""
return client.delete(parse_url(url), headers=headers)
6 changes: 4 additions & 2 deletions clientele/generators/standard/generators/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(
delete="get_method.jinja2",
post="post_method.jinja2",
put="post_method.jinja2",
patch="post_method.jinja2",
)

def generate_paths(self) -> None:
Expand All @@ -64,6 +65,7 @@ def generate_paths(self) -> None:
console.log(f"Generated {self.results['get']} GET methods...")
console.log(f"Generated {self.results['post']} POST methods...")
console.log(f"Generated {self.results['put']} PUT methods...")
console.log(f"Generated {self.results['patch']} PATCH methods...")
console.log(f"Generated {self.results['delete']} DELETE methods...")

def generate_parameters(self, parameters: list[dict], additional_parameters: list[dict]) -> ParametersResponse:
Expand Down Expand Up @@ -197,9 +199,9 @@ def generate_function(
api_url = url + utils.create_query_args(list(query_args.keys()))
else:
api_url = url
if method in ["post", "put"] and not operation.get("requestBody"):
if method in ["post", "put", "patch"] and not operation.get("requestBody"):
data_class_name = "None"
elif method in ["post", "put"]:
elif method in ["post", "put", "patch"]:
data_class_name = self.generate_input_types(operation.get("requestBody", {}))
else:
data_class_name = None
Expand Down
8 changes: 8 additions & 0 deletions clientele/generators/standard/templates/sync_methods.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def put(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Re
return client.put(parse_url(url), json=json_data, headers=client_headers)


def patch(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response:
"""Issue an HTTP PATCH request"""
if headers:
client_headers.update(headers)
json_data = json.loads(json.dumps(data, default=json_serializer))
return client.patch(parse_url(url), json=json_data, headers=client_headers)


def delete(url: str, headers: typing.Optional[dict] = None) -> httpx.Response:
"""Issue an HTTP DELETE request"""
if headers:
Expand Down

0 comments on commit 7a0ce8b

Please sign in to comment.