# Python 3.10+; standard library only. import os import urllib.request import urllib.error base = os.environ['PETROSUPPORT_BASE_URL'].rstrip('/') body = b'{"title":"Fleet roadside incident","priority":"high","requested_by":"11111111-1111-4111-a111-111111111111","latitude":-1.292100000000000026290081223123706877231597900390625,"longitude":36.821899999999999408828443847596645355224609375,"services":["recovery"],"external_reference":"FLEET-INC-001"}' request = urllib.request.Request(base + '/incidents', data=body, method='POST', headers={ 'Authorization': 'Bearer ' + os.environ['PETROSUPPORT_TOKEN'], 'Content-Type': 'application/json', 'Idempotency-Key': os.environ['PETROSUPPORT_IDEMPOTENCY_KEY'], }) class NoRedirect(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): return None opener = urllib.request.build_opener(NoRedirect) try: with opener.open(request, timeout=20) as response: print(response.read().decode()) except urllib.error.HTTPError as error: raise RuntimeError(f'HTTP {error.code}: {error.read().decode()}') from error