-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements almost all the routes using the new method to chain config…
…urations
- Loading branch information
Showing
25 changed files
with
1,029 additions
and
379 deletions.
There are no files selected for viewing
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
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,95 @@ | ||
import logging | ||
from contextlib import ExitStack | ||
from importlib.util import find_spec | ||
from pathlib import Path | ||
from types import TracebackType | ||
from typing import Dict | ||
from typing import Optional | ||
from typing import Type | ||
|
||
from httpx import Client | ||
from httpx import Response | ||
from httpx._types import RequestFiles | ||
|
||
from gotenberg_client.pdf_format import PdfAFormatOptions | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BaseRoute: | ||
def __init__(self, client: Client, api_route: str) -> None: | ||
self._client = client | ||
self._route = api_route | ||
self._stack = ExitStack() | ||
self._form_data: Dict[str, str] = {} | ||
self._file_map: Dict[str, Path] = {} | ||
|
||
def __enter__(self) -> "BaseRoute": | ||
self.reset() | ||
return self | ||
|
||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]], | ||
exc_val: Optional[BaseException], | ||
exc_tb: Optional[TracebackType], | ||
) -> None: | ||
self.reset() | ||
|
||
def reset(self) -> None: | ||
self._stack.close() | ||
self._form_data.clear() | ||
self._file_map.clear() | ||
|
||
def run(self) -> Response: | ||
resp = self._client.post(url=self._route, data=self._form_data, files=self.get_files()) | ||
resp.raise_for_status() | ||
return resp | ||
|
||
def get_files(self) -> RequestFiles: | ||
files = {} | ||
for filename in self._file_map: | ||
file_path = self._file_map[filename] | ||
# Gotenberg requires these to have the specific name | ||
filepath_name = filename if filename in {"index.html", "header.html", "footer.html"} else file_path.name | ||
|
||
mime_type = guess_mime_type(file_path) | ||
if mime_type is not None: | ||
files.update( | ||
{filepath_name: (filepath_name, self._stack.enter_context(file_path.open("rb")), mime_type)}, | ||
) | ||
else: # pragma: no cover | ||
files.update({filepath_name: (filepath_name, self._stack.enter_context(file_path.open("rb")))}) # type: ignore | ||
return files | ||
|
||
def _add_file_map(self, filepath: Path, name: Optional[str] = None) -> None: | ||
if name is None: | ||
name = filepath.name | ||
if name in self._file_map: # pragma: no cover | ||
logger.warning(f"{name} has already been provided, overwriting anyway") | ||
self._file_map[name] = filepath | ||
|
||
def pdf_format(self, pdf_format: PdfAFormatOptions) -> "BaseRoute": | ||
self._form_data.update(pdf_format.to_form()) | ||
return self | ||
|
||
|
||
class BaseApi: | ||
def __init__(self, client: Client) -> None: | ||
self._client = client | ||
|
||
|
||
def guess_mime_type_stdlib(url: Path) -> Optional[str]: | ||
import mimetypes | ||
|
||
mime_type, _ = mimetypes.guess_type(url) | ||
return mime_type | ||
|
||
|
||
def guess_mime_type_magic(url: Path) -> Optional[str]: | ||
import magic # type: ignore | ||
|
||
return magic.from_file(url, mime=True) # type: ignore | ||
|
||
|
||
guess_mime_type = guess_mime_type_magic if find_spec("magic") is not None else guess_mime_type_stdlib |
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.