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

refactor: Replace requests with httpx #17

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
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
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
Loading