From ac91a0d18369a04a2a02a58d768ddc149155fe34 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Tue, 30 Jul 2024 11:17:17 +0200 Subject: [PATCH 1/5] Update python documentation template --- downstream-templates/python/README.md | 42 ++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/downstream-templates/python/README.md b/downstream-templates/python/README.md index 9e43a84..afe94b8 100644 --- a/downstream-templates/python/README.md +++ b/downstream-templates/python/README.md @@ -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="" + 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")) @@ -51,6 +70,7 @@ 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") @@ -58,29 +78,37 @@ 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) \ No newline at end of file +![IoT Cloud](img/api_step3.png) From 45668c0b401a6e2a454e2365a77fc030ea12a694 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 2 Aug 2024 09:15:28 +0200 Subject: [PATCH 2/5] update generator --- config/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.yaml b/config/config.yaml index 5403c1d..ab9c47e 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -22,7 +22,7 @@ languages: - template-patches/go-sum.patch 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: go # directory with templates for this language system: true downstream_templates: From 486b3f2af6acb305aaaf5e17e104c9721b6ebecf Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 2 Aug 2024 09:20:31 +0200 Subject: [PATCH 3/5] Revert "update generator" This reverts commit 45668c0b401a6e2a454e2365a77fc030ea12a694. --- config/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.yaml b/config/config.yaml index ab9c47e..5403c1d 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -22,7 +22,7 @@ languages: - template-patches/go-sum.patch source: type: openapi-git - git_committish: "v7.7.0" # git committish to checkout before extracting the templates + git_committish: "v7.0.0" # git committish to checkout before extracting the templates templates_dir: go # directory with templates for this language system: true downstream_templates: From ee83ac83d9590cb4c126963fc9445241f44464ab Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 2 Aug 2024 09:21:07 +0200 Subject: [PATCH 4/5] update generator --- config/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.yaml b/config/config.yaml index 5403c1d..a81e249 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -103,7 +103,7 @@ 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: From cb96daa431386138db4789941a6f7f38f5d649ad Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 2 Aug 2024 09:46:16 +0200 Subject: [PATCH 5/5] added new downstream template with utility class --- config/config.yaml | 1 + downstream-templates/python/utils.py | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 downstream-templates/python/utils.py diff --git a/config/config.yaml b/config/config.yaml index a81e249..1a2b1cd 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -109,6 +109,7 @@ languages: 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' diff --git a/downstream-templates/python/utils.py b/downstream-templates/python/utils.py new file mode 100644 index 0000000..acc8116 --- /dev/null +++ b/downstream-templates/python/utils.py @@ -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