Skip to content

Commit

Permalink
RSDK-4528 - Allow API keys for cloud apis + allow users to specify ap…
Browse files Browse the repository at this point in the history
…p url (#424)
  • Loading branch information
cheukt authored Sep 7, 2023
1 parent 715e587 commit e5d6ca9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
17 changes: 11 additions & 6 deletions src/viam/app/viam_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Mapping
from typing import Mapping, Optional

from grpclib.client import Channel
from typing_extensions import Self
Expand All @@ -20,13 +20,14 @@ class ViamClient:
"""

@classmethod
async def create_from_dial_options(cls, dial_options: DialOptions) -> Self:
"""Create `ViamClient` that establishes a connection to app.viam.com.
async def create_from_dial_options(cls, dial_options: DialOptions, app_url: Optional[str] = None) -> Self:
"""Create `ViamClient` that establishes a connection to the Viam app.
Args:
dial_options (viam.rpc.dial.DialOptions): Required information for authorization and connection to app. `creds` and
`auth_entity` fields are required.
app_url: (Optional[str]): URL of app. Uses app.viam.com if not specified.
Raises:
ValueError: If the input parameters are missing a required field or simply invalid.
Expand All @@ -42,16 +43,20 @@ async def create_from_dial_options(cls, dial_options: DialOptions) -> Self:
raise ValueError("dial_options.auth_entity cannot be None.")

self = cls()
self._location_id = dial_options.auth_entity.split(".")[1]
self._channel = await _dial_app(dial_options)
self._location_id = None
if dial_options.credentials.type == "robot-location-secret":
self._location_id = dial_options.auth_entity.split(".")[1]
if app_url is None:
app_url = "app.viam.com"
self._channel = await _dial_app(app_url)
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: Mapping[str, str]
_closed: bool = False
_location_id: str
_location_id: Optional[str]

@property
def data_client(self) -> DataClient:
Expand Down
11 changes: 4 additions & 7 deletions src/viam/rpc/dial.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@

@dataclass
class Credentials:
"""Credentials to connect to the robot.
"""Credentials to connect to the robot and the Viam app."""

Currently only supports robot location secret.
"""

type: Union[Literal["robot-location-secret"], Literal["robot-secret"]]
type: Union[Literal["robot-location-secret"], Literal["robot-secret"], Literal["api-key"]]
"""The type of credential
"""

Expand Down Expand Up @@ -314,5 +311,5 @@ async def dial_direct(address: str, options: Optional[DialOptions] = None) -> Ch
return await _dial_direct(address, options)


async def _dial_app(options: DialOptions) -> Channel:
return await _dial_direct("app.viam.com:443")
async def _dial_app(app_url: str) -> Channel:
return await _dial_direct(app_url)

0 comments on commit e5d6ca9

Please sign in to comment.