Skip to content

Commit

Permalink
Log Output Disabled by Default (#36)
Browse files Browse the repository at this point in the history
* added docstrings and disabled logger

* Updated version to 0.0.9
  • Loading branch information
fullerzz authored May 1, 2024
1 parent a9890e7 commit 1682fe4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
38 changes: 19 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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"
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,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
Expand All @@ -41,6 +44,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")
Expand Down Expand Up @@ -120,7 +127,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 Down Expand Up @@ -150,7 +157,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 Down Expand Up @@ -180,7 +187,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 Down Expand Up @@ -210,7 +217,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 Down Expand Up @@ -240,7 +247,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 Down Expand Up @@ -270,7 +277,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 1682fe4

Please sign in to comment.