Implement 'traffic' endpoint #5

Merged
enderlein merged 6 commits from traffic2 into develop 2022-03-12 04:07:11 +01:00
3 changed files with 158 additions and 1 deletions

View file

@ -135,6 +135,7 @@ class System(Positionable):
2h)
:attribute primaryStar: information about the primary star (dict)
:attribute bodyCount: amount of bodies in the system (int)
:attribute traffic: information about traffic in the system (dict, cached for 2h)
:method fetch: updates all attributes in one go
"""
@ -147,6 +148,7 @@ class System(Positionable):
self.__permitName = None
self.__information = {'cachedAt': None}
self.__primaryStar = None
self.__traffic = {'cachedAt' : None}
@property
def coords(self):
@ -166,7 +168,7 @@ class System(Positionable):
return self.__id64
@property
def ids(self):
return {id:self.id, id64:self.id64}
return {'id':self.id, 'id64':self.id64}
def __updateIDs(self):
ids = systemsApi.System.getIds(self.name)
self.__id = ids['id']
@ -212,6 +214,14 @@ class System(Positionable):
"""
return len(systemApi.Bodies.getBodies(self.name)['bodies'])
@property
def traffic(self):
if self.__traffic['cachedAt'] == None or (datetime.datetime.now()
- self.__traffic['cachedAt'] > datetime.timedelta(hours=2)):
self.__traffic = systemApi.Traffic.getTraffic(self.name)
self.__traffic['cachedAt'] = datetime.datetime.now()
return self.__traffic
def fetch(self):
"""
Fetches all information about the system from the API (again). Useful if you

View file

@ -41,3 +41,42 @@ class Bodies(base.ApiEndpoint):
json = cls.query({'systemId': str(systemId)})
return json
class Traffic(base.ApiEndpoint):
"""
The "traffic" endpoint of the "system" API
:attribute url: the API endpoint URL
"""
url = base.ApiEndpoint.url + "system-v1/traffic"
@classmethod
def query(cls, params):
try:
json = super().query(params)
except exception.NotFoundError:
raise exception.SystemNotFoundError(params)
return json
@classmethod
def getTraffic(cls, systemName):
"""
Requests information about traffic in a system.
:param systemName: name of the system in question
"""
json = cls.query({'systemName': systemName})
return json
@classmethod
def getTrafficById(cls, systemId):
"""
Requests information about traffic in a system.
:param systemName: ID of the system in question
"""
json = cls.query({'systemId' : str(systemId)})
return json

View file

@ -1,6 +1,7 @@
import unittest
from . import exception
from .systemApi import Bodies
from .systemApi import Traffic
class BodiesTest(unittest.TestCase):
@ -125,3 +126,110 @@ class BodiesTest(unittest.TestCase):
# FIXXME: I remember finding a system with “[]” in the name, but cant
# remember … and the search function in the usual tools arent very helpful
# :)
class TrafficTest(unittest.TestCase):
def test_getTraffic_Sol(self):
json = Traffic.getTraffic('Sol')
self.assertIs(type(json), dict)
# system data
self.assertEqual(27, json['id'])
self.assertEqual(10477373803, json['id64'])
self.assertEqual('Sol', json['name'])
self.assertEqual('https://www.edsm.net/en/system/id/27/name/Sol', json['url'])
self.assertEqual('J. Calvert (Joshua)', json['discovery']['commander'])
self.assertEqual('2014-11-18 18:21:43', json['discovery']['date'])
# format
self.assertIn('breakdown', json)
self.assertIs(type(json['breakdown']), dict)
self.assertIn('traffic', json)
self.assertIn('total', json['traffic'])
self.assertIn('week', json['traffic'])
self.assertIn('day', json['traffic'])
self.assertIs(type(json['traffic']), dict)
self.assertIs(type(json['traffic']['total']), int)
self.assertIs(type(json['traffic']['week']), int)
self.assertIs(type(json['traffic']['day']), int)
def test_getTrafficById_Sol(self):
json = Traffic.getTrafficById(27)
self.assertIs(type(json), dict)
# system data
self.assertEqual(27, json['id'])
self.assertEqual(10477373803, json['id64'])
self.assertEqual('Sol', json['name'])
self.assertEqual('https://www.edsm.net/en/system/id/27/name/Sol', json['url'])
self.assertEqual('J. Calvert (Joshua)', json['discovery']['commander'])
self.assertEqual('2014-11-18 18:21:43', json['discovery']['date'])
# format
self.assertIn('breakdown', json)
self.assertIs(type(json['breakdown']), dict)
self.assertIn('traffic', json)
self.assertIn('total', json['traffic'])
self.assertIn('week', json['traffic'])
self.assertIn('day', json['traffic'])
self.assertIs(type(json['traffic']), dict)
self.assertIs(type(json['traffic']['total']), int)
self.assertIs(type(json['traffic']['week']), int)
self.assertIs(type(json['traffic']['day']), int)
def test_getTraffic_HD43193(self):
json = Traffic.getTraffic('HD 43193')
self.assertIs(type(json), dict)
# system data
self.assertEqual(85920, json['id'])
self.assertEqual(167244341, json['id64'])
self.assertEqual('HD 43193', json['name'])
self.assertEqual('https://www.edsm.net/en/system/id/85920/name/HD+43193', json['url'])
self.assertEqual('Virosh Lich', json['discovery']['commander'])
self.assertEqual('2015-02-11 19:20:59', json['discovery']['date'])
# format
self.assertIn('breakdown', json)
self.assertIs(type(json['breakdown']), dict)
self.assertIn('traffic', json)
self.assertIn('total', json['traffic'])
self.assertIn('week', json['traffic'])
self.assertIn('day', json['traffic'])
self.assertIs(type(json['traffic']), dict)
self.assertIs(type(json['traffic']['total']), int)
self.assertIs(type(json['traffic']['week']), int)
self.assertIs(type(json['traffic']['day']), int)
def test_getTrafficById_HD43193(self):
json = Traffic.getTrafficById(85920)
self.assertIs(type(json), dict)
# system data
self.assertEqual(85920, json['id'])
self.assertEqual(167244341, json['id64'])
self.assertEqual('HD 43193', json['name'])
self.assertEqual('https://www.edsm.net/en/system/id/85920/name/HD+43193', json['url'])
self.assertEqual('Virosh Lich', json['discovery']['commander'])
self.assertEqual('2015-02-11 19:20:59', json['discovery']['date'])
# format
self.assertIn('breakdown', json)
self.assertIs(type(json['breakdown']), dict)
self.assertIn('traffic', json)
self.assertIn('total', json['traffic'])
self.assertIn('week', json['traffic'])
self.assertIn('day', json['traffic'])
self.assertIs(type(json['traffic']), dict)
self.assertIs(type(json['traffic']['total']), int)
self.assertIs(type(json['traffic']['week']), int)
self.assertIs(type(json['traffic']['day']), int)