Skip to content

Commit efd6a1c

Browse files
authored
Update python documentation template (#90)
1 parent 5a8f8ed commit efd6a1c

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

config/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ languages:
103103
templates:
104104
source:
105105
type: openapi-git
106-
git_committish: "v7.0.0" # git committish to checkout before extracting the templates
106+
git_committish: "v7.7.0" # git committish to checkout before extracting the templates
107107
templates_dir: python # directory with templates for this language
108108
system: true
109109
downstream_templates:
110110
downstream-templates/python/README.md: README.md
111111
downstream-templates/python/setup.py: setup.py
112+
downstream-templates/python/utils.py: iot_api_client/utils.py
112113
github_org_name: arduino
113114
github_repo_name: iot-client-py
114115
library_version: '1.3.5'

downstream-templates/python/README.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,31 @@ oauth_client = BackendApplicationClient(client_id=YOUR_CLIENT_ID)
3434
token_url = "https://api2.arduino.cc/iot/v1/clients/token"
3535

3636
oauth = OAuth2Session(client=oauth_client)
37+
token = oauth.fetch_token(
38+
token_url=token_url,
39+
client_id=YOUR_CLIENT_ID,
40+
client_secret=YOUR_CLIENT_SECRET,
41+
include_client_id=True,
42+
audience="https://api2.arduino.cc/iot"
43+
)
44+
45+
print(token.get("access_token"))
46+
```
47+
48+
In case of organization access, you can add organization identifier specifying required header:
49+
50+
51+
```python
52+
53+
org_id="<org-id>"
54+
3755
token = oauth.fetch_token(
3856
token_url=token_url,
3957
client_id=YOUR_CLIENT_ID,
4058
client_secret=YOUR_CLIENT_SECRET,
4159
include_client_id=True,
4260
audience="https://api2.arduino.cc/iot",
61+
headers={"X-Organization":org_id}
4362
)
4463

4564
print(token.get("access_token"))
@@ -51,36 +70,45 @@ Once you get a token, you can create an instance of the iot-api client:
5170
import iot_api_client as iot
5271
from iot_api_client.rest import ApiException
5372
from iot_api_client.configuration import Configuration
73+
import iot_api_client.apis.tags.devices_v2_api as deviceApi
5474

5575
# configure and instance the API client
5676
client_config = Configuration(host="https://api2.arduino.cc/iot")
5777
client_config.access_token = YOUR_ACCESS_TOKEN
5878
client = iot.ApiClient(client_config)
5979

6080
# as an example, interact with the devices API
61-
devices_api = iot.DevicesV2Api(client)
81+
devices_api = deviceApi.DevicesV2Api(client)
6282

6383
try:
64-
resp = devices_api.devices_v2_list()
65-
print(resp)
84+
devices = devices_api.devices_v2_list()
85+
if devices.response.status==200:
86+
for device in devices.body:
87+
print("Device ("+device["id"]+"): "+device["name"])
6688
except ApiException as e:
6789
print("Got an exception: {}".format(e))
6890
```
6991

92+
In case of organization access, you can specify organization identifier in this way:
93+
94+
```python
95+
client = iot.ApiClient(client_config,header_name="X-Organization",header_value=org_id)
96+
```
97+
7098
For a working example, see [the example folder](https://github.com/arduino/iot-client-py/tree/master/example/main.py) in this repo.
7199

72100
## How to get Arduino IoT Cloud Client Credentials
73101

74-
You can generate Arduino IoT Cloud Client Credentials in the `ARDUINO API` section in the [IoT Cloud things section](https://create.arduino.cc/iot/things):
102+
You can generate Arduino IoT Cloud Client Credentials in `API Keys` section in the [IoT Cloud](https://app.arduino.cc/api-keys):
75103

76104
### Step 1
77105

78-
![IoT Cloud Site](https://github.com/arduino/iot-client-js/blob/master/img/selection_1.png?raw=true)
106+
![IoT Cloud](img/api_step1.png)
79107

80108
### Step 2
81109

82-
![IoT Cloud Site](https://github.com/arduino/iot-client-js/blob/master/img/selection_2.png?raw=true)
110+
![IoT Cloud](img/api_step2.png)
83111

84112
### Step 3
85113

86-
![IoT Cloud Site](https://github.com/arduino/iot-client-js/blob/master/img/selection_3.png?raw=true)
114+
![IoT Cloud](img/api_step3.png)

downstream-templates/python/utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding: utf-8
2+
3+
"""
4+
Arduino IoT Cloud API
5+
6+
Provides a set of endpoints to manage Arduino IoT Cloud **Devices**, **Things**, **Properties** and **Timeseries**. This API can be called just with any HTTP Client, or using one of these clients: * [Javascript NPM package](https://www.npmjs.com/package/@arduino/arduino-iot-client) * [Python PYPI Package](https://pypi.org/project/arduino-iot-client/) * [Golang Module](https://github.com/arduino/iot-client-go) # noqa: E501
7+
8+
"""
9+
10+
import frozendict
11+
from iot_api_client.schemas import *
12+
13+
class DecodeUtils(object):
14+
15+
def decode_value(self, value):
16+
"""
17+
Decode value from API provided DynamicSchema object to its supported subclass (like Decimal, dictionary, boolean, string)
18+
"""
19+
if issubclass(value.__class__, decimal.Decimal):
20+
return decimal.Decimal(value)
21+
if issubclass(value.__class__, str):
22+
return str(value)
23+
if issubclass(value.__class__, frozendict.frozendict):
24+
dict = frozendict.frozendict(value)
25+
decodedvals = {}
26+
for key in dict.keys():
27+
decodedvals[key] = self.decode_value(dict[key])
28+
return decodedvals
29+
if issubclass(value.__class__, BoolClass):
30+
relval = value.is_true_oapg()
31+
return relval
32+
return value

0 commit comments

Comments
 (0)