Fetching systems

This commit is contained in:
Richard Bronkhorst 2023-06-10 20:49:50 +02:00
parent cea10ae07c
commit 042b931133
6 changed files with 36 additions and 15 deletions

9
api.py
View File

@ -1,4 +1,5 @@
import requests
from models.system import System
from util import *
class ApiError(Exception):
@ -17,9 +18,8 @@ class Api:
raise ApiError('no token. Please register', 1337)
return self.agent.token
def request(self, method, path, data=None, need_token=True):
def request(self, method, path, data=None, need_token=True, params={}):
headers = {}
params = {}
if need_token:
headers['Authorization'] = 'Bearer ' + self.token()
if method == 'get':
@ -53,4 +53,9 @@ class Api:
data = self.request('get', 'my/agent')
self.agent.update(data)
return self.agent
def list_systems(self, page=1):
data = self.request('get', 'systems', params={'page': page})
return self.store.update_list(System, data)

View File

@ -29,15 +29,14 @@ class Commander(CommandLine):
def after_cmd(self):
self.store.flush()
def do_foo(self):
self.store.foo()
self.store.flush()
def do_info(self):
pprint(self.api.info(), 100)
def do_register(self, faction):
self.api.register(faction.upper())
def do_systems(self, page=1):
self.api.list_systems(int(page))
def main(args):
c = Commander(args.store_dir, args.agent)

View File

@ -5,7 +5,7 @@ class Agent(Base):
credits: int = 0
def update(self, d):
self.seta(d, 'credits')
self.seta('credits', d)
def path(self):
return f'{self.symbol}.{self.ext()}'

View File

@ -1,6 +1,6 @@
from copy import deepcopy
from dataclasses import dataclass
from util import sg
@dataclass
class Base:
symbol: str
@ -11,9 +11,12 @@ class Base:
self.store = store
self.dirty = True
def seta(self, d, name):
if name in d:
setattr(self, name, d[name])
def seta(self, attr, d, name=None):
if name is None:
name = attr
val = sg(d, name)
if val is not None:
setattr(self, attr, val)
def __setattr__(self, name, value):
if name != 'dirty':

View File

@ -3,6 +3,15 @@ from .base import Base
class System(Base):
x:int = 0
y:int = 0
type:str = 'unknown'
def update(self, d):
self.seta('x', d)
self.seta('y', d)
self.seta('type', d)
@classmethod
def ext(self):
return 'stm'

View File

@ -6,6 +6,7 @@ from models.agent import Agent
from os.path import isfile, dirname, isdir
import os
import json
from util import *
class Store:
def __init__(self, data_dir):
@ -41,6 +42,14 @@ class Store:
self.data[symbol] = obj
return obj
def update(self, typ, symbol, data):
obj = self.get(typ, symbol)
obj.update(data)
return obj
def update_list(self, typ, lst):
return [self.update(typ, mg(d, 'symbol'), d) for d in lst]
def all(self, path, typ):
if hasattr(path, 'path'):
path = path.path()
@ -62,7 +71,3 @@ class Store:
for obj in self.data.values():
if obj.dirty:
self.store(obj)
def foo(self):
s = self.get(System, 'dez-hq14')
print(s)