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

Rename response to raw_response and next() to next_from_response() #51

Merged
merged 1 commit into from
Feb 11, 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
34 changes: 21 additions & 13 deletions src/requestmodel/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,26 @@ def request_args_for_values(self) -> RequestArgs:


class RequestModel(BaseRequestModel[ResponseType]):
response: Optional[Response] = None
raw_response: Optional[Response] = None

def handle_error(self, response: Response) -> None:
response.raise_for_status()

def send(self, client: Client) -> ResponseType:
"""Send the request synchronously"""
r = self.as_request(client)
self.response = client.send(r)
self.handle_error(self.response)
self.raw_response = client.send(r)
self.handle_error(self.raw_response)
if isinstance(self.response_model, TypeAdapter):
return self.response_model.validate_python(self.response.json())
return self.response_model.model_validate(self.response.json())
return self.response_model.validate_python(self.raw_response.json())
return self.response_model.model_validate(self.raw_response.json())

async def asend(self, client: AsyncClient) -> ResponseType:
"""Send the request asynchronously"""
r = self.as_request(client)
self.response = await client.send(r)
self.handle_error(self.response)
return self.response_model.model_validate(self.response.json())
self.raw_response = await client.send(r)
self.handle_error(self.raw_response)
return self.response_model.model_validate(self.raw_response.json())

def as_request(self, client: BaseClient) -> Request:
"""Transform the properties of the object into a request"""
Expand All @@ -117,17 +117,25 @@ def as_request(self, client: BaseClient) -> Request:


class IteratorRequestModel(RequestModel[ResponseType]):
response: Optional[Response] = None
raw_response: Optional[Response] = None

def next(self, response: ResponseType) -> bool: # pragma: no cover
def next_from_response(self, response: ResponseType) -> bool: # pragma: no cover
"""
Prepare the current object for the next request and return true

In case of cursor pagination set the next token
In case of offset pagination increment the offset

"""
raise NotImplementedError

@override
def send(self, client: Client) -> Iterator[ResponseType]: # type: ignore[override]
response = super().send(client)
yield response
self.response = None
while self.next(response):
self.response = None

while self.next_from_response(response):
# avoid serializing the response
self.raw_response = None
response = super().send(client)
yield response
2 changes: 1 addition & 1 deletion tests/test_param_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PaginatedRequest(IteratorRequestModel[PaginatedResponse]):
page: int
size: int

def next(self, response: PaginatedResponse) -> bool:
def next_from_response(self, response: PaginatedResponse) -> bool:
self.page = response.page + 1
if self.page * response.size > response.total:
return False
Expand Down
Loading