Browse Source

Exception handling. Class rename

dev
Laurent Kislaire 3 years ago
parent
commit
34911287dd
  1. 42
      pyfbx/client.py

42
pyfbx/client.py

@ -13,10 +13,7 @@ from . import api
from . import utils
from . import mdns
__all__ = [
"FbxTransport",
"Fbx",
"FbxClass", ]
__all__ = ["Transport", "Fbx", "FbxClass"]
class FbxClass():
@ -28,7 +25,7 @@ class FbxClass():
self._trn = transport
class FbxTransport():
class Transport():
"""
Transport abstraction and context handling for all methods
"""
@ -53,23 +50,27 @@ class FbxTransport():
else:
self._url = url
def set_session_header(self, session_token):
def set_header(self, session_token):
self._session.headers.update({'X-Fbx-App-Auth': session_token})
def api_exec(self, http_method, endpoint, post_data=None, **kwargs):
req_response = self._session.request(
http_method,
self._url + "/" + endpoint.format(**kwargs), json=post_data)
self.log.debug(">> Sent %s %s/%s Post: %s", http_method, self._url,
endpoint.format(**kwargs), post_data)
req_response.raise_for_status()
try:
self.log.debug(">> Sent %s %s/%s Post: %s", http_method, self._url,
endpoint.format(**kwargs), post_data)
req_response = self._session.request(
http_method,
self._url + "/" + endpoint.format(**kwargs), json=post_data)
req_response.raise_for_status()
except requests.exceptions.RequestException as exc:
raise RequestError(exc)
response = req_response.json()
if response['success']:
if 'result' in response:
self.log.debug("<< Got {}".format(response['result']))
return response['result']
else:
raise FbxErrorResponse(response['error_code'], response['msg'])
raise ResponseError(response['error_code'], response['msg'])
def local_base(self, url=api._DISC_HTTP_URL):
response = self._session.get("{}/api_version".format(url)).json()
@ -85,7 +86,7 @@ class Fbx():
def __init__(self, url=None, nomdns=False, session=None):
self.log = logging.getLogger("pyfbx.fbx")
self._trn = FbxTransport(url, nomdns=nomdns, session=session)
self._trn = Transport(url, nomdns=nomdns, session=session)
self.app_id = None
self.token = None
@ -132,13 +133,20 @@ class Fbx():
}
resp = self._trn.api_exec("POST", "login/session/", data)
session_token = resp["session_token"]
self.app_id = app_id
self._trn.set_session_header(session_token)
self._trn.set_header(session_token)
self.log.info("Authenticated. Storing token={}, app_id={}".format(self.token, self.app_id))
return resp["permissions"]
class FbxErrorResponse(Exception):
class RequestError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return '{}'.format(self.msg)
class ResponseError(Exception):
def __init__(self, error_code, msg):
self.error_code = error_code
self.msg = msg

Loading…
Cancel
Save