This repository has been archived on 2022-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
pyurbantz/urbantz/delivery.py

116 lines
3.1 KiB
Python
Raw Normal View History

2018-10-20 16:57:13 +00:00
from datetime import datetime
from urbantz.utils import Coordinates
from urbantz.exceptions import APIError
2019-01-30 07:03:13 +00:00
import json
2018-10-20 16:57:13 +00:00
import requests
class Delivery(object):
"""
A UrbanTZ delivery with a unique ID.
"""
2019-01-30 07:03:13 +00:00
def __init__(self, tracking_code=None):
2018-10-25 17:10:49 +00:00
"""
2019-01-30 07:03:13 +00:00
:param tracking_code: A delivery public tracking code.
:type tracking_code: str or None
2018-10-25 17:10:49 +00:00
"""
2019-01-30 07:03:13 +00:00
self.tracking_code = tracking_code
2018-10-25 17:10:49 +00:00
"""
2019-01-30 07:03:13 +00:00
The delivery public tracking code.
2018-10-25 17:10:49 +00:00
2019-01-30 07:03:13 +00:00
:type: str or None
2018-10-25 17:10:49 +00:00
"""
2018-10-20 16:57:13 +00:00
self.last_updated = None
2018-10-25 17:10:49 +00:00
"""
Last API update date/time. Is None if data has never been fetched
from the API.
:type: datetime or None
"""
2019-01-30 07:03:13 +00:00
self.payload = None
"""
Latest parsed JSON payload from the API. Is None if data has never
been fetched from the API or loaded via :meth:`Delivery.use`.
:type: dict or None
"""
2018-10-20 16:57:13 +00:00
self.position = None
2018-10-25 17:10:49 +00:00
"""
Coordinates of the delivery truck's position.
:type: urbantz.utils.Coordinates
"""
2018-10-20 16:57:13 +00:00
self.destination = None
2018-10-25 17:10:49 +00:00
"""
Coordinates of the delivery destination.
:type: urbantz.utils.Coordinates
"""
2018-10-20 16:57:13 +00:00
def __repr__(self):
2019-01-30 07:05:20 +00:00
return '{}({})'.format(
self.__class__.__name__, repr(self.tracking_code))
2018-10-20 16:57:13 +00:00
@property
def api_url(self):
2018-10-25 17:10:49 +00:00
"""
URL pointing to the API endpoint to use for the specific delivery.
:type: str
"""
2019-01-30 07:05:20 +00:00
return 'https://backend.urbantz.com/public/task/tracking/{}'.format(
self.tracking_code)
2018-10-20 16:57:13 +00:00
def update(self):
2018-10-25 17:10:49 +00:00
"""
Fetch the latest delivery information from the API.
:raises requests.exceptions.HTTPError: If the response has an
HTTP 4xx or 5xx code.
:raises urbantz.exceptions.APIError: If the API returned an error.
"""
2018-10-20 16:57:13 +00:00
resp = requests.get(self.api_url)
resp.raise_for_status()
data = resp.json()
if 'error' in data:
2019-01-30 07:03:13 +00:00
raise APIError(self.payload['error'])
2018-10-20 16:57:13 +00:00
2019-01-30 07:03:13 +00:00
self.use(data)
2018-10-20 16:57:13 +00:00
2019-01-30 07:03:13 +00:00
# TODO: See if the payload holds a last update value
2018-10-20 16:57:13 +00:00
self.last_updated = datetime.now()
2019-01-30 07:03:13 +00:00
def use(self, payload):
"""
Use a parsed JSON payload to update the properties.
:param dict payload: A parsed JSON payload from the API.
"""
self.payload = payload
self.position = Coordinates.fromJSON(self.payload['position'])
self.destination = Coordinates.fromJSON(
self.payload['location']['location']['geometry'])
@classmethod
def from_payload(cls, payload):
"""
Create a Delivery instance from an existing payload.
:param payload: A parsed JSON payload, a str holding a JSON payload,
or an open file-like object.
:type payload: dict or str or file-like object
"""
if isinstance(payload, str):
payload = json.loads(payload)
if not isinstance(payload, dict):
payload = json.load(payload)
instance = cls()
instance.use(payload)
return instance