Skip to content

Commit 512dd2e

Browse files
sky-sharmarajasd27
andauthored
Develop (#29)
* ListDevicesRequest >> _prepare_params_for_list: (#16) modified to handle gatewayListOptions following NodeJS SDK * updated device class and enums (#19) * updated send_command function to base64 encode payload (#18) * Changed 'cloud_ack_time' to 'cloud_update_time' (#20) * Iot 918 conform to google format times binary data (#24) * 1. imports incl. google.api_core.datetime_helpers 2. Added logic to def from_json in class DeviceState and DeviceConfig to convert times to DateTimeWithNanoseconds and binaryData to bytes if env. vars set * 1. Changed Device.from_json to support GCP types for time, binaryData. 2. Changed DeviceState.from_json to handle blank binaryData 3. Changed DeviceConfig.from_json to handle blank binaryData * Removed extra 'import base64' * Added note about types of times, binaryData * Cleaned up formatting * Further cleanup * Added copyright info. at top of files * Not to 'pip install google-api-core' * Removed instructions for installing datetime_helpers module. Also now changed 'google.api_core' to 'proto' * Added "proto.datetime_helpers" to dependencies * Changed 'google.api_core' to 'proto.' * Removed call to set 'convert_binarydata_to_bytes' in Devices.from_json * Changed README to reflect decision on ONE env. var * Changed module to import to proto-plus * Two env. vars -> BINARYDATA_AND_TIME_GOOGLE_FORMAT * Removed a '.' * Format improvements for README * Added note about new env. var in 'Quick Start' sec * 'config', 'state' None? (i.e. not in fieldMask?) (#26) * 'config', 'state' None? (i.e. not in fieldMask?) * Added new @Property's to address DESK-2174 * Iot 994 add credential classes (#28) * Made PublicKeyFormat a child class of Enum * 1. import PublicKeyFormat from .resources 2. New dodict class for providing dot notation for dicts when read. 3. Call to dodict in credentials getter. 4. New functions for converting PublicKeyFormat to/from string. 5. Calls to PublicKeyFormat conversion functions. * Added note about how to run from source * Formatting * More formatting * Formatting * Formatting * Clearer info. on running from source * 1. Import PublicKeyCredential & DeviceCredential 2. Remove dotdict class. 3. Remove convertCredentialsFormatToString. 4. Set self._credentials=convertCredentialsFormatsToString(credentials) 5. Simplified Device class' credentials getter. 6. Changed Device class attr from 'credentials' to '_credentials' * Added code to _create_device_body to: 1. Convert DeviceCredential and PublicKeyCredential objects to dicts 2. Convert PublicKeyFormat class to string This is prior to creating device in registry. * Add PublicKeyCredential & DeviceCredential classes * Changed PublicKeyCredential constructor params: publicKeyFormat -> format publicKey -> key * 1. bug: DeviceCredential constructor calls PublicKeyCredential constructor where params were reversed. 2. Added classmethod convert_credentials_for_create_update. * 1. Removed unnecessary var from convertCredentialsFormatToString. 2. Calling convert_credentials_for_create_update in _prepare_params_body_for_update fcn * 1. Import DeviceCredential 2. Remove code for converting credentials from _create_device_body and call DeviceCredential.convert_credentials_for_create_update instead * 1. Removed the config and state conversion code from class Device. 2. In Device.from_json returned config as DeviceConfig.from_json() and state as DeviceState.from_json() 3. In DeviceState replace attr. self._update_time w/ self.updateTime and self._binary_data w/ self.binaryData 4. Added __getitem__ and get functions. * In classes PublicKeyCredential & DeviceCredential added function "get" which does the same thing as __getitem__ * Document changes from last version in UPGRADING.md * Turned ClearBlade and Google License info. into comments * Formatting changes * In 'convert_credentials_for_create_update' check for expirationTime. If it is datetime convert to ISO string * Formatting * Formatting * Formatting * Formatting * Formatting * Prefixed license info. * bug: if expirationTime type 'datetime', isoformat won't work: 'Z' suffix is left off and is needed by CB platform. Resolved by replacing isoformat with strftime. --------- Co-authored-by: rajasd27 <rajasdeshpande27@gmail.com>
1 parent 12a2cfe commit 512dd2e

15 files changed

Lines changed: 804 additions & 215 deletions

README.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,20 @@ Note about types of times and binaryData
116116
1. All times: **DatetimeWithNanoseconds** (defined in the **proto.datetime_helpers** module)
117117
2. All **binaryData** (CONFIG, STATE etc.): **BYTE ARRAYS**
118118

119-
- If this environment variable is not set, or is set to any unexpeced values, then the default types listed previously are used.
119+
- If this environment variable is not set, or is set to any unexpeced values, then the default types listed previously are used.
120+
121+
Note about running from source instead of PyPi (pip) module:
122+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123+
- To temporarily use the source code in this repo. instead of the installed PyPi (pip) module do the following:
124+
125+
1. Clone this repo.
126+
2. Checkout the desired branch using **git checkout <branch>**.
127+
3. In your code find where **clearblade** or **clearblade.cloud** is being imported.
128+
4. Precede that line with **import sys** and **sys.path.insert(0, <path_to_python-iot>)**. The path must end with "python-iot". So for example:
129+
130+
.. code-block:: console
131+
132+
import sys
133+
sys.path.insert(0, "path/to/python-iot")
134+
135+
from clearblade.cloud import iot_v1

UPGRADING.md

Lines changed: 113 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,151 @@
1-
# 2.0.0 Migration Guide
2-
3-
The 2.0 release of the `google-cloud-iot` client is a significant upgrade based on a [next-gen code generator](https://github.com/googleapis/gapic-generator-python), and includes substantial interface changes. Existing code written for earlier versions of this library will likely require updates to use this version. This document describes the changes that have been made, and what you need to do to update your usage.
4-
5-
If you experience issues or have questions, please file an [issue](https://github.com/googleapis/python-iot/issues).
6-
7-
## Supported Python Versions
8-
9-
> **WARNING**: Breaking change
10-
11-
The 2.0.0 release requires Python 3.6+.
12-
1+
<!-- "Copyright 2023 ClearBlade Inc."
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
https://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
Copyright 2018 Google LLC
13+
Licensed under the Apache License, Version 2.0 (the "License");
14+
you may not use this file except in compliance with the License.
15+
You may obtain a copy of the License at
16+
https://www.apache.org/licenses/LICENSE-2.0
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
Copyright 2023 ClearBlade Inc.
23+
Licensed under the Apache License, Version 2.0 (the "License");
24+
you may not use this file except in compliance with the License.
25+
You may obtain a copy of the License at
26+
https://www.apache.org/licenses/LICENSE-2.0
27+
Unless required by applicable law or agreed to in writing, software
28+
distributed under the License is distributed on an "AS IS" BASIS,
29+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30+
See the License for the specific language governing permissions and
31+
limitations under the License.
32+
Copyright 2018 Google LLC
33+
Licensed under the Apache License, Version 2.0 (the "License");
34+
you may not use this file except in compliance with the License.
35+
You may obtain a copy of the License at
36+
https://www.apache.org/licenses/LICENSE-2.0
37+
Unless required by applicable law or agreed to in writing, software
38+
distributed under the License is distributed on an "AS IS" BASIS,
39+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40+
See the License for the specific language governing permissions and
41+
limitations under the License. -->
1342

14-
## Method Calls
15-
16-
> **WARNING**: Breaking change
17-
18-
Methods expect request objects. We provide a script that will convert most common use cases.
19-
20-
* Install the library with `libcst`.
21-
22-
```py
23-
python3 -m pip install google-cloud-iot[libcst]
24-
```
25-
26-
* The script `fixup_iot_v1_keywords.py` is shipped with the library. It expects
27-
an input directory (with the code to convert) and an empty destination directory.
43+
# 2.0.0 Migration Guide
2844

29-
```sh
30-
$ fixup_iot_v1_keywords.py --input-directory .samples/ --output-directory samples/
31-
```
45+
The 2.0 release of the `clearblade-cloud-iot` client is a significant upgrade based on addition of two new classes in **iot_v1**:
3246

33-
**Before:**
34-
```py
35-
from google.cloud import iot_v1
47+
- **DeviceCredential**
48+
- **PublicKeyCredential**
3649

37-
client = iot_v1.DeviceManagerClient()
50+
The release also includes enhancements to these classes already present in **iot_v1**:
3851

39-
registry = client.get_device_registry("registry_name")
40-
```
52+
- **DeviceConfig**
53+
- **DeviceState**
4154

55+
The version was made with the intent of minimizing required code changes. **However these changes should be considrered Breaking changes**.
4256

43-
**After:**
44-
```py
45-
from google.cloud import iot_v1
57+
#
4658

47-
client = iot_v1.DeviceManagerClient()
59+
1. If **device** is an object of class **Device**.
4860

49-
registry = client.get_device_registry(request={'name': "registry_name"})
50-
```
61+
**Before**:
62+
device.credentials is of type **[dict]** (i.e. list of dicts).
5163

52-
### More Details
64+
**After**:
65+
device.credentials is of type **[DeviceCredential]** (i.e. list of objects of class DeviceCredential).
5366

54-
In `google-cloud-iot<2.0.0`, parameters required by the API were positional parameters and optional parameters were keyword parameters.
67+
The **DeviceCredential** class has these features for usability:
5568

56-
**Before:**
57-
```py
58-
def create_device(
59-
self,
60-
parent,
61-
device,
62-
retry=google.api_core.gapic_v1.method.DEFAULT,
63-
timeout=google.api_core.gapic_v1.method.DEFAULT,
64-
metadata=None,
65-
):
66-
```
69+
- A **get** method that mimics the **get** method of a dict.
70+
- Allows accessing attributes using dot notation OR square-brackets.
71+
- Supports camel-case as well as snake-case for accessing attributes:
6772

68-
In the 2.0.0 release, all methods have a single positional parameter `request`. Method docstrings indicate whether a parameter is required or optional.
73+
e.g. All these are valid for retrieving the public key:
6974

70-
Some methods have additional keyword only parameters. The available parameters depend on the `google.api.method_signature` annotation specified by the API producer.
75+
- **public_key = device.credentials[0]['publicKey']**
76+
- **public_key = device.credentials[0]['public_key']**
77+
- **public_key = device.credentials[0].get('publicKey')**
78+
- **public_key = device.credentials[0].get('public_key')**
79+
- **public_key = device.credentials[0].publicKey**
80+
- **public_key = device.credentials[0].public_key**
7181

82+
#
7283

73-
**After:**
74-
```py
75-
def create_device(
76-
self,
77-
request: device_manager.CreateDeviceRequest = None,
78-
*,
79-
parent: str = None,
80-
device: resources.Device = None,
81-
retry: retries.Retry = gapic_v1.method.DEFAULT,
82-
timeout: float = None,
83-
metadata: Sequence[Tuple[str, str]] = (),
84-
) -> resources.Device:
85-
```
84+
2. This refers to pub_key mentioned in the previous section.
8685

87-
> **NOTE:** The `request` parameter and flattened keyword parameters for the API are mutually exclusive.
88-
> Passing both will result in an error.
86+
**Before**:
87+
public_key was of type **dict**.
8988

89+
**After**:
90+
public_key is an object of class **PublicKeyCredential**.
9091

91-
Both of these calls are valid:
92+
The **PublicKeyCredential** class has these features for usability:
9293

93-
```py
94-
response = client.create_device(
95-
request={
96-
"parent": parent,
97-
"device": device,
98-
}
99-
)
100-
```
94+
- A **get** method that mimics the **get** method of a dict.
95+
- Allows accessing attributes using dot notation OR square-brackets.
10196

102-
```py
103-
response = client.create_device(
104-
parent=parent,
105-
device=device,
106-
)
107-
```
97+
e.g. All these are valid for retrieving the public key format:
10898

109-
This call is invalid because it mixes `request` with a keyword argument `device`. Executing this code
110-
will result in an error.
99+
- **format = public_key['format']**
100+
- **format = public_key.get('format')**
101+
- **format = public_key.format**
111102

112-
```py
113-
response = client.create_device(
114-
request={
115-
"parent": parent,
116-
},
117-
device=device
118-
)
119-
```
103+
#
120104

105+
3. This section refers to **dev_config** which holds device config.
121106

107+
**Before**:
108+
dev_config is of type **dict**.
122109

123-
## Enums and Types
110+
**After**:
111+
dev_config is an object of class **DeviceConfig**.
124112

113+
The **DeviceConfig** class has these features for usability:
125114

126-
> **WARNING**: Breaking change
115+
- A **get** method that mimics the **get** method of a dict.
116+
- Allows accessing attributes using dot notation OR square-brackets.
117+
- Supports camel-case as well as snake-case for accessing attributes:
127118

128-
The submodules `enums` and `types` have been removed.
119+
e.g. All these are valid for retrieving the cloud_update_time:
129120

130-
**Before:**
131-
```py
132-
from google.cloud import iot_v1
121+
- **cloud_update_time = device.credentials[0]['cloudUpdateTime']**
122+
- **cloud_update_time = device.credentials[0]['cloud_update_time']**
123+
- **cloud_update_time = device.credentials[0].get('cloudUpdateTime')**
124+
- **cloud_update_time = device.credentials[0].get('cloud_update_time')**
125+
- **cloud_update_time = device.credentials[0].cloudUpdateTime**
126+
- **cloud_update_time = device.credentials[0].cloud_update_time**
133127

134-
gateway_type = iot_v1.enums.GatewayType.GATEWAY
135-
device = iot_v1.types.Device(name="name")
136-
```
128+
#
137129

130+
4. This section refers to **dev_state** which contains device state.
138131

139-
**After:**
140-
```py
141-
from google.cloud import iot_v1
132+
**Before**:
133+
dev_state is of type **dict**.
142134

143-
gateway_type = iot_v1.GatewayType.GATEWAY
144-
device = iot_v1.Device(name="name")
145-
```
135+
**After**:
136+
dev_state is an object of class **DeviceState**.
146137

147-
## Location Path Helper Method
138+
The **DeviceState** class has these features for usability:
148139

149-
Location path helper method has been removed. Please construct
150-
the path manually.
140+
- A **get** method that mimics the **get** method of a dict.
141+
- Allows accessing attributes using dot notation OR square-brackets.
142+
- Supports camel-case as well as snake-case for accessing attributes:
151143

152-
```py
153-
project = 'my-project'
154-
location = 'location'
144+
e.g. All these are valid for retrieving the binary_data:
155145

156-
location_path = f'projects/{project}/locations/{location}'
157-
```
146+
- **binary_data = device.credentials[0]['binaryData']**
147+
- **binary_data = device.credentials[0]['binary_data']**
148+
- **binary_data = device.credentials[0].get('binaryData')**
149+
- **binary_data = device.credentials[0].get('binary_data')**
150+
- **binary_data = device.credentials[0].binaryData**
151+
- **binary_data = device.credentials[0].binary_data**

clearblade/cloud/iot_v1/__init__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
"""
2+
"Copyright 2023 ClearBlade Inc."
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
https://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
Copyright 2018 Google LLC
14+
Licensed under the Apache License, Version 2.0 (the "License");
15+
you may not use this file except in compliance with the License.
16+
You may obtain a copy of the License at
17+
https://www.apache.org/licenses/LICENSE-2.0
18+
Unless required by applicable law or agreed to in writing, software
19+
distributed under the License is distributed on an "AS IS" BASIS,
20+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
See the License for the specific language governing permissions and
22+
limitations under the License.
23+
Copyright 2023 ClearBlade Inc.
24+
Licensed under the Apache License, Version 2.0 (the "License");
25+
you may not use this file except in compliance with the License.
26+
You may obtain a copy of the License at
27+
https://www.apache.org/licenses/LICENSE-2.0
28+
Unless required by applicable law or agreed to in writing, software
29+
distributed under the License is distributed on an "AS IS" BASIS,
30+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31+
See the License for the specific language governing permissions and
32+
limitations under the License.
33+
Copyright 2018 Google LLC
34+
Licensed under the Apache License, Version 2.0 (the "License");
35+
you may not use this file except in compliance with the License.
36+
You may obtain a copy of the License at
37+
https://www.apache.org/licenses/LICENSE-2.0
38+
Unless required by applicable law or agreed to in writing, software
39+
distributed under the License is distributed on an "AS IS" BASIS,
40+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41+
See the License for the specific language governing permissions and
42+
limitations under the License.
43+
"""
44+
145
from .client import DeviceManagerClient, DeviceManagerAsyncClient
246
from .device_types import *
347
from .registry_types import *

clearblade/cloud/iot_v1/client.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
"""
2+
"Copyright 2023 ClearBlade Inc."
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
https://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
Copyright 2018 Google LLC
14+
Licensed under the Apache License, Version 2.0 (the "License");
15+
you may not use this file except in compliance with the License.
16+
You may obtain a copy of the License at
17+
https://www.apache.org/licenses/LICENSE-2.0
18+
Unless required by applicable law or agreed to in writing, software
19+
distributed under the License is distributed on an "AS IS" BASIS,
20+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
See the License for the specific language governing permissions and
22+
limitations under the License.
23+
Copyright 2023 ClearBlade Inc.
24+
Licensed under the Apache License, Version 2.0 (the "License");
25+
you may not use this file except in compliance with the License.
26+
You may obtain a copy of the License at
27+
https://www.apache.org/licenses/LICENSE-2.0
28+
Unless required by applicable law or agreed to in writing, software
29+
distributed under the License is distributed on an "AS IS" BASIS,
30+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31+
See the License for the specific language governing permissions and
32+
limitations under the License.
33+
Copyright 2018 Google LLC
34+
Licensed under the Apache License, Version 2.0 (the "License");
35+
you may not use this file except in compliance with the License.
36+
You may obtain a copy of the License at
37+
https://www.apache.org/licenses/LICENSE-2.0
38+
Unless required by applicable law or agreed to in writing, software
39+
distributed under the License is distributed on an "AS IS" BASIS,
40+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41+
See the License for the specific language governing permissions and
42+
limitations under the License.
43+
"""
44+
145
from .device_types import *
246
from .devices import *
347
from .registry import *

0 commit comments

Comments
 (0)