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