Skip to content
Snippets Groups Projects
Commit 577e933e authored by David Douard's avatar David Douard
Browse files

api: refactor the SWHRemoteAPI's exception class handling

- add a default implementation of the exception class
- make the exception class a class attribute of SWHRemoteAPI that can be
  overloaded at instanciation time by the api_exception named argument
- so the api_exception positional argument of SWHRemoteAPI becomes a named
  argument

WARNING: This later point does break compatibility for any use of this class
not using named arguments when instanciating it. Fortunately, as far as I
know, every swh packages does use named argument when instanciating this
class, so this should not break anything.
parent 8147cd7f
No related branches found
No related tags found
1 merge request!51api: refactor the SWHRemoteAPI's exception class handling
......@@ -84,6 +84,13 @@ def remote_api_endpoint(path):
return dec
class APIError(Exception):
"""API Error"""
def __str__(self):
return ('An unexpected error occurred in the backend: {}'
.format(self.args))
class MetaSWHRemoteAPI(type):
"""Metaclass for SWHRemoteAPI, which adds a method for each endpoint
of the database it is designed to access.
......@@ -141,9 +148,14 @@ class SWHRemoteAPI(metaclass=MetaSWHRemoteAPI):
This backend class will never be instantiated, it only serves as
a template."""
def __init__(self, api_exception, url,
api_exception = APIError
"""The exception class to raise in case of communication error with
the server."""
def __init__(self, url, api_exception=None,
timeout=None, chunk_size=4096, **kwargs):
self.api_exception = api_exception
if api_exception:
self.api_exception = api_exception
base_url = url if url.endswith('/') else url + '/'
self.url = base_url
self.session = requests.Session()
......
......@@ -74,7 +74,7 @@ class ApiTest(unittest.TestCase):
super().__init__(*args, **kwargs)
self.session.mount('mock', adapter)
c = Testclient('foo', 'mock://example.com/')
c = Testclient(url='mock://example.com/')
res = c.test_endpoint('spam')
self.assertEqual(nb_http_calls, 1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment