-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f6d175
commit 6521b89
Showing
2 changed files
with
56 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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,48 @@ | ||
from typing_extensions import Self | ||
|
||
from grpclib.client import Channel | ||
|
||
from viam.rpc.dial import DialOptions, _dial_app, _get_access_token | ||
from viam import logging | ||
from viam.app.data.client import DataClient | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class AppClient: | ||
"""gRPC client for all communication and interaction with app. | ||
Use create() to instantiate an AppClient:: | ||
AppClient.create(...) | ||
You may ``close()`` an AppClient after its use. | ||
""" | ||
|
||
@classmethod | ||
async def create(cls, dial_options: DialOptions) -> Self: | ||
""" | ||
Create an app client that establishes a connection to viam.app.com. | ||
Args: | ||
dial_options (DialOptions): Required information for authorization and connection to app, creds and auth_entity are necessary. | ||
Returns: | ||
Self: the AppClient. | ||
""" | ||
self = cls() | ||
self._channel = await _dial_app(dial_options) | ||
access_token = await _get_access_token(self._channel, dial_options.auth_entity, dial_options) | ||
self._metadata = {"authorization": f"Bearer {access_token}"} | ||
return self | ||
|
||
_channel: Channel | ||
_metadata: str | ||
_closed: bool = False | ||
|
||
@property | ||
def data_client(self) -> DataClient: | ||
return DataClient(self._channel, self._metadata) | ||
|
||
async def close(self): | ||
raise NotImplementedError() |
This file contains 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