diff --git a/config/config.yaml b/config/config.yaml index 5403c1d..1a2b1cd 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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' 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) 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