-
Notifications
You must be signed in to change notification settings - Fork 98
Add client method to Session class #858
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
Changes from 6 commits
0b0f69b
4691840
f1b7b65
762345a
1ec36f8
184f538
72f1851
977d51a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,14 +16,15 @@ | |
| from . import order_request, reporting | ||
| from .__version__ import __version__ # NOQA | ||
| from .auth import Auth | ||
| from .clients import DataClient, OrdersClient # NOQA | ||
| from .clients import DataClient, OrdersClient, SubscriptionsClient # NOQA | ||
| from .io import collect | ||
|
|
||
| __all__ = [ | ||
| 'Auth', | ||
| 'collect', | ||
| 'DataClient' | ||
| 'OrdersClient', | ||
| 'SubscriptionsClient', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found that this was missing! |
||
| 'order_request', | ||
| 'reporting', | ||
| 'Session', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,9 @@ | |
| import random | ||
| import time | ||
| from typing import AsyncGenerator, Optional | ||
|
|
||
| import httpx | ||
| from typing_extensions import Literal | ||
|
|
||
| from .auth import Auth, AuthType | ||
| from . import exceptions, models | ||
|
|
@@ -413,6 +415,35 @@ async def stream( | |
| finally: | ||
| await response.aclose() | ||
|
|
||
| def client(self, | ||
| name: Literal['data', 'orders', 'subscriptions'], | ||
| base_url: Optional[str] = None) -> object: | ||
| """Get a client by its module name. | ||
|
|
||
| Parameters: | ||
| name: one of 'data', 'orders', or 'subscriptions'. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This typing hint IS nice! This would be great to use this for all of the other arguments that have a set number of 'valid' entries, we already have them defined either at the top of the client module or in the specs module. |
||
|
|
||
| Returns: | ||
| A client instance. | ||
|
|
||
| Raises: | ||
| ClientError when no such client can be had. | ||
|
|
||
| """ | ||
| # To avoid circular dependency. | ||
| from planet.clients.data import DataClient | ||
| from planet.clients.orders import OrdersClient | ||
| from planet.clients.subscriptions import SubscriptionsClient | ||
|
||
|
|
||
| try: | ||
| return { | ||
| 'data': DataClient, | ||
| 'orders': OrdersClient, | ||
| 'subscriptions': SubscriptionsClient | ||
|
||
| }[name](self, base_url=base_url) | ||
| except KeyError: | ||
| raise exceptions.ClientError("No such client.") | ||
|
|
||
|
|
||
| class AuthSession(BaseSession): | ||
| """Synchronous connection to the Planet Auth service.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| 'jsonschema', | ||
| 'pyjwt>=2.1', | ||
| 'tqdm>=4.56', | ||
| 'typing-extensions', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module provides a backport of |
||
| ] | ||
|
|
||
| test_requires = ['pytest', 'pytest-asyncio==0.16', 'pytest-cov', 'respx==0.19'] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """Session module tests.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from planet import DataClient, OrdersClient, SubscriptionsClient, Session | ||
| from planet.exceptions import ClientError | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("client_name,client_class", | ||
| [('data', DataClient), ('orders', OrdersClient), | ||
| ('subscriptions', SubscriptionsClient)]) | ||
| def test_session_get_client(client_name, client_class): | ||
| """Get a client from a session.""" | ||
| session = Session() | ||
| client = session.client(client_name) | ||
| assert isinstance(client, client_class) | ||
|
|
||
|
|
||
| def test_session_get_client_error(): | ||
| """Get an exception when no such client exists.""" | ||
| session = Session() | ||
| with pytest.raises(ClientError): | ||
| _ = session.client('bogus') | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test coverage of the new code is 100%. |
||

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.
This is what I mean by "centers the Session class". Creating one is the first thing we do and it's the manager for the context of a block of code.