in client/iot_client.py [0:0]
def init_thing_with_reg_api(self):
"""
This provisioning flow runs a local web server that waits for a registration code and then uses that
registration code to call a registration API that returns a certificate
:return:
Certificate
"""
global registration_code
while not self.certificate_pem:
if registration_code and registration_code != "INVALID":
print("Received registration code:")
print(registration_code)
post_body = {
"csr": self.csr.decode('utf-8'),
"regToken": registration_code,
"serialNumber": self.serial_number
}
headers = {
"Content-Type": "application/json"
}
cert_response = requests.post(os.environ['REG_API'] + "/api/certificate", data=json.dumps(post_body), headers=headers)
registration_code = None
if cert_response.status_code == 200 and 'certificate' in cert_response.json():
return cert_response.json()
else:
print("Error getting certificate, clearing registration code")
print(cert_response.text)
registration_code = "INVALID"
continue
else:
print("Waiting for registration code")
time.sleep(3)
continue