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

OKTA-672843 include response for next() during paging operation #379

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,27 @@ loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```

If you require access to the response headers during a pagination operation, set the `includeResponse` parameter to `True` in the call to the `next` method; this returns the response as a third tuple value. See the following example:

```python
from okta.client import Client as OktaClient
import asyncio

async def main():
client = OktaClient()
users, resp, err = await client.list_users()
while True:
for user in users:
print(user.profile.login)
if resp.has_next():
users, err, response = await resp.next(True) # Specify True to receive the response object as the third part of the tuple for further analysis
else:
break

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```

## Logging

> Feature appears in version 1.5.0
Expand Down
20 changes: 13 additions & 7 deletions okta/api_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def has_next(self):
"""
return self._next is not None

async def next(self):
async def next(self, includeResponse=False):
"""
Generator iterating function. Retrieves the next page of results
from the API.
Expand All @@ -134,17 +134,23 @@ async def next(self):
# if not self.has_next():
# return (None, None) #This causes errors with our async testing
MODELS_NOT_TO_CAMEL_CASE = [User, Group, UserSchema, GroupSchema]
next_page, error = await self.get_next().__anext__()
next_page, error, next_response = await self.get_next().__anext__()
if error:
return (None, error)
if self._type is not None:
result = []
for item in next_page:
result.append(self._type(item) if self._type in MODELS_NOT_TO_CAMEL_CASE
else self._type(APIClient.form_response_body(item)))
return (result, None)
if includeResponse:
return (result, None, next_response)
else:
return (result, None)

return (next_page, error)
if includeResponse:
return (next_page, error, next_response)
else:
return (next_page, error)

async def get_next(self):
"""
Expand All @@ -162,19 +168,19 @@ async def get_next(self):
if error:
# Return None if error and set next to none
self._next = None
yield (None, error)
yield (None, error, None)

req, res_details, resp_body, error = await \
self._request_executor.fire_request(next_request)
if error:
# Return None if error and set next to none
self._next = None
yield (None, error)
yield (None, error, None)

if next_request:
# create new response and update generator values
next_response = OktaAPIResponse(
self._request_executor, req, res_details, resp_body)
self._next = next_response._next
# yield next page
yield (next_response.get_body(), None)
yield (next_response.get_body(), None, next_response)
bryanapellanes-okta marked this conversation as resolved.
Show resolved Hide resolved
Loading