-
Notifications
You must be signed in to change notification settings - Fork 2
Enable creation of nodes from dictionary #7
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
Open
InnocentBug
wants to merge
4
commits into
main
Choose a base branch
from
child-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import httpx | ||
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven | ||
from .._utils import ( | ||
maybe_transform, | ||
async_maybe_transform, | ||
) | ||
from .._compat import cached_property | ||
from .._response import ( | ||
to_raw_response_wrapper, | ||
to_streamed_response_wrapper, | ||
async_to_raw_response_wrapper, | ||
async_to_streamed_response_wrapper, | ||
) | ||
from .._base_client import ( | ||
make_request_options, | ||
) | ||
import cript | ||
import inspect | ||
from ..types.shared.search import Search | ||
from .._resource import SyncAPIResource | ||
|
||
class ChildPaginator: | ||
# TODO consider writing operations | ||
def __init__(self, parent, child, client=None, raw_dict_output=False): | ||
if client is None: | ||
client = parent.client | ||
self._client = client | ||
self._parent = parent | ||
self._child = child | ||
|
||
self._current_child_list = [] | ||
self._current_child_position = 0 | ||
self._current_page = 0 | ||
self._count = None | ||
self.raw_dict_output = raw_dict_output | ||
|
||
def __iter__(self): | ||
self._current_child_position = 0 | ||
return self | ||
|
||
def __next__(self): | ||
if self._current_child_position >= len(self._current_child_list): | ||
self._fetch_next_page() | ||
try: | ||
next_node = self._current_child_list[self._current_child_position] | ||
except IndexError: | ||
raise StopIteration | ||
|
||
self._current_child_position += 1 | ||
|
||
if not self.raw_dict_output: | ||
|
||
return next_node | ||
|
||
def _fetch_next_page(self): | ||
if self._finished_fetching: | ||
raise StopIteration | ||
|
||
response = self._client._child.child(self._parent, self._child, self._current_page) | ||
self._current_page += 1 | ||
if self._count is not None and self._count != int(response.data.count): | ||
raise RuntimeError("The number of elements for a child iteration changed during pagination. This may lead to inconsistencies. Please try again.") | ||
self._count = int(response.data.count) | ||
|
||
self._current_child_list += response.data.result | ||
|
||
# Make it a random access iterator, since ppl expect it to behave list a list | ||
def __getitem__(self, key): | ||
key_index = int(key) | ||
previous_pos = self._current_child_position | ||
try: | ||
if key_index < 0: | ||
while not self._finished_fetching: | ||
next(self) | ||
|
||
while len(self._current_child_list) <= key_index: | ||
try: | ||
next(self) | ||
except StopIteration: | ||
break | ||
finally: | ||
self._current_child_position = previous_pos | ||
# We don't need explicit bounds checking, since the list access does that for us. | ||
return self._current_child_list[key_index] | ||
|
||
def __len__(self): | ||
previous_pos = self._current_child_position | ||
try: | ||
if self._count is None: | ||
try: | ||
next(iter(self)) | ||
except StopIteration: | ||
self._count = 0 | ||
finally: | ||
self._current_child_position = previous_pos | ||
return self._count | ||
|
||
@property | ||
def _finished_fetching(self): | ||
if self._count is None: | ||
return False | ||
return len(self._current_child_list) == self._count | ||
|
||
|
||
class ChildResource(SyncAPIResource): | ||
@cached_property | ||
def with_raw_response(self): | ||
return ChildResourceWithRawResponse(self) | ||
|
||
@cached_property | ||
def with_streaming_response(self): | ||
return ChildResourceWithStreamingResponse(self) | ||
|
||
def child( | ||
self, | ||
parent, | ||
child: str, | ||
page: int, | ||
*, | ||
extra_headers: Headers | None = None, | ||
extra_query: Query | None = None, | ||
extra_body: Body | None = None, | ||
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, | ||
) -> Search: | ||
""" | ||
Obtain all children of parent node. | ||
|
||
Args: | ||
parent: parent node | ||
child: attribute name of the child node | ||
""" | ||
return self._get(f"/{parent.name_url}/{parent.uuid}/{child}", | ||
options=make_request_options( | ||
extra_headers=extra_headers, | ||
extra_query=extra_query, | ||
extra_body=extra_body, | ||
query={"page": page}, | ||
timeout=timeout, | ||
), | ||
cast_to=Search, | ||
) | ||
|
||
|
||
class ChildResourceWithRawResponse: | ||
def __init__(self, child:ChildResource) -> None: | ||
self._child = child | ||
|
||
self.node = to_raw_response_wrapper(child.node) | ||
|
||
class ChildResourceWithStreamingResponse: | ||
def __init__(self, child:ChildResource) -> None: | ||
self._child = child | ||
|
||
self.node = to_streamed_response_wrapper(child.node) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to assign valid kwargs to self at this point and then return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did I do it right now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but we need to make sure not to include the extra fields that the api returns, you can refactor the
retrieve_by_uuid
and create a separate method fromThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pointers.
I used that snippet to refactor it a little bit.
I think there is some room to streamline this a little bit.
But we can address that in a refactor down the line.