web: Log warning and return None on errors

This commit is contained in:
Teemu Ikonen
2022-10-25 13:07:29 +03:00
parent 46a5105eb0
commit 44c212d65b

View File

@@ -1,34 +1,37 @@
import aiohttp
import asyncio
import gpxpy
import logging
import os.path
import sys
import yaml
from datetime import datetime
log = logging.getLogger(__name__)
class Locator(object):
def __init__(self, url, apikey=None):
self.api = url + ('?key=' + apikey) if apikey is not None else ''
def __init__(self, locateurl, apikey=None):
self.api = locateurl + ('?key=' + apikey) if apikey is not None else ''
async def locate(self, observation, **kwargs):
jd = observation.copy()
jd.pop('time', None)
async with aiohttp.ClientSession() as session:
async with session.post(self.api, json=jd) as response:
try:
async with session.post(self.api, json=jd) as response:
if (response.status != 200
or response.headers['content-type'] !=
'application/json'):
raise ValueError("Did not get JSON")
# print("Status:", response.status)
# print("Content-type:", response.headers['content-type'])
if (response.status != 200
or response.headers['content-type'] !=
'application/json'):
raise ValueError("Did not get JSON") # FIXME: Other exc
outd = await response.json()
print("Body:", str(outd))
outd = await response.json()
except Exception as e:
log.warning("Failed get location: " + str(e))
return None, None
loc = outd.get('location', {})
latlon = loc.get('lat'), loc.get('lng')