Lot of cleanup

This commit is contained in:
Thomas Sileo
2018-05-29 21:36:05 +02:00
parent 559c65f474
commit 6aea610fb6
9 changed files with 143 additions and 25 deletions

View File

@@ -5,6 +5,7 @@ from urllib.parse import urlparse
from Crypto.PublicKey import RSA
from .urlutils import check_url
from .errors import ActivityNotFoundError
logger = logging.getLogger(__name__)
@@ -32,6 +33,9 @@ class ActorService(object):
'Accept': 'application/activity+json',
'User-Agent': self._user_agent,
})
if resp.status_code == 404:
raise ActivityNotFoundError(f'{actor_url} cannot be fetched, 404 not found error')
resp.raise_for_status()
return resp.json()

View File

@@ -1,6 +1,25 @@
class Error(Exception):
pass
status_code = 400
def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv
def __repr__(self):
return f'{self.__class__.__qualname__}({self.message!r}, payload={self.payload!r}, status_code={self.status_code})'
class ActivityNotFoundError(Error):
status_code = 404
class BadActivityError(Error):

View File

@@ -2,14 +2,18 @@ import os
import binascii
from Crypto.PublicKey import RSA
from typing import Callable
KEY_DIR = 'config/'
def get_secret_key(name:str) -> str:
def _new_key() -> str:
return binascii.hexlify(os.urandom(32)).decode('utf-8')
def get_secret_key(name: str, new_key: Callable[[], str] = _new_key) -> str:
key_path = f'{KEY_DIR}{name}.key'
if not os.path.exists(key_path):
k = binascii.hexlify(os.urandom(32)).decode('utf-8')
k = new_key()
with open(key_path, 'w+') as f:
f.write(k)
return k

View File

@@ -2,6 +2,7 @@ import requests
from urllib.parse import urlparse
from .urlutils import check_url
from .errors import ActivityNotFoundError
class ObjectService(object):
@@ -20,6 +21,9 @@ class ObjectService(object):
'Accept': 'application/activity+json',
'User-Agent': self._user_agent,
})
if resp.status_code == 404:
raise ActivityNotFoundError(f'{object_id} cannot be fetched, 404 error not found')
resp.raise_for_status()
return resp.json()

View File

@@ -5,11 +5,12 @@ import ipaddress
from urllib.parse import urlparse
from . import strtobool
from .errors import Error
logger = logging.getLogger(__name__)
class InvalidURLError(Exception):
class InvalidURLError(Error):
pass