diff --git a/pyproject.toml b/pyproject.toml index 183c8ac..c589954 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "loamy" -version = "0.0.8" +version = "0.0.9.dev" description = "This project allows you to execute a list of http operations asynchronously from within a synchronous context." authors = ["Zach Fuller "] readme = "README.md" diff --git a/src/loamy/session.py b/src/loamy/session.py index 2d5b0bc..688852d 100644 --- a/src/loamy/session.py +++ b/src/loamy/session.py @@ -1,5 +1,4 @@ import asyncio -import sys from json import JSONDecodeError from typing import Literal @@ -7,13 +6,9 @@ import msgspec from loguru import logger -logger.add( - sys.stdout, - format="{time} {level} {message}", - filter="loamy", - level="DEBUG", -) - +# Disable the logger. If a consuming app wishes to see loamy's logs, they can enable() it again. +logger.disable("loamy") +# https://loguru.readthedocs.io/en/stable/overview.html#suitable-for-scripts-and-libraries try: import uvloop @@ -25,6 +20,10 @@ class RequestMap(msgspec.Struct): + """ + Class containing information about a single HTTP request to be sent. + """ + url: str http_op: Literal["GET", "POST", "PUT", "PATCH", "OPTIONS", "DELETE"] body: dict | None = None @@ -33,6 +32,10 @@ class RequestMap(msgspec.Struct): class RequestResponse(msgspec.Struct): + """ + Class containing information about the result of an HTTP request. + """ + request_map: RequestMap status_code: int body: dict | None = None @@ -40,6 +43,10 @@ class RequestResponse(msgspec.Struct): class Clump: + """ + Class for sending multiple HTTP requests concurrently. + """ + def __init__(self, requests: list[RequestMap]) -> None: self._requestMaps: list[RequestMap] = requests logger.debug(f"Clump created with {len(self._requestMaps)} requests") @@ -119,7 +126,7 @@ async def _send_get_request( try: body = await resp.json() except (aiohttp.ContentTypeError, JSONDecodeError) as e: - logger.exception(f"Failed to decode JSON response from {resp.url}") + logger.error(f"Failed to decode JSON response from {resp.url}") error = e logger.trace("Attempting to read response as text") text: str = await resp.text() @@ -145,7 +152,7 @@ async def _send_post_request( try: body = await resp.json() except (aiohttp.ContentTypeError, JSONDecodeError) as e: - logger.exception(f"Failed to decode JSON response from {resp.url}") + logger.error(f"Failed to decode JSON response from {resp.url}") error = e logger.trace("Attempting to read response as text") text: str = await resp.text() @@ -171,7 +178,7 @@ async def _send_put_request( try: body = await resp.json() except (aiohttp.ContentTypeError, JSONDecodeError) as e: - logger.exception(f"Failed to decode JSON response from {resp.url}") + logger.error(f"Failed to decode JSON response from {resp.url}") error = e logger.trace("Attempting to read response as text") text: str = await resp.text() @@ -197,7 +204,7 @@ async def _send_patch_request( try: body = await resp.json() except (aiohttp.ContentTypeError, JSONDecodeError) as e: - logger.exception(f"Failed to decode JSON response from {resp.url}") + logger.error(f"Failed to decode JSON response from {resp.url}") error = e logger.trace("Attempting to read response as text") text: str = await resp.text() @@ -223,7 +230,7 @@ async def _send_options_request( try: body = await resp.json() except (aiohttp.ContentTypeError, JSONDecodeError) as e: - logger.exception(f"Failed to decode JSON response from {resp.url}") + logger.error(f"Failed to decode JSON response from {resp.url}") error = e logger.trace("Attempting to read response as text") text: str = await resp.text() @@ -249,7 +256,7 @@ async def _send_delete_request( try: body = await resp.json() except (aiohttp.ContentTypeError, JSONDecodeError) as e: - logger.exception(f"Failed to decode JSON response from {resp.url}") + logger.error(f"Failed to decode JSON response from {resp.url}") error = e logger.trace("Attempting to read response as text") text: str = await resp.text()