Skip to content

Commit

Permalink
added docstrings and disabled logger
Browse files Browse the repository at this point in the history
  • Loading branch information
fullerzz committed Apr 28, 2024
1 parent 89e897c commit c46aad4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down
35 changes: 21 additions & 14 deletions src/loamy/session.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import asyncio
import sys
from json import JSONDecodeError
from typing import Literal

import aiohttp
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

Expand All @@ -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
Expand All @@ -33,13 +32,21 @@ 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
error: BaseException | None = None


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")
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit c46aad4

Please sign in to comment.