Skip to content

Commit

Permalink
chore(routers): always use verbose match names (#39)
Browse files Browse the repository at this point in the history
In the URL regex matches, we should always prefer verbose names like `run_id` to just `id`.
  • Loading branch information
mharrisb1 authored May 21, 2024
1 parent 6807cc6 commit b3560ed
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 56 deletions.
7 changes: 3 additions & 4 deletions examples/test_router_usage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from typing import Any

import httpx
import openai
Expand All @@ -14,10 +13,9 @@ def polled_get_run_responses(
request: Request,
route: Route,
state_store: StateStore,
**kwargs: Any,
thread_id: str,
run_id: str,
) -> Response:
# RESPX will inject `id` (for run ID) and `thread_id` into the kwargs
run_id = kwargs["id"]
run = state_store.beta.threads.runs.get(run_id)
assert run
if route.call_count < 4:
Expand All @@ -29,6 +27,7 @@ def polled_get_run_responses(
run = merge_run_with_partial(
run,
{
"thread_id": thread_id,
"status": "requires_action",
"required_action": {
"type": "submit_tool_outputs",
Expand Down
28 changes: 18 additions & 10 deletions src/openai_responses/_routes/assistants.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def _build(
class AssistantRetrieveRoute(StatefulRoute[Assistant, PartialAssistant]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.get(url__regex=r"/assistants/(?P<id>[a-zA-Z0-9\_]+)"),
route=router.get(
url__regex=r"/assistants/(?P<assistant_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
)
Expand All @@ -118,8 +120,8 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
found = self._state.beta.assistants.get(id)
assistant_id = kwargs["assistant_id"]
found = self._state.beta.assistants.get(assistant_id)
if not found:
return httpx.Response(404)

Expand All @@ -133,7 +135,9 @@ def _build(partial: PartialAssistant, request: httpx.Request) -> Assistant:
class AssistantUpdateRoute(StatefulRoute[Assistant, PartialAssistant]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.post(url__regex=r"/assistants/(?P<id>[a-zA-Z0-9\_]+)"),
route=router.post(
url__regex=r"/assistants/(?P<assistant_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
)
Expand All @@ -146,8 +150,8 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
found = self._state.beta.assistants.get(id)
assistant_id = kwargs["assistant_id"]
found = self._state.beta.assistants.get(assistant_id)
if not found:
return httpx.Response(404)

Expand All @@ -166,7 +170,9 @@ def _build(partial: PartialAssistant, request: httpx.Request) -> Assistant:
class AssistantDeleteRoute(StatefulRoute[AssistantDeleted, PartialAssistantDeleted]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.delete(url__regex=r"/assistants/(?P<id>[a-zA-Z0-9\_]+)"),
route=router.delete(
url__regex=r"/assistants/(?P<assistant_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
)
Expand All @@ -179,12 +185,14 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
deleted = self._state.beta.assistants.delete(id)
assistant_id = kwargs["assistant_id"]
deleted = self._state.beta.assistants.delete(assistant_id)
return httpx.Response(
status_code=200,
json=model_dict(
AssistantDeleted(id=id, deleted=deleted, object="assistant.deleted")
AssistantDeleted(
id=assistant_id, deleted=deleted, object="assistant.deleted"
)
),
)

Expand Down
14 changes: 7 additions & 7 deletions src/openai_responses/_routes/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _build(
class FileRetrieveRoute(StatefulRoute[FileObject, PartialFileObject]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.get(url__regex=r"/files/(?P<id>[a-zA-Z0-9\-]+)"),
route=router.get(url__regex=r"/files/(?P<file_id>[a-zA-Z0-9\-]+)"),
status_code=200,
state=state,
)
Expand All @@ -115,8 +115,8 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
found = self._state.files.get(id)
fil_id = kwargs["file_id"]
found = self._state.files.get(fil_id)
if not found:
return httpx.Response(404)

Expand All @@ -133,7 +133,7 @@ def _build(
class FileDeleteRoute(StatefulRoute[FileObject, PartialFileDeleted]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.delete(url__regex=r"/files/(?P<id>[a-zA-Z0-9\-]+)"),
route=router.delete(url__regex=r"/files/(?P<file_id>[a-zA-Z0-9\-]+)"),
status_code=200,
state=state,
)
Expand All @@ -146,11 +146,11 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
deleted = self._state.files.delete(id)
file_id = kwargs["file_id"]
deleted = self._state.files.delete(file_id)
return httpx.Response(
status_code=200,
json=model_dict(FileDeleted(id=id, deleted=deleted, object="file")),
json=model_dict(FileDeleted(id=file_id, deleted=deleted, object="file")),
)

@staticmethod
Expand Down
22 changes: 12 additions & 10 deletions src/openai_responses/_routes/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class MessageRetrieveRoute(StatefulRoute[Message, PartialMessage]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.get(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/messages/(?P<id>[a-zA-Z0-9\_]+)"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/messages/(?P<message_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
Expand All @@ -167,8 +167,8 @@ def _handler(
if not found_thread:
return httpx.Response(404)

id = kwargs["id"]
found_message = self._state.beta.threads.messages.get(id)
message_id = kwargs["message_id"]
found_message = self._state.beta.threads.messages.get(message_id)
if not found_message:
return httpx.Response(404)

Expand All @@ -183,7 +183,7 @@ class MessageUpdateRoute(StatefulRoute[Message, PartialMessage]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.post(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/messages/(?P<id>[a-zA-Z0-9\_]+)"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/messages/(?P<message_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
Expand All @@ -203,8 +203,8 @@ def _handler(
if not found_thread:
return httpx.Response(404)

id = kwargs["id"]
found_message = self._state.beta.threads.messages.get(id)
message_id = kwargs["message_id"]
found_message = self._state.beta.threads.messages.get(message_id)
if not found_message:
return httpx.Response(404)

Expand All @@ -224,7 +224,7 @@ class MessageDeleteRoute(StatefulRoute[MessageDeleted, PartialMessageDeleted]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.delete(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/messages/(?P<id>[a-zA-Z0-9\_]+)"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/messages/(?P<message_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
Expand All @@ -244,12 +244,14 @@ def _handler(
if not found_thread:
return httpx.Response(404)

id = kwargs["id"]
deleted = self._state.beta.threads.messages.delete(id)
message_id = kwargs["message_id"]
deleted = self._state.beta.threads.messages.delete(message_id)
return httpx.Response(
status_code=200,
json=model_dict(
MessageDeleted(id=id, deleted=deleted, object="thread.message.deleted")
MessageDeleted(
id=message_id, deleted=deleted, object="thread.message.deleted"
)
),
)

Expand Down
6 changes: 3 additions & 3 deletions src/openai_responses/_routes/run_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RunStepRetrieveRoute(StatefulRoute[RunStep, PartialRunStep]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.get(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<run_id>[a-zA-Z0-9\_]+)/steps/(?P<id>[a-zA-Z0-9\_]+)"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<run_id>[a-zA-Z0-9\_]+)/steps/(?P<step_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
Expand All @@ -110,8 +110,8 @@ def _handler(
if not found_run:
return httpx.Response(404)

id = kwargs["id"]
found_run_step = self._state.beta.threads.runs.steps.get(id)
step_id = kwargs["step_id"]
found_run_step = self._state.beta.threads.runs.steps.get(step_id)
if not found_run_step:
return httpx.Response(404)

Expand Down
24 changes: 12 additions & 12 deletions src/openai_responses/_routes/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class RunRetrieveRoute(StatefulRoute[Run, PartialRun]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.get(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<id>[a-zA-Z0-9\_]+)"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<run_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
Expand All @@ -224,8 +224,8 @@ def _handler(
if not found_thread:
return httpx.Response(404)

id = kwargs["id"]
found_run = self._state.beta.threads.runs.get(id)
run_id = kwargs["run_id"]
found_run = self._state.beta.threads.runs.get(run_id)
if not found_run:
return httpx.Response(404)

Expand All @@ -240,7 +240,7 @@ class RunUpdateRoute(StatefulRoute[Run, PartialRun]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.post(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<id>[a-zA-Z0-9\_]+)"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<run_id>[a-zA-Z0-9\_]+)"
),
status_code=200,
state=state,
Expand All @@ -260,8 +260,8 @@ def _handler(
if not found_thread:
return httpx.Response(404)

id = kwargs["id"]
found_run = self._state.beta.threads.runs.get(id)
run_id = kwargs["run_id"]
found_run = self._state.beta.threads.runs.get(run_id)
if not found_run:
return httpx.Response(404)

Expand All @@ -281,7 +281,7 @@ class RunSubmitToolOutputsRoute(StatefulRoute[Run, PartialRun]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.post(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<id>[a-zA-Z0-9\_]+)/submit_tool_outputs"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<run_id>[a-zA-Z0-9\_]+)/submit_tool_outputs"
),
status_code=200,
state=state,
Expand All @@ -302,8 +302,8 @@ def _handler(
if not found_thread:
return httpx.Response(404)

id = kwargs["id"]
found_run = self._state.beta.threads.runs.get(id)
run_id = kwargs["run_id"]
found_run = self._state.beta.threads.runs.get(run_id)
if not found_run:
return httpx.Response(404)

Expand All @@ -318,7 +318,7 @@ class RunCancelRoute(StatefulRoute[Run, PartialRun]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.post(
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<id>[a-zA-Z0-9\_]+)/cancel"
url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)/runs/(?P<run_id>[a-zA-Z0-9\_]+)/cancel"
),
status_code=200,
state=state,
Expand All @@ -339,8 +339,8 @@ def _handler(
if not found_thread:
return httpx.Response(404)

id = kwargs["id"]
found_run = self._state.beta.threads.runs.get(id)
run_id = kwargs["run_id"]
found_run = self._state.beta.threads.runs.get(run_id)
if not found_run:
return httpx.Response(404)

Expand Down
20 changes: 10 additions & 10 deletions src/openai_responses/_routes/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _build(partial: PartialThread, request: httpx.Request) -> Thread:
class ThreadRetrieveRoute(StatefulRoute[Thread, PartialThread]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.get(url__regex=r"/threads/(?P<id>[a-zA-Z0-9\_]+)"),
route=router.get(url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)"),
status_code=200,
state=state,
)
Expand All @@ -88,8 +88,8 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
found = self._state.beta.threads.get(id)
thread_id = kwargs["thread_id"]
found = self._state.beta.threads.get(thread_id)
if not found:
return httpx.Response(404)

Expand All @@ -104,7 +104,7 @@ class ThreadUpdateRoute(StatefulRoute[Thread, PartialThread]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.post(
url__regex=r"/threads/(?P<id>(?!.*runs)[a-zA-Z0-9_]+)" # NOTE: avoids match on /threads/runs
url__regex=r"/threads/(?P<thread_id>(?!.*runs)[a-zA-Z0-9_]+)" # NOTE: avoids match on /threads/runs
),
status_code=200,
state=state,
Expand All @@ -118,8 +118,8 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
found = self._state.beta.threads.get(id)
thread_id = kwargs["thread_id"]
found = self._state.beta.threads.get(thread_id)
if not found:
return httpx.Response(404)

Expand All @@ -138,7 +138,7 @@ def _build(partial: PartialThread, request: httpx.Request) -> Thread:
class ThreadDeleteRoute(StatefulRoute[ThreadDeleted, PartialThreadDeleted]):
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
super().__init__(
route=router.delete(url__regex=r"/threads/(?P<id>[a-zA-Z0-9\_]+)"),
route=router.delete(url__regex=r"/threads/(?P<thread_id>[a-zA-Z0-9\_]+)"),
status_code=200,
state=state,
)
Expand All @@ -151,12 +151,12 @@ def _handler(
**kwargs: Any,
) -> httpx.Response:
self._route = route
id = kwargs["id"]
deleted = self._state.beta.threads.delete(id)
thread_id = kwargs["thread_id"]
deleted = self._state.beta.threads.delete(thread_id)
return httpx.Response(
status_code=200,
json=model_dict(
ThreadDeleted(id=id, deleted=deleted, object="thread.deleted")
ThreadDeleted(id=thread_id, deleted=deleted, object="thread.deleted")
),
)

Expand Down

0 comments on commit b3560ed

Please sign in to comment.