Skip to content

Update python documentation template #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ languages:
templates:
source:
type: openapi-git
git_committish: "v7.0.0" # git committish to checkout before extracting the templates
git_committish: "v7.7.0" # git committish to checkout before extracting the templates
templates_dir: python # directory with templates for this language
system: true
downstream_templates:
downstream-templates/python/README.md: README.md
downstream-templates/python/setup.py: setup.py
downstream-templates/python/utils.py: iot_api_client/utils.py
github_org_name: arduino
github_repo_name: iot-client-py
library_version: '1.3.5'
Expand Down
42 changes: 35 additions & 7 deletions downstream-templates/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,31 @@ oauth_client = BackendApplicationClient(client_id=YOUR_CLIENT_ID)
token_url = "https://api2.arduino.cc/iot/v1/clients/token"

oauth = OAuth2Session(client=oauth_client)
token = oauth.fetch_token(
token_url=token_url,
client_id=YOUR_CLIENT_ID,
client_secret=YOUR_CLIENT_SECRET,
include_client_id=True,
audience="https://api2.arduino.cc/iot"
)

print(token.get("access_token"))
```

In case of organization access, you can add organization identifier specifying required header:


```python

org_id="<org-id>"

token = oauth.fetch_token(
token_url=token_url,
client_id=YOUR_CLIENT_ID,
client_secret=YOUR_CLIENT_SECRET,
include_client_id=True,
audience="https://api2.arduino.cc/iot",
headers={"X-Organization":org_id}
)

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

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

# as an example, interact with the devices API
devices_api = iot.DevicesV2Api(client)
devices_api = deviceApi.DevicesV2Api(client)

try:
resp = devices_api.devices_v2_list()
print(resp)
devices = devices_api.devices_v2_list()
if devices.response.status==200:
for device in devices.body:
print("Device ("+device["id"]+"): "+device["name"])
except ApiException as e:
print("Got an exception: {}".format(e))
```

In case of organization access, you can specify organization identifier in this way:

```python
client = iot.ApiClient(client_config,header_name="X-Organization",header_value=org_id)
```

For a working example, see [the example folder](https://github.com/arduino/iot-client-py/tree/master/example/main.py) in this repo.

## How to get Arduino IoT Cloud Client Credentials

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):
You can generate Arduino IoT Cloud Client Credentials in `API Keys` section in the [IoT Cloud](https://app.arduino.cc/api-keys):

### Step 1

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

### Step 2

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

### Step 3

![IoT Cloud Site](https://github.com/arduino/iot-client-js/blob/master/img/selection_3.png?raw=true)
![IoT Cloud](img/api_step3.png)
32 changes: 32 additions & 0 deletions downstream-templates/python/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8

"""
Arduino IoT Cloud API

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

"""

import frozendict
from iot_api_client.schemas import *

class DecodeUtils(object):

def decode_value(self, value):
"""
Decode value from API provided DynamicSchema object to its supported subclass (like Decimal, dictionary, boolean, string)
"""
if issubclass(value.__class__, decimal.Decimal):
return decimal.Decimal(value)
if issubclass(value.__class__, str):
return str(value)
if issubclass(value.__class__, frozendict.frozendict):
dict = frozendict.frozendict(value)
decodedvals = {}
for key in dict.keys():
decodedvals[key] = self.decode_value(dict[key])
return decodedvals
if issubclass(value.__class__, BoolClass):
relval = value.is_true_oapg()
return relval
return value
Loading