Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: monkeypatch httpx to have longer timeout #3

Merged
merged 2 commits into from
May 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 78 additions & 9 deletions graphpyshop/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import argparse
import logging
import functools
import logging.config
import os
import shutil
import time
from functools import lru_cache

import httpx
from dotenv import find_dotenv, load_dotenv
Expand All @@ -23,14 +26,59 @@ def new_client_init(self, *args, **kwargs):
logging.basicConfig(level=logging.INFO)


class TimedLog:
def __init__(self, name: str):
self.name = name

def __enter__(self):
self.start = time.time()

def __exit__(self, *args):
duration = (time.time() - self.start) * 1000
logging.info(f"{self.name} took {duration:.1f} ms")


@lru_cache
def monkey_patch_httpx():
"""Wrap all the httpx functions to have a default timeout of 30 (get, post, etc)"""
import httpx
from httpx import Timeout

new_timeout = Timeout(90)

original_get = httpx.get

@functools.wraps(httpx.get)
def get(*args, **kwargs):
if "timeout" not in kwargs:
kwargs["timeout"] = new_timeout
return original_get(*args, **kwargs)

httpx.get = get

original_post = httpx.post

@functools.wraps(httpx.post)
def post(*args, **kwargs):
if "timeout" not in kwargs:
kwargs["timeout"] = new_timeout
return original_post(*args, **kwargs)

httpx.post = post


def generate_client():
monkey_patch_httpx()
from ariadne_codegen.config import get_config_dict
from ariadne_codegen.main import client

logging.info("Starting generation of client")

load_dotenv(find_dotenv())
client(get_config_dict())
config_dict = get_config_dict()
print(config_dict)
with TimedLog("Client generation"):
client(config_dict)


def generate_queries():
Expand Down Expand Up @@ -77,6 +125,25 @@ def clean():
shutil.rmtree(path)


def configure_logging():
log_config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"}
},
"handlers": {
"default": {
"level": "INFO",
"formatter": "standard",
"class": "logging.StreamHandler",
}
},
"loggers": {"": {"handlers": ["default"], "level": "INFO", "propagate": True}},
}
logging.config.dictConfig(log_config)


def main():
parser = argparse.ArgumentParser(description="GraphPyShop CLI")
parser.add_argument(
Expand All @@ -85,13 +152,15 @@ def main():
help="Command to run",
)
args = parser.parse_args()

if args.command == "generate-client":
generate_client()
elif args.command == "generate-queries":
generate_queries()
elif args.command == "clean":
clean()
configure_logging()
monkey_patch_httpx()
with TimedLog(f"Command {args.command}"):
if args.command == "generate-client":
generate_client()
elif args.command == "generate-queries":
generate_queries()
elif args.command == "clean":
clean()


if __name__ == "__main__":
Expand Down
Loading