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

Design by wishful thinking for Connect SDK #16

Merged
merged 6 commits into from
Feb 14, 2024
Merged

Conversation

nealrichardson
Copy link
Collaborator

@nealrichardson nealrichardson commented Feb 6, 2024

Just to start discussion

Fixes #9


```
my_app.update(title="Quarterly Analysis of Team Velocity")
my_app.permissions.find_one(email="[email protected]").update(role="owner")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like this quite a bit. +1 for find_one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming that find_one(kwargs) is find(kwargs)[0], should it error if find() returns len != 1?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've associated find with an Optional return value in other languages. (e.g., go find this thing if you can; otherwise, let me know you came up empty-handed.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restored .find_one() and more explicitly described behavior, LMK if that's what you had in mind.

Comment on lines +9 to +13
```
from posit.connect import Client

con = Client()
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```
from posit.connect import Client
con = Client()
```
from posit.connect import create_client
with create_client() as client:
client...

This helps with resource management.

@contextmanager
def create_client(
    api_key: Optional[str] = None, endpoint: Optional[str] = None
) -> Generator[Client, None, None]:
    """Creates a new :class:`Client` instance

    Keyword Arguments:
        api_key -- an api_key for authentication (default: {None})
        endpoint -- a base api endpoint (url) (default: {None})

    Returns:
        A :class:`Client` instance
    """
    client = Client(api_key=api_key, endpoint=endpoint)
    try:
        yield client
    finally:
        del client

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What resource do you expect client to be holding onto?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, it holds a requests.Session object, which has a close method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything that needs to be cleaned up that won't be handled on exit?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the requests.Session instance that needs to be released. Otherwise, the GC should handle most scenarios.

Entities have methods that are appropriate to them. Fields in the entity bodies can be accessed as properties.

```
for st in con.content.find(app_mode="streamlit"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the .find(app_mode="streamline") vs .find({ "app_mode": "streamline"}). I'm unsure what it looks like to support API differences between Connect versions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many ways to address this and we don't need to solve it now. But, I would like to note it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we make the Python interface whatever we think it should be, and internally map parameters to whatever the connect api/version uses.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some language about this below, LMK what you think.

* If no entities match the query, `.find()` returns a length-0 collection.
* Iterating over a collection without having first called `find()` is equivalent to having queried for all.
* `find()` should use query-based REST APIs where existing, and fall back to retrieving all and filtering client-side where those APIs do not (yet) exist.
* Should `collection.find().find()` work? Probably.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and the second call should operate on in-memory/cached results.

Copy link
Collaborator Author

@nealrichardson nealrichardson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this, but we can of course continue to refine as we go forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Establish conventions for querying Connect API collections
3 participants