Skip to content

Commit

Permalink
Combine functions into one event hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kxtran committed Jul 9, 2024
1 parent 3217368 commit 1d5f704
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 96 deletions.
32 changes: 14 additions & 18 deletions log10/_httpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,22 +340,20 @@ class _RequestHooks:
def __init__(self):
logger.debug("LOG10: initializing request hooks")
self.event_hooks = {
"request": [self.get_completion_id, self.log_request],
"request": [self.log_request],
}
self.completion_id = ""
self.log_row = {}

def get_completion_id(self, request: httpx.Request):
logger.debug("LOG10: generating completion id")
self.completion_id = get_completion_id(request)

def log_request(self, request: httpx.Request):
logger.debug("LOG10: sending sync request")
if not self.completion_id:
logger.debug("LOG10: generating completion id")
completion_id = get_completion_id(request)
if not completion_id:
logger.debug("LOG10: completion id is not generated. Skipping")
return

logger.debug("LOG10: sending sync request")
self.log_row = _init_log_row(request)
_try_post_request(url=f"{base_url}/api/completions/{self.completion_id}", payload=self.log_row)
_try_post_request(url=f"{base_url}/api/completions/{completion_id}", payload=self.log_row)


class _AsyncRequestHooks:
Expand All @@ -369,23 +367,21 @@ class _AsyncRequestHooks:
def __init__(self):
logger.debug("LOG10: initializing async request hooks")
self.event_hooks = {
"request": [self.get_completion_id, self.log_request],
"request": [self.log_request],
}
self.completion_id = ""
self.log_row = {}

def get_completion_id(self, request: httpx.Request):
logger.debug("LOG10: generating completion id")
self.completion_id = get_completion_id(request)

async def log_request(self, request: httpx.Request):
logger.debug("LOG10: sending async request")
if not self.completion_id:
logger.debug("LOG10: generating completion id")
completion_id = get_completion_id(request)
if not completion_id:
logger.debug("LOG10: completion id is not generated. Skipping")
return

logger.debug("LOG10: sending async request")
self.log_row = _init_log_row(request)
asyncio.create_task(
_try_post_request_async(url=f"{base_url}/api/completions/{self.completion_id}", payload=self.log_row)
_try_post_request_async(url=f"{base_url}/api/completions/{completion_id}", payload=self.log_row)
)


Expand Down
78 changes: 0 additions & 78 deletions tests/test_request_hooks.py

This file was deleted.

0 comments on commit 1d5f704

Please sign in to comment.