| 1 |
from datetime import datetime |
| 2 |
from enum import Enum |
| 3 |
from typing import Optional, List, Any, Dict |
| 4 |
|
| 5 |
from requests import Response |
| 6 |
|
| 7 |
|
| 8 |
class Price(object): |
| 9 |
def __init__(self, fuel_type: str, price: float, |
| 10 |
last_updated: Optional[datetime], price_unit: Optional[str], |
| 11 |
station_code: Optional[int]) -> None: |
| 12 |
self.fuel_type = fuel_type |
| 13 |
self.price = price |
| 14 |
self.last_updated = last_updated |
| 15 |
self.price_unit = price_unit |
| 16 |
self.station_code = station_code |
| 17 |
|
| 18 |
@classmethod |
| 19 |
def deserialize(cls, data: Dict[str, Any]) -> 'Price': |
| 20 |
|
| 21 |
# Stupid API has two different date representations! :O |
| 22 |
lastupdated = None |
| 23 |
try: |
| 24 |
lastupdated = datetime.strptime( |
| 25 |
data['lastupdated'], '%d/%m/%Y %H:%M:%S') |
| 26 |
except ValueError: |
| 27 |
pass |
| 28 |
try: |
| 29 |
lastupdated = datetime.strptime( |
| 30 |
data['lastupdated'], '%Y-%m-%d %H:%M:%S') |
| 31 |
except ValueError: |
| 32 |
pass |
| 33 |
|
| 34 |
station_code = None # type: Optional[int] |
| 35 |
if 'stationcode' in data: |
| 36 |
station_code = int(data['stationcode']) |
| 37 |
|
| 38 |
return Price( |
| 39 |
fuel_type=data['fueltype'], |
| 40 |
price=data['price'], |
| 41 |
last_updated=lastupdated, |
| 42 |
price_unit=data.get('priceunit'), |
| 43 |
station_code=station_code |
| 44 |
) |
| 45 |
|
| 46 |
def __repr__(self) -> str: |
| 47 |
return '<Price fuel_type={} price={}>'.format( |
| 48 |
self.fuel_type, self.price) |
| 49 |
|
| 50 |
|
| 51 |
class Station(object): |
| 52 |
def __init__(self, id: Optional[str], brand: str, code: int, |
| 53 |
name: str, address: str) -> None: |
| 54 |
self.id = id |
| 55 |
self.brand = brand |
| 56 |
self.code = code |
| 57 |
self.name = name |
| 58 |
self.address = address |
| 59 |
|
| 60 |
@classmethod |
| 61 |
def deserialize(cls, data: Dict[str, Any]) -> 'Station': |
| 62 |
return Station( |
| 63 |
id=data.get('stationid'), |
| 64 |
brand=data['brand'], |
| 65 |
code=int(data['code']), |
| 66 |
name=data['name'], |
| 67 |
address=data['address'] |
| 68 |
) |
| 69 |
|
| 70 |
def __repr__(self) -> str: |
| 71 |
return '<Station id={} code={} brand={} name={}>'.format( |
| 72 |
self.id, self.code, self.brand, self.name) |
| 73 |
|
| 74 |
|
| 75 |
class Period(Enum): |
| 76 |
DAY = 'Day' |
| 77 |
MONTH = 'Month' |
| 78 |
YEAR = 'Year' |
| 79 |
WEEK = 'Week' |
| 80 |
|
| 81 |
|
| 82 |
class Variance(object): |
| 83 |
def __init__(self, fuel_type: str, period: Period, price: float) -> None: |
| 84 |
self.fuel_type = fuel_type |
| 85 |
self.period = period |
| 86 |
self.price = price |
| 87 |
|
| 88 |
@classmethod |
| 89 |
def deserialize(cls, data: Dict[str, Any]) -> 'Variance': |
| 90 |
return Variance( |
| 91 |
fuel_type=data['Code'], |
| 92 |
period=Period(data['Period']), |
| 93 |
price=data['Price'], |
| 94 |
) |
| 95 |
|
| 96 |
def __repr__(self) -> str: |
| 97 |
return '<Variance fuel_type={} period={} price={}>'.format( |
| 98 |
self.fuel_type, |
| 99 |
self.period, |
| 100 |
self.price, |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
class AveragePrice(object): |
| 105 |
def __init__(self, fuel_type: str, period: Period, price: float, |
| 106 |
captured: datetime) -> None: |
| 107 |
self.fuel_type = fuel_type |
| 108 |
self.period = period |
| 109 |
self.price = price |
| 110 |
self.captured = captured |
| 111 |
|
| 112 |
@classmethod |
| 113 |
def deserialize(cls, data: Dict[str, Any]) -> 'AveragePrice': |
| 114 |
period = Period(data['Period']) |
| 115 |
|
| 116 |
captured_raw = data['Captured'] |
| 117 |
if period in [Period.DAY, Period.WEEK, Period.MONTH]: |
| 118 |
captured = datetime.strptime(captured_raw, '%Y-%m-%d') |
| 119 |
elif period == Period.YEAR: |
| 120 |
captured = datetime.strptime(captured_raw, '%B %Y') |
| 121 |
else: |
| 122 |
captured = captured_raw |
| 123 |
|
| 124 |
return AveragePrice( |
| 125 |
fuel_type=data['Code'], |
| 126 |
period=period, |
| 127 |
price=data['Price'], |
| 128 |
captured=captured, |
| 129 |
) |
| 130 |
|
| 131 |
def __repr__(self) -> str: |
| 132 |
return ('<AveragePrice fuel_type={} period={} price={} ' |
| 133 |
'captured={}>').format( |
| 134 |
self.fuel_type, |
| 135 |
self.period, |
| 136 |
self.price, |
| 137 |
self.captured |
| 138 |
) |
| 139 |
|
| 140 |
|
| 141 |
class FuelType(object): |
| 142 |
def __init__(self, code: str, name: str) -> None: |
| 143 |
self.code = code |
| 144 |
self.name = name |
| 145 |
|
| 146 |
@classmethod |
| 147 |
def deserialize(cls, data: Dict[str, Any]) -> 'FuelType': |
| 148 |
return FuelType( |
| 149 |
code=data['code'], |
| 150 |
name=data['name'] |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
class TrendPeriod(object): |
| 155 |
def __init__(self, period: str, description: str) -> None: |
| 156 |
self.period = period |
| 157 |
self.description = description |
| 158 |
|
| 159 |
@classmethod |
| 160 |
def deserialize(cls, data: Dict[str, Any]) -> 'TrendPeriod': |
| 161 |
return TrendPeriod( |
| 162 |
period=data['period'], |
| 163 |
description=data['description'] |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
class SortField(object): |
| 168 |
def __init__(self, code: str, name: str) -> None: |
| 169 |
self.code = code |
| 170 |
self.name = name |
| 171 |
|
| 172 |
@classmethod |
| 173 |
def deserialize(cls, data: Dict[str, Any]) -> 'SortField': |
| 174 |
return SortField( |
| 175 |
code=data['code'], |
| 176 |
name=data['name'] |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
class GetReferenceDataResponse(object): |
| 181 |
def __init__(self, stations: List[Station], brands: List[str], |
| 182 |
fuel_types: List[FuelType], trend_periods: List[TrendPeriod], |
| 183 |
sort_fields: List[SortField]) -> None: |
| 184 |
self.stations = stations |
| 185 |
self.brands = brands |
| 186 |
self.fuel_types = fuel_types |
| 187 |
self.trend_periods = trend_periods |
| 188 |
self.sort_fields = sort_fields |
| 189 |
|
| 190 |
@classmethod |
| 191 |
def deserialize(cls, data: Dict[str, Any]) -> 'GetReferenceDataResponse': |
| 192 |
stations = [Station.deserialize(x) for x in data['stations']['items']] |
| 193 |
brands = [x['name'] for x in data['brands']['items']] |
| 194 |
fuel_types = [FuelType.deserialize(x) for x in |
| 195 |
data['fueltypes']['items']] |
| 196 |
trend_periods = [TrendPeriod.deserialize(x) for x in |
| 197 |
data['trendperiods']['items']] |
| 198 |
sort_fields = [SortField.deserialize(x) for x in |
| 199 |
data['sortfields']['items']] |
| 200 |
|
| 201 |
return GetReferenceDataResponse( |
| 202 |
stations=stations, |
| 203 |
brands=brands, |
| 204 |
fuel_types=fuel_types, |
| 205 |
trend_periods=trend_periods, |
| 206 |
sort_fields=sort_fields |
| 207 |
) |
| 208 |
|
| 209 |
def __repr__(self) -> str: |
| 210 |
return ('<GetReferenceDataResponse stations=<{} stations>>').format( |
| 211 |
len(self.stations) |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
class GetFuelPricesResponse(object): |
| 216 |
def __init__(self, stations: List[Station], prices: List[Price]) -> None: |
| 217 |
self.stations = stations |
| 218 |
self.prices = prices |
| 219 |
|
| 220 |
@classmethod |
| 221 |
def deserialize(cls, data: Dict[str, Any]) -> 'GetFuelPricesResponse': |
| 222 |
stations = [Station.deserialize(x) for x in data['stations']] |
| 223 |
prices = [Price.deserialize(x) for x in data['prices']] |
| 224 |
return GetFuelPricesResponse( |
| 225 |
stations=stations, |
| 226 |
prices=prices |
| 227 |
) |
| 228 |
|
| 229 |
|
| 230 |
class FuelCheckError(Exception): |
| 231 |
def __init__(self, error_code: Optional[str] = None, |
| 232 |
description: Optional[str] = None) -> None: |
| 233 |
super(FuelCheckError, self).__init__(description) |
| 234 |
self.error_code = error_code |
| 235 |
|
| 236 |
@classmethod |
| 237 |
def create(cls, response: Response) -> 'FuelCheckError': |
| 238 |
error_code = None |
| 239 |
description = response.text |
| 240 |
try: |
| 241 |
data = response.json() |
| 242 |
if 'errorDetails' in data: |
| 243 |
error_details = data['errorDetails'] |
| 244 |
if type(error_details) == list and len(error_details) > 0: |
| 245 |
error_details = error_details[0] |
| 246 |
error_code = error_details.get('code') |
| 247 |
description = error_details.get('description') |
| 248 |
elif type(error_details) == dict: |
| 249 |
error_code = error_details.get('code') |
| 250 |
description = error_details.get('message') # type: ignore |
| 251 |
|
| 252 |
except ValueError: |
| 253 |
pass |
| 254 |
|
| 255 |
return FuelCheckError( |
| 256 |
error_code=error_code, |
| 257 |
description=description |
| 258 |
) |