Skip to content

Commit

Permalink
restructure app client
Browse files Browse the repository at this point in the history
  • Loading branch information
bashar-515 committed Jun 26, 2023
1 parent 3f6d175 commit 6521b89
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
48 changes: 48 additions & 0 deletions src/viam/app/client.py
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()
34 changes: 8 additions & 26 deletions src/viam/app/data/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import List, Optional, Mapping, Any
from typing_extensions import Self

from grpclib.client import Channel

Expand All @@ -13,7 +12,6 @@
FileData
)
from viam.proto.app.datasync import DataSyncServiceStub
from viam.rpc.dial import DialOptions, _dial_app, _get_access_token
from viam import logging

LOGGER = logging.getLogger(__name__)
Expand All @@ -22,40 +20,24 @@
class DataClient:
"""gRPC client for uploading and retreiving data from app
Use connect() to instantiate a DataClient::
DataClient.connect(...)
You must ``close()`` a DataClient after its use.
Constructor must be passed a channel to app, along with the required metadata.
"""

@classmethod
async def connect(cls, dial_options: DialOptions) -> Self:
def __init__(self, channel: Channel, metadata: str):
"""
Create a data client that establishes a connection to app.
Args:
dial_options (DialOptions): Required information for authorization and connected to app, creds and auth_entity must be provided.
Returns:
Self: the DataClient.
channel (Channel): an already-established connection to app.
metadata (str): the required authorization token to send requests to app.
"""
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}"}
self._data_client = DataServiceStub(self._channel)
self._data_sync_client = DataSyncServiceStub(self._channel)
return self

_channel: Channel
self._metadata = metadata
self._data_client = DataServiceStub(channel)
self._data_sync_client = DataSyncServiceStub(channel)

_data_client: DataServiceStub
_data_sync_client: DataSyncServiceStub
_metadata: str
_closed: bool = False

async def close(self):
raise NotImplementedError()

async def tabular_data_by_filter(
self,
Expand Down

0 comments on commit 6521b89

Please sign in to comment.