Skip to content

Commit

Permalink
Merge pull request #51 from foarsitter/refactor
Browse files Browse the repository at this point in the history
Rename response to raw_response and next() to next_from_response()
  • Loading branch information
foarsitter authored Feb 11, 2024
2 parents 8d2b9ba + 34d9d27 commit ccc9c22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
34 changes: 21 additions & 13 deletions src/requestmodel/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,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 @@ -119,17 +119,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

0 comments on commit ccc9c22

Please sign in to comment.