"""Python standard-library starter. Keep keys in your environment."""
import json,os,urllib.request,urllib.error
key=os.environ.get('LOCATIONIQ_KEY','')
mode=os.environ.get('LOCATIONIQ_ENV','sandbox')
if mode not in ('sandbox','production'):raise SystemExit('LOCATIONIQ_ENV must be sandbox or production')
if not key.startswith('tz_test_' if mode=='sandbox' else 'tz_live_'):raise SystemExit('Set LOCATIONIQ_KEY to a key for the selected environment')
class NoRedirect(urllib.request.HTTPRedirectHandler):
 def redirect_request(self,*args,**kwargs):return None
path='/sandbox/v1/reverse' if mode=='sandbox' else '/v1/reverse'
request=urllib.request.Request('https://locationiq.co.tz'+path+'?latitude=-6.81&longitude=39.28',headers={'X-API-Key':key,'Accept':'application/json'})
try:
 with urllib.request.build_opener(NoRedirect()).open(request,timeout=20) as response:print(json.dumps(json.load(response),indent=2))
except urllib.error.HTTPError as error:
 print('HTTP',error.code)
 try:print(json.loads(error.read(8192)).get('detail','Request failed'))
 except Exception:print('Request failed; check your environment, allowance and production IP approval.')
 raise SystemExit(1)
