Skip to content

Commit e4dd87b

Browse files
authored
DOCS-1875: Add generic and ViamClient examples (#558)
1 parent 1125f05 commit e4dd87b

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

src/viam/app/viam_client.py

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
class ViamClient:
1717
"""gRPC client for all communication and interaction with app.
1818
19+
`ViamClient` class for creating and managing specialized client instances.
1920
There is currently 1 way to instantiate a `ViamClient` object::
2021
2122
ViamClient.create_from_dial_options(...)
@@ -25,10 +26,14 @@ class ViamClient:
2526
async def create_from_dial_options(cls, dial_options: DialOptions, app_url: Optional[str] = None) -> Self:
2627
"""Create `ViamClient` that establishes a connection to the Viam app.
2728
28-
Args:
29+
::
30+
31+
dial_options = DialOptions.with_api_key("<API-KEY>", "<API-KEY-ID>")
32+
ViamClient.create_from_dial_options(dial_options)
2933
30-
dial_options (viam.rpc.dial.DialOptions): Required information for authorization and connection to app. `creds` and
31-
`auth_entity` fields are required.
34+
Args:
35+
dial_options (viam.rpc.dial.DialOptions): Required information for authorization and connection to app.
36+
`creds` and `auth_entity` fields are required.
3237
app_url: (Optional[str]): URL of app. Uses app.viam.com if not specified.
3338
3439
Raises:
@@ -62,22 +67,70 @@ async def create_from_dial_options(cls, dial_options: DialOptions, app_url: Opti
6267

6368
@property
6469
def data_client(self) -> DataClient:
65-
"""Insantiate and return a `DataClient` used to make `data` and `data_sync` method calls."""
70+
"""Instantiate and return a `DataClient` object used to make `data` and `data_sync` method calls.
71+
To use the `DataClient`, you must first instantiate a `ViamClient`.
72+
73+
::
74+
75+
async def connect() -> ViamClient:
76+
# Replace "<API-KEY>" (including brackets) with your API key and "<API-KEY-ID>" with your API key ID
77+
dial_options = DialOptions.with_api_key("<API-KEY>", "<API-KEY-ID>")
78+
return await ViamClient.create_from_dial_options(dial_options)
79+
80+
async def main():
81+
viam_client = await connect()
82+
83+
# Instantiate a DataClient to run data client API methods on
84+
data_client = viam_client.data_client
85+
"""
6686
return DataClient(self._channel, self._metadata)
6787

6888
@property
6989
def app_client(self) -> AppClient:
70-
"""Insantiate and return an `AppClient` used to make `app` method calls."""
90+
"""Instantiate and return an `AppClient` used to make `app` method calls.
91+
To use the `AppClient`, you must first instantiate a `ViamClient`.
92+
93+
::
94+
95+
async def connect() -> ViamClient:
96+
# Replace "<API-KEY>" (including brackets) with your API key and "<API-KEY-ID>" with your API key ID
97+
dial_options = DialOptions.with_api_key("<API-KEY>", "<API-KEY-ID>")
98+
return await ViamClient.create_from_dial_options(dial_options)
99+
100+
101+
async def main():
102+
viam_client = await connect()
103+
104+
# Instantiate an AppClient called "fleet" to run fleet management API methods on
105+
fleet = viam_client.app_client
106+
"""
71107
return AppClient(self._channel, self._metadata, self._location_id)
72108

73109
@property
74110
def ml_training_client(self) -> MLTrainingClient:
75-
"""Instantiate and return a `MLTrainingClient` used to make `ml_training` method calls."""
111+
"""Instantiate and return a `MLTrainingClient` used to make `ml_training` method calls.
112+
To use the `MLTrainingClient`, you must first instantiate a `ViamClient`.
113+
114+
::
115+
116+
async def connect() -> ViamClient:
117+
# Replace "<API-KEY>" (including brackets) with your API key and "<API-KEY-ID>" with your API key ID
118+
dial_options = DialOptions.with_api_key("<API-KEY>", "<API-KEY-ID>")
119+
return await ViamClient.create_from_dial_options(dial_options)
120+
121+
122+
async def main():
123+
viam_client = await connect()
124+
125+
# Instantiate an MLTrainingClient to run ML training client API methods on
126+
ml_training_client = viam_client.ml_training_client
127+
"""
76128
return MLTrainingClient(self._channel, self._metadata)
77129

78130
@property
79131
def billing_client(self) -> BillingClient:
80-
"""Instantiate and return a `BillingClient` used to make `billing` method calls."""
132+
"""Instantiate and return a `BillingClient` used to make `billing` method calls.
133+
To use the `BillingClient`, you must first instantiate a `ViamClient`."""
81134
return BillingClient(self._channel, self._metadata)
82135

83136
def close(self):

src/viam/services/service_base.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ def __init__(self, name: str) -> None:
2525
def from_robot(cls, robot: "RobotClient", name: str) -> Self:
2626
"""Get the service named ``name`` from the provided robot.
2727
28+
::
29+
30+
async def connect() -> ViamClient:
31+
# Replace "<API-KEY>" (including brackets) with your API key and "<API-KEY-ID>" with your API key ID
32+
dial_options = DialOptions.with_api_key("<API-KEY>", "<API-KEY-ID>")
33+
return await ViamClient.create_from_dial_options(dial_options)
34+
35+
async def main():
36+
robot = await connect()
37+
38+
# Can be used with any resource, using the motion service as an example
39+
motion = MotionClient.from_robot(robot=robot, name="builtin")
40+
41+
robot.close()
42+
2843
Args:
2944
robot (RobotClient): The robot
3045
name (str): The name of the service
@@ -36,7 +51,19 @@ def from_robot(cls, robot: "RobotClient", name: str) -> Self:
3651
return cast(cls, service)
3752

3853
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
39-
"""Send/receive arbitrary commands
54+
"""Send/receive arbitrary commands.
55+
56+
::
57+
58+
motion = MotionClient.from_robot(robot, "builtin")
59+
60+
my_command = {
61+
"cmnd": "dosomething",
62+
"someparameter": 52
63+
}
64+
65+
# Can be used with any resource, using the motion service as an example
66+
await motion.do_command(command=my_command)
4067
4168
Args:
4269
command (Dict[str, ValueTypes]): The command to execute

0 commit comments

Comments
 (0)