27
27
import json
28
28
import time
29
29
30
+ from google .api_core import retry
30
31
import jwt
31
32
import requests
32
33
33
-
34
34
_BASE_URL = 'https://cloudiot-device.googleapis.com/v1beta1'
35
+ _BACKOFF_DURATION = 60
35
36
36
37
37
38
def create_jwt (project_id , private_key_file , algorithm ):
@@ -54,6 +55,9 @@ def create_jwt(project_id, private_key_file, algorithm):
54
55
return jwt .encode (token , private_key , algorithm = algorithm ).decode ('ascii' )
55
56
56
57
58
+ @retry .Retry (
59
+ predicate = retry .if_exception_type (AssertionError ),
60
+ deadline = _BACKOFF_DURATION )
57
61
def publish_message (
58
62
message , message_type , base_url , project_id , cloud_region , registry_id ,
59
63
device_id , jwt_token ):
@@ -83,9 +87,16 @@ def publish_message(
83
87
resp = requests .post (
84
88
publish_url , data = json .dumps (body ), headers = headers )
85
89
90
+ if (resp .status_code != 200 ):
91
+ print ('Response came back {}, retrying' .format (resp .status_code ))
92
+ raise AssertionError ('Not OK response: {}' .format (resp .status_code ))
93
+
86
94
return resp
87
95
88
96
97
+ @retry .Retry (
98
+ predicate = retry .if_exception_type (AssertionError ),
99
+ deadline = _BACKOFF_DURATION )
89
100
def get_config (
90
101
version , message_type , base_url , project_id , cloud_region , registry_id ,
91
102
device_id , jwt_token ):
@@ -102,6 +113,10 @@ def get_config(
102
113
103
114
resp = requests .get (config_url , headers = headers )
104
115
116
+ if (resp .status_code != 200 ):
117
+ print ('Error getting config: {}, retrying' .format (resp .status_code ))
118
+ raise AssertionError ('Not OK response: {}' .format (resp .status_code ))
119
+
105
120
return resp
106
121
107
122
0 commit comments