Ver Fonte

planet_bases: handle FIO HTTP 204

https://rest.fnar.net/company/code/HIP
raylu há 2 semanas atrás
pai
commit
39dd12e8bc
2 ficheiros alterados com 10 adições e 1 exclusões
  1. 6 0
      cache.py
  2. 4 1
      planet_bases.py

+ 6 - 0
cache.py

@@ -44,6 +44,9 @@ def get(url: str, *, json=True, headers=None, expiry=datetime.timedelta(minutes=
 		pass # fall through
 
 	r = get_with_retries(url, headers)
+	if r.status_code == 204:
+		raise NoContent
+
 	cache_path.parent.mkdir(parents=True, exist_ok=True)
 	with lzma.open(cache_path, 'wb') as f:
 		if json:
@@ -64,3 +67,6 @@ def get_with_retries(url: str, headers: httpx._types.HeaderTypes|None=None) -> h
 			else:
 				print(url, 'attempt', attempt+1, 'timed out; retrying...')
 	raise AssertionError
+
+class NoContent(Exception):
+	pass

+ 4 - 1
planet_bases.py

@@ -69,7 +69,10 @@ def get_bases(planet: str) -> typing.Sequence[Site]:
 	return cache.get('https://rest.fnar.net/planet/sites/' + planet, expiry=cache.ONE_DAY)
 
 def get_company(code: str) -> typing.Mapping[str, typing.Any]:
-	return cache.get('https://rest.fnar.net/company/code/' + code, expiry=cache.ONE_DAY)
+	try:
+		return cache.get('https://rest.fnar.net/company/code/' + code, expiry=cache.ONE_DAY)
+	except cache.NoContent:
+		return {'CorporationCode': None, 'CompanyCode': code, 'CompanyName': None, 'UserName': None}
 
 class System(typing.TypedDict):
 	SystemId: str