Initial working … thing.

Right now can only query full systems from the systems API.
This commit is contained in:
alterNERDtive 2019-09-17 13:03:16 +02:00
parent 9aaafbb558
commit 47c9f34a5c
12 changed files with 208 additions and 0 deletions

24
.editorconfig Normal file
View file

@ -0,0 +1,24 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
indent_style = tab
indent_size = 8
# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.py]
indent_style = spaces
indent_size = 2
[*.md]
indent_style = spaces
indent_size = 4
trim_trailing_whitespace = false

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
**/__pycache__/**

4
Makefile Normal file
View file

@ -0,0 +1,4 @@
all: test
test:
python -m unittest

1
edsm/__init__.py Normal file
View file

@ -0,0 +1 @@
from . import base

42
edsm/base.py Normal file
View file

@ -0,0 +1,42 @@
import requests
from . import exception
class ApiEndpoint:
"""
Parent class for all API endpoints.
:param url: the endpoints URL
"""
url = "https://www.edsm.net/api-"
def query(self, params):
"""
Queries the API endpoint with the given parameters.
:param params: the parameters to append to the base URL
"""
response = requests.get(self.url + "?" + params)
if response.status_code != 200:
raise exception.serverError()
json = response.json()
if not json:
raise exception.notFoundError()
return json
class System:
"""
FIXXME
"""
def __init__(self, name=""):
systemName = name
id = None
id64 = None
coords = None
information = None
primaryStar = None
class Commander:
"""
FIXXME
"""
pass

0
edsm/cache.py Normal file
View file

0
edsm/commanderApi.py Normal file
View file

14
edsm/exception.py Normal file
View file

@ -0,0 +1,14 @@
class serverError(Exception):
pass
class notFoundError(Exception):
def __self__(self, name):
self.name = name
class systemNotFoundError(notFoundError):
def str(self):
return "System \"{}\" not found.".format(self.name)
class commanderNotFoundError(notFoundError):
def str(self):
return "Commander \"{}\" not found or has not made his flight logs public.".format(self.name)

0
edsm/logsApi.py Normal file
View file

0
edsm/systemApi.py Normal file
View file

53
edsm/systemsApi.py Normal file
View file

@ -0,0 +1,53 @@
import requests
from . import base
from . import exception
class System(base.ApiEndpoint):
"""
The system endpoint of the systems API
:attribute url: the API endpoint URL
"""
url = base.ApiEndpoint.url + "v1/system"
def getSystem(self, systemName):
"""
Requests the entire range of information for a system
:param systemName: name of the system in question
"""
try:
json = self.query("systemName=" + systemName
+ "&showId=1"
+ "&showCoordinates=1"
+ "&showPermit=1"
+ "&showInformation=1"
+ "&showPrimaryStar=1")
except exception.notFoundError:
raise exception.systemNotFoundError(systemName)
system = base.System()
system.systemName = json['name']
system.id = json['id']
system.id64 = json['id64']
system.coords = json['coords']
system.requirePermit = json['requirePermit']
if system.requirePermit:
system.permitName = json['permitName']
system.information = json['information']
system.primaryStar = json['primaryStar']
return system
def getIds(self, systemName):
return None
def getCoordinates(self, systemName):
return None
def getInformation(self, systemName):
return None
def getPrimaryStar(self, systemName):
return None

69
edsm/test_systemsApi.py Normal file
View file

@ -0,0 +1,69 @@
import unittest
from . import exception
from .systemsApi import System
class SystemTest(unittest.TestCase):
def test_getSystem_sol(self):
system = System().getSystem("Sol")
# ids
self.assertEqual(27, system.id)
self.assertEqual(10477373803, system.id64)
# coordinates
self.assertTrue(system.coords)
self.assertEqual(system.coords['x'], 0)
self.assertEqual(system.coords['y'], 0)
self.assertEqual(system.coords['z'], 0)
# permit
self.assertTrue(system.requirePermit)
self.assertEqual("Sol", system.permitName)
# information
self.assertTrue(system.information)
self.assertEqual("Federation", system.information['allegiance'])
self.assertTrue('government' in system.information)
self.assertTrue('faction' in system.information)
self.assertTrue('factionState' in system.information)
self.assertTrue('population' in system.information)
self.assertTrue('security' in system.information)
self.assertTrue('economy' in system.information)
self.assertTrue('secondEconomy' in system.information)
self.assertTrue('reserve' in system.information)
# primaryStar
self.assertTrue(system.primaryStar)
self.assertEqual("G (White-Yellow) Star", system.primaryStar['type'])
self.assertEqual("Sol", system.primaryStar['name'])
self.assertTrue(system.primaryStar['isScoopable'])
def test_getSystem_Beagle(self):
system = System().getSystem("Beagle Point")
# ids
self.assertEqual(124406, system.id)
self.assertEqual(81973396946, system.id64)
# coordinates
self.assertTrue(system.coords)
self.assertEqual(system.coords['x'], -1111.5625)
self.assertEqual(system.coords['y'], -134.21875)
self.assertEqual(system.coords['z'], 65269.75)
# permit
self.assertFalse(system.requirePermit)
# information
self.assertFalse(system.information)
# primaryStar
self.assertTrue(system.primaryStar)
self.assertEqual("K (Yellow-Orange) Star", system.primaryStar['type'])
self.assertEqual("Beagle Point", system.primaryStar['name'])
self.assertTrue(system.primaryStar['isScoopable'])
def test_getSystem_invalidName(self):
with self.assertRaises(exception.systemNotFoundError):
System().getSystem("Lol")