commit 9ba78973b0074f7d6d8f6e8adb7814cc57e42828 Author: Ēriks Karls Date: Sun Mar 10 08:51:52 2024 +0200 Init v1.0.1 diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..18b706d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# http://editorconfig.org + +root = true + +[*] +max_line_length = 160 +indent_style = space +indent_size = 4 +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{json,yml,toml}] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false + +[Makefile] +indent_style = tab diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..2643256 --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +extend-ignore = E203, E704, W605, E701 +exclude = .git,__pycache__,venv,migrations +max-line-length = 160 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b570c68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,368 @@ +.idea/ +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf +.idea/**/aws.xml +.idea/**/contentModel.xml +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml +.idea/**/gradle.xml +.idea/**/libraries +.idea/**/mongoSettings.xml +.idea/httpRequests +.idea/replstate.xml +.idea/sonarlint/ +.idea/caches/build_file_checksums.ser +*.iws + +.idea_modules/ + +cmake-build-*/ +out/ + +atlassian-ide-plugin.xml +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +.Python +[Bb]in +[Ii]nclude +[Ll]ib +[Ll]ib64 +[Ll]ocal +[Ss]cripts +pyvenv.cfg +.venv +pip-selfcheck.json + +### VisualStudioCode template +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff + +# AWS User-specific + +# Generated files + +# Sensitive or high-churn files + +# Gradle + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake + +# Mongo Explorer plugin + +# File-based project format + +# IntelliJ + +# mpeltonen/sbt-idea plugin + +# JIRA plugin + +# Cursive Clojure plugin + +# SonarLint plugin + +# Crashlytics plugin (for Android Studio and IntelliJ) + +# Editor-based Rest Client + +# Android studio 3.1+ serialized cache file + +### PyCharm template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff + +# AWS User-specific + +# Generated files + +# Sensitive or high-churn files + +# Gradle + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake + +# Mongo Explorer plugin + +# File-based project format + +# IntelliJ + +# mpeltonen/sbt-idea plugin + +# JIRA plugin + +# Cursive Clojure plugin + +# SonarLint plugin + +# Crashlytics plugin (for Android Studio and IntelliJ) + +# Editor-based Rest Client + +# Android studio 3.1+ serialized cache file + +### VirtualEnv template +# Virtualenv +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ + +### Intellij+all template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff + +# AWS User-specific + +# Generated files + +# Sensitive or high-churn files + +# Gradle + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake + +# Mongo Explorer plugin + +# File-based project format + +# IntelliJ + +# mpeltonen/sbt-idea plugin + +# JIRA plugin + +# Cursive Clojure plugin + +# SonarLint plugin + +# Crashlytics plugin (for Android Studio and IntelliJ) + +# Editor-based Rest Client + +# Android studio 3.1+ serialized cache file + +### Django template +*.log +*.pot +*.pyc +__pycache__/ +local_settings.py +db.sqlite** +media/** +logs/** + +# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/ +# in your Git repository. Update and uncomment the following line accordingly. +# /staticfiles/ + +### Python template +# Byte-compiled / optimized / DLL files +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo + +# Django stuff: + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json +.pyre/ +.pytype/ +cython_debug/ + +instance/* +!instance/.gitignore diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..2837e51 --- /dev/null +++ b/LICENCE @@ -0,0 +1,7 @@ +Copyright 2024 "Eriks Karls" + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b6c19c7 --- /dev/null +++ b/Makefile @@ -0,0 +1,65 @@ +.PHONY: clean clean/build clean/pyc help lint lint/flake8 lint/black lint/isort lint/type test +.DEFAULT_GOAL := help + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef +export PRINT_HELP_PYSCRIPT + + +help: + @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) + +clean: clean/build clean/pyc ## remove all build, test, coverage and Python artifacts + +clean/build: ## remove build artifacts + rm -fr build/ + rm -fr dist/ + rm -fr .eggs/ + find . -name '*.egg-info' -exec rm -fr {} + + find . -name '*.egg' -exec rm -f {} + + +clean/pyc: ## remove Python file artifacts + find . -name '*.pyc' -exec rm -f {} + + find . -name '*.pyo' -exec rm -f {} + + find . -name '*~' -exec rm -f {} + + find . -name '__pycache__' -exec rm -fr {} + + find . -name '.pytest_cache' -exec rm -fr {} + + +build/source: + python -m build --sdist + +build/wheel: + python -m build --wheel + +patch: + bump-my-version bump patch + +minor: + bump-my-version bump minor + +major: + bump-my-version bump major + +release: clean build/source build/wheel + twine upload dist/* + +lint/flake8: ## check style with flake8 + flake8 src/api_client +lint/black: ## check style with black + black src/api_client +lint/isort: ## check imports with isort + isort src/api_client +lint/type: ## check imports with isort + mypy src/api_client + +lint: lint/isort lint/black lint/flake8 lint/type ## check style + +test: ## run tests quickly with the default Python + pytest -v diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad295ea --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# Example code +```python +# client.py +from api_client import BaseAPIClient + + +class LocalClient(BaseAPIClient): + name = "local" + _base_url = "https://obligari.serveo.net/ping/local" + + def __init__(self, nonce=None): + super().__init__(nonce) + self._session.headers.update( + {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:123.0) Gecko/20100101 Firefox/123.0"} + ) + + def send_post_ping(self, var1: str, var2: int) -> bool: + res = self.post("/some-post", json={"variable_one": var1, "second_variable": var2}) + return res.json().get("status") + + def send_put_ping(self, var1: str, var2: int) -> bool: + res = self.put("/some-put", data={"variable_one": var1, "second_variable": var2}) + return res.json().get("status") + + def send_get_ping(self, var1: str, var2: int) -> bool: + res = self.get("/some-get", params={"variable_one": var1, "second_variable": var2}) + return res.json().get("status") + + def send_patch_ping(self, var1: str, var2: int) -> bool: + res = self.put("/some-patch", data=(("variable_one", var1), ("variable_one", var2))) + return res.json().get("status") + + def send_trace_ping(self, var1: str, var2: int) -> bool: + res = self.trace("/some-trace", params=(("variable_one", var1), ("variable_one", var2))) + return res.json().get("status") + + +``` + +```python +# main.py +from .client import LocalClient + + +client = LocalClient() +client.send_post_ping("asd", 123) +client.send_put_ping("asd", 123) +client.send_get_ping("asd", 123) +client.send_patch_ping("asd", 123) +client.send_trace_ping("asd", 123) +``` + +## Log output +### Simple +```text +[14d709e02c0c] Preparing POST request to "https://obligari.serveo.net/ping/local/some-post" +[14d709e02c0c] Sending request with payload=b'{"variable_one": "asd", "second_variable": 123}' +[14d709e02c0c] Response response.status_code=200 str_repr_content='{"status":true,"request_id":62}' +[14d709e02c0c] Preparing GET request to "https://obligari.serveo.net/ping/local/some-get" +[14d709e02c0c] Sending request with payload=None +[14d709e02c0c] Response response.status_code=200 str_repr_content='{"status":true,"request_id":63}' +``` +### Structured +```json +{"app": "dev", "level": "DEBUG", "name": "APIClient", "date_time": "2024-03-09 22:59:24", "location": "api_client/client.py:_request:71", "message": "[cfbdadc56f53] Preparing POST request to \"https://obligari.serveo.net/ping/local/some-post\"", "extra_data": {"hooks": {"response": []}, "method": "POST", "url": "https://obligari.serveo.net/ping/local/some-post", "headers": {}, "files": [], "data": [], "json": {"variable_one": "asd", "second_variable": 123}, "params": {}, "auth": null, "cookies": null}} +{"app": "dev", "level": "INFO", "name": "APIClient", "date_time": "2024-03-09 22:59:24", "location": "api_client/client.py:_request:74", "message": "[cfbdadc56f53] Sending request with payload=b'{\"variable_one\": \"asd\", \"second_variable\": 123}'", "extra_data": {"payload": "{\"variable_one\": \"asd\", \"second_variable\": 123}"}} +{"app": "dev", "level": "INFO", "name": "APIClient", "date_time": "2024-03-09 22:59:25", "location": "api_client/client.py:_request:81", "message": "[cfbdadc56f53] Response response.status_code=200 str_repr_content='{\"status\":true,\"request_id\":72}'", "extra_data": {"status_code": 200, "content": "{\"status\":true,\"request_id\":72}"}} +{"app": "dev", "level": "DEBUG", "name": "APIClient", "date_time": "2024-03-09 22:59:25", "location": "api_client/client.py:_request:71", "message": "[cfbdadc56f53] Preparing GET request to \"https://obligari.serveo.net/ping/local/some-get\"", "extra_data": {"hooks": {"response": []}, "method": "GET", "url": "https://obligari.serveo.net/ping/local/some-get", "headers": {}, "files": [], "data": [], "json": null, "params": {"variable_one": "asd", "second_variable": 123}, "auth": null, "cookies": null}} +{"app": "dev", "level": "INFO", "name": "APIClient", "date_time": "2024-03-09 22:59:25", "location": "api_client/client.py:_request:74", "message": "[cfbdadc56f53] Sending request with payload=None", "extra_data": {"payload": "{}"}} +{"app": "dev", "level": "INFO", "name": "APIClient", "date_time": "2024-03-09 22:59:25", "location": "api_client/client.py:_request:81", "message": "[cfbdadc56f53] Response response.status_code=200 str_repr_content='{\"status\":true,\"request_id\":74}'", "extra_data": {"status_code": 200, "content": "{\"status\":true,\"request_id\":73}"}} +``` diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e518be3 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,85 @@ +[build-system] +requires = ["setuptools>=61.0"] +build-backend = "setuptools.build_meta" + +[project] +name = "sdlv_api_client" +dynamic = ["version"] +description = "API Client Base class" +readme = "README.md" +requires-python = "~=3.10" +license = { file = "LICENSE" } +keywords = ["api", "client", "api-client"] +authors = [ + { name = "Eriks Karls", email = "git@72.lv" }, +] +classifiers = [ + "Development Status :: 5 - Stable", + "Intended Audience :: Developers", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3 :: Only", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", +] +dependencies = [ + "requests~=2.31", + "pydantic~=2.6", +] + + +[project.urls] +"Homepage" = "https://pypi.org/project/sdlv-api-client/" +"Bug Reports" = "https://git.72.lv/eriks/api_client/issues" +"Funding" = "https://donate.pypi.org" +"Source" = "https://git.72.lv/eriks/api_client" + + +[tool.setuptools.dynamic] +version = { attr = "api_client.__version__" } + + +[tool.bumpversion] +current_version = "1.0.1" +commit = true +tag = true +tag_name = "v{new_version}" +tag_message = "Bump version: {current_version} → {new_version}" +allow_dirty = false +parse = "(?P\\d+)\\.(?P\\d+)\\.(?P\\d+)" +serialize = ["{major}.{minor}.{patch}"] +message = "Bump version: {current_version} → {new_version}" + + +[[tool.bumpversion.files]] +filename = "src/api_client/__init__.py" + + +[tool.black] +line-length = 160 +target-version = ['py311'] +include = '\.pyi?$' +extend-exclude = '''( + | .git/* +)''' +workers = 4 + + +[tool.isort] +profile = "black" +line_length = 160 +skip = ["migrations", "env", "venv", ".venv", ".git", "media"] + + +[tool.mypy] +python_version = "3.11" +exclude = ['^\.?venv/',] +plugins = ["pydantic.mypy"] +warn_unused_configs = true +disallow_untyped_defs = true +implicit_optional = true +warn_redundant_casts = true +warn_no_return = false +ignore_missing_imports = false diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d2b8bf3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,24 @@ +## Requirements for development of library +# Base requirements +requests==2.31.0 +pydantic==2.6.3 + +# Code style +black==24.2 +flake8==7.0 +isort==5.13.2 + +# Type check +mypy==1.9 +types-requests==2.31.0.20240218 + +# Version and package support +twine==5.0 +bump-my-version==0.18.3 +build==1.1.1 +pur==7.3.1 +setuptools==69.1.1 + +# Testing and debugging +pytest==8.1.1 +ipython==8.22.2 diff --git a/sdlv_api_client.iml b/sdlv_api_client.iml new file mode 100644 index 0000000..bf01f66 --- /dev/null +++ b/sdlv_api_client.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/api_client/__init__.py b/src/api_client/__init__.py new file mode 100644 index 0000000..d0020b4 --- /dev/null +++ b/src/api_client/__init__.py @@ -0,0 +1,4 @@ +from .client import BaseAPIClient + +__version__ = "1.0.1" +__all__ = ["BaseAPIClient"] diff --git a/src/api_client/client.py b/src/api_client/client.py new file mode 100644 index 0000000..ede9efc --- /dev/null +++ b/src/api_client/client.py @@ -0,0 +1,107 @@ +import logging +from abc import ABC +from typing import Any + +import requests +from pydantic import HttpUrl + +from .utils import get_logger +from .utils.logs import get_nonce, shortify_log_value + + +class BaseAPIClient(ABC): + base_url: HttpUrl + _session: requests.Session + _nonce: str + _logger: logging.Logger + + def __init__(self, nonce: str = ""): + self._nonce = nonce or get_nonce() + self._session = requests.Session() + self._logger = get_logger("APIClient") + + def get(self, endpoint: str, *, params: Any = None, **kwargs: Any) -> requests.Response: + return self._request("GET", endpoint, params=params, **kwargs) + + def post(self, endpoint: str, *, json: Any = None, data: Any = None, **kwargs: Any) -> requests.Response: + return self._request("POST", endpoint, json=json, data=data, **kwargs) + + def put(self, endpoint: str, *, json: Any = None, data: Any = None, **kwargs: Any) -> requests.Response: + return self._request("PUT", endpoint, json=json, data=data, **kwargs) + + def patch(self, endpoint: str, *, json: Any = None, data: Any = None, **kwargs: Any) -> requests.Response: + return self._request("PATCH", endpoint, json=json, data=data, **kwargs) + + def delete(self, endpoint: str, *, params: Any = None, **kwargs: Any) -> requests.Response: + return self._request("DELETE", endpoint, params=params, **kwargs) + + def head(self, endpoint: str, *, params: Any = None, **kwargs: Any) -> requests.Response: + return self._request("HEAD", endpoint, params=params, **kwargs) + + def options(self, endpoint: str, *, params: Any = None, **kwargs: Any) -> requests.Response: + return self._request("OPTIONS", endpoint, params=params, **kwargs) + + def trace(self, endpoint: str, *, params: Any = None, **kwargs: Any) -> requests.Response: + return self._request("TRACE", endpoint, params=params, **kwargs) + + def connect(self, endpoint: str, *, params: Any = None, **kwargs: Any) -> requests.Response: + return self._request("CONNECT", endpoint, params=params, **kwargs) + + def _make_full_url(self, endpoint: str) -> str: + return f"{self.base_url}{endpoint}" + + def _request(self, method: str, endpoint: str, **kwargs: Any) -> requests.Response: + full_url = self._make_full_url(endpoint) + + # Create the Request. + req = requests.Request( + method=method.upper(), + url=full_url, + headers=kwargs.get("headers"), + files=kwargs.get("files"), + data=kwargs.get("data"), + json=kwargs.get("json"), + params=kwargs.get("params"), + auth=kwargs.get("auth"), + cookies=kwargs.get("cookies"), + hooks=kwargs.get("hooks"), + ) + self.prepare_authentication(req) + self._debug(f'Preparing {req.method} request to "{req.url}"', extra=req.__dict__) + prepared_request: requests.PreparedRequest = self._session.prepare_request(req) + + self._info( + f"Sending request with payload={prepared_request.body!r}", + extra={"payload": shortify_log_value(kwargs.get("json", kwargs.get("data", {})))}, + ) + response = self._session.send(prepared_request) + + str_repr_content = response.content.decode("utf8")[:500] + self._info(f"Response {response.status_code=} {str_repr_content=}", extra={"status_code": response.status_code, "content": str_repr_content}) + + response.raise_for_status() + return response + + def prepare_authentication(self, request: requests.Request) -> None: + """Do auth setup in-place""" + pass + + def _info(self, msg: str, *args: Any, **kwargs: Any) -> None: + self._log("INFO", f"[{self._nonce}] {msg}", *args, **kwargs) + + def _debug(self, msg: str, *args: Any, **kwargs: Any) -> None: + self._log("DEBUG", f"[{self._nonce}] {msg}", *args, **kwargs) + + def _warn(self, msg: str, *args: Any, **kwargs: Any) -> None: + self._log("WARNING", f"[{self._nonce}] {msg}", *args, **kwargs) + + def _error(self, msg: str, *args: Any, **kwargs: Any) -> None: + self._log("ERROR", f"[{self._nonce}] {msg}", *args, **kwargs) + + def _critical(self, msg: str, *args: Any, **kwargs: Any) -> None: + self._log("CRITICAL", f"[{self._nonce}] {msg}", *args, **kwargs) + + def _log(self, level: str, msg: object, *args: object, stacklevel: int = 3, extra: dict | None = None, **kwargs: Any) -> None: + extra = extra or {} + extra.update(nonce=self._nonce) + self._logger.log(logging.getLevelNamesMapping()[level], msg, *args, stacklevel=stacklevel, extra=extra, **kwargs) diff --git a/src/api_client/utils/__init__.py b/src/api_client/utils/__init__.py new file mode 100644 index 0000000..5b0c658 --- /dev/null +++ b/src/api_client/utils/__init__.py @@ -0,0 +1,4 @@ +from .logs import APIClientLogJSONFormatter, get_logger, shortify_log_value +from .parse import APIClientJSONEncoder + +__all__ = ["get_logger", "APIClientLogJSONFormatter", "shortify_log_value", "APIClientJSONEncoder"] diff --git a/src/api_client/utils/logs.py b/src/api_client/utils/logs.py new file mode 100644 index 0000000..c4c58a1 --- /dev/null +++ b/src/api_client/utils/logs.py @@ -0,0 +1,79 @@ +import dataclasses +import datetime +import json +import logging +import os +from dataclasses import asdict +from logging import LogRecord +from typing import Any +from uuid import uuid4 + +from .parse import APIClientJSONEncoder + + +def get_logger(name: str = "service") -> logging.Logger: + return logging.getLogger(name) + + +def _shorten_dict_keys(dct: dict) -> dict: + res = {} + for k, v in dct.items(): + if isinstance(v, str) and len(v) > 64: + v = f"{v[:30]}...{v[-30:]}" + elif isinstance(v, dict): + v = _shorten_dict_keys(v) + res[k] = v + return res + + +def shortify_log_value(dct: Any) -> str: + if dataclasses.is_dataclass(dct): + dct = asdict(dct) + if not isinstance(dct, dict): + return str(dct) + return json.dumps(_shorten_dict_keys(dct), cls=APIClientJSONEncoder) + + +def get_nonce() -> str: + return uuid4().hex[:12] + + +class APIClientLogJSONFormatter(logging.Formatter): + default_time_format = "%Y-%m-%d %H:%M:%S" + + def format(self, record: LogRecord) -> str: + record_default_keys = [ + "name", + "msg", + "args", + "levelname", + "levelno", + "pathname", + "exc_info", + "filename", + "lineno", + "funcName", + "created", + "msecs", + "relativeCreated", + "thread", + "threadName", + "processName", + "process", + "message", + "asctime", + "module", + "exc_text", + "stack_info", + ] + structured_data = dict( + app=os.environ.get("APP_NAME", "dev"), + level=record.levelname, + name=record.name, + date_time=datetime.datetime.fromtimestamp(record.created).strftime(self.default_time_format), + location=f"{record.pathname or record.filename}:{record.funcName}:{record.lineno}", + message=record.getMessage(), + extra_data={k: record.__dict__[k] for k in record.__dict__.keys() if k not in record_default_keys}, + ) + + return json.dumps(structured_data, cls=APIClientJSONEncoder) diff --git a/src/api_client/utils/parse.py b/src/api_client/utils/parse.py new file mode 100644 index 0000000..1ebc40f --- /dev/null +++ b/src/api_client/utils/parse.py @@ -0,0 +1,36 @@ +import datetime +import decimal +import json +from typing import Any + +from pydantic import BaseModel + +__all__ = [ + "APIClientJSONEncoder", +] + + +class APIClientJSONEncoder(json.JSONEncoder): + def default(self, obj: Any) -> Any: + try: + if isinstance(obj, decimal.Decimal): + return str(obj) + if isinstance(obj, (datetime.datetime, datetime.date)): + return obj.isoformat() + if isinstance(obj, BaseModel): + return obj.model_dump_json() + if isinstance(obj, datetime.timedelta): + return dict(__type__="timedelta", total_seconds=obj.total_seconds()) + if hasattr(obj, "as_dict"): + if callable(getattr(obj, "as_dict")): + return super().default(obj.as_dict()) + return super().default(obj.as_dict) + if isinstance(obj, set): + return list(obj) + if hasattr(obj, "__dict__"): + return obj.__dict__ + return super().default(obj) + except TypeError as exc: + if "not JSON serializable" in str(exc): + return str(obj) + raise exc