-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from phalt/0.7.0
0.7.0
- Loading branch information
Showing
60 changed files
with
1,083 additions
and
635 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generators | ||
|
||
In the future, this will be the directory for all the possible generators that clientele supports. | ||
|
||
Copy the basic template if you want to start your own. | ||
|
||
## Standard | ||
|
||
The standard generator | ||
|
||
## Basic | ||
|
||
A basic client with a file structure and not much else. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from os import remove | ||
from os.path import exists | ||
|
||
from clientele import settings, utils | ||
from clientele.generators.basic import writer | ||
|
||
|
||
class BasicGenerator: | ||
""" | ||
Generates a "basic" HTTP client, which is just a file structure | ||
and some useful imports. | ||
This generator can be used as a template for future generators. | ||
It is also a great way to generate a file structure for consistent HTTP API clients | ||
that are not OpenAPI but you want to keep the same file structure. | ||
""" | ||
|
||
def __init__(self, output_dir: str) -> None: | ||
self.output_dir = output_dir | ||
|
||
self.file_name_writer_tuple = ( | ||
("config.py", "config_py.jinja2", writer.write_to_config), | ||
("client.py", "client_py.jinja2", writer.write_to_client), | ||
("http.py", "http_py.jinja2", writer.write_to_http), | ||
("schemas.py", "schemas_py.jinja2", writer.write_to_schemas), | ||
) | ||
|
||
def generate(self) -> None: | ||
client_project_directory_path = utils.get_client_project_directory_path( | ||
output_dir=self.output_dir | ||
) | ||
if exists(f"{self.output_dir}/MANIFEST.md"): | ||
remove(f"{self.output_dir}/MANIFEST.md") | ||
manifest_template = writer.templates.get_template("manifest.jinja2") | ||
manifest_content = manifest_template.render( | ||
command=f"-o {self.output_dir}", clientele_version=settings.VERSION | ||
) | ||
writer.write_to_manifest( | ||
content=manifest_content + "\n", output_dir=self.output_dir | ||
) | ||
writer.write_to_init(output_dir=self.output_dir) | ||
for ( | ||
client_file, | ||
client_template_file, | ||
write_func, | ||
) in self.file_name_writer_tuple: | ||
if exists(f"{self.output_dir}/{client_file}"): | ||
remove(f"{self.output_dir}/{client_file}") | ||
template = writer.templates.get_template(client_template_file) | ||
content = template.render( | ||
client_project_directory_path=client_project_directory_path, | ||
) | ||
write_func(content, output_dir=self.output_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" | ||
API Client functions. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import typing # noqa | ||
|
||
from {{client_project_directory_path}} import http, schemas # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
API Client configuration. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
HTTP layer management. | ||
""" | ||
|
||
import typing | ||
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse | ||
import httpx # noqa | ||
|
||
from {{client_project_directory_path}} import config as c # noqa | ||
|
||
|
||
class APIException(Exception): | ||
"""Could not match API response to return type of this function""" | ||
|
||
reason: str | ||
response: httpx.Response | ||
|
||
def __init__(self, response: httpx.Response, reason: str, *args: object) -> None: | ||
self.response = response | ||
self.reason = reason | ||
super().__init__(*args) | ||
|
||
|
||
def parse_url(url: str) -> str: | ||
""" | ||
Returns the full URL from a string. | ||
|
||
Will filter out any optional query parameters if they are None. | ||
""" | ||
api_url = f"{c.api_base_url()}{url}" | ||
url_parts = urlparse(url=api_url) | ||
# Filter out "None" optional query parameters | ||
filtered_query_params = { | ||
k: v for k, v in parse_qs(url_parts.query).items() if v[0] not in ["None", ""] | ||
} | ||
filtered_query_string = urlencode(filtered_query_params, doseq=True) | ||
return urlunparse( | ||
( | ||
url_parts.scheme, | ||
url_parts.netloc, | ||
url_parts.path, | ||
url_parts.params, | ||
filtered_query_string, | ||
url_parts.fragment, | ||
) | ||
) | ||
|
||
client = httpx.Client() | ||
|
||
|
||
def get(url: str, headers: typing.Optional[dict] = None) -> httpx.Response: | ||
"""Issue an HTTP GET request""" | ||
return client.get(parse_url(url), headers=headers) | ||
|
||
|
||
def post(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: | ||
"""Issue an HTTP POST request""" | ||
return client.post(parse_url(url), json=data, headers=headers) | ||
|
||
|
||
def put(url: str, data: dict, headers: typing.Optional[dict] = None) -> httpx.Response: | ||
"""Issue an HTTP PUT request""" | ||
return client.put(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.