Skip to content

Commit

Permalink
refactor: Replace requests with httpx (#17)
Browse files Browse the repository at this point in the history
- Update dependencies: requests -> httpx
- Refactor HTTP client code to use httpx
  • Loading branch information
chyroc authored Sep 26, 2024
1 parent 09d2a05 commit 62f5e83
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Build
run: |
poetry run ruff check cozepy
poetry run format --check
poetry run ruff format --check
poetry build
- name: Run tests
run: poetry run pytest
Expand Down
4 changes: 2 additions & 2 deletions cozepy/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class ChatEvent(CozeModel):


class ChatChatIterator(object):
def __init__(self, iters: Iterator[bytes]):
def __init__(self, iters: Iterator[str]):
self._iters = iters

def __iter__(self):
Expand All @@ -241,7 +241,7 @@ def __next__(self) -> ChatEvent:
times = 0

while times < 2:
line = next(self._iters).decode("utf-8")
line = next(self._iters)
if line == "":
continue
elif line.startswith("event:"):
Expand Down
9 changes: 4 additions & 5 deletions cozepy/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Iterator,
)

import requests
from requests import Response
import httpx
from httpx import Response

if TYPE_CHECKING:
from cozepy.auth import Auth
Expand Down Expand Up @@ -48,22 +48,21 @@ def request(
files: dict = None,
stream: bool = False,
data_field: str = "data",
) -> Union[T, List[T], Iterator[bytes]]:
) -> Union[T, List[T], Iterator[str]]:
"""
Send a request to the server.
"""
if headers is None:
headers = {}
if self._auth:
self._auth.authentication(headers)
r = requests.request(
r = httpx.request(
method,
url,
params=params,
headers=headers,
json=body,
files=files,
stream=stream,
)
if stream:
return r.iter_lines()
Expand Down
4 changes: 2 additions & 2 deletions cozepy/workflows/runs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class WorkflowEvent(CozeModel):


class WorkflowEventIterator(object):
def __init__(self, iters: Iterator[bytes]):
def __init__(self, iters: Iterator[str]):
self._iters = iters

def __iter__(self):
Expand All @@ -113,7 +113,7 @@ def __next__(self) -> WorkflowEvent:
times = 0

while times < 3:
line = next(self._iters).decode("utf-8")
line = next(self._iters)
if line == "":
continue
elif line.startswith("id:"):
Expand Down
Loading

0 comments on commit 62f5e83

Please sign in to comment.