Skip to content

Commit

Permalink
[ES-53] Allow deriving from Session and JsonApiSession (#6631)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 0fe426a00fe8eaa86d8ce8d4ba543805fb16610a
  • Loading branch information
Jaap Vermeulen authored and Descartes Labs Build committed Apr 1, 2020
1 parent 229eb91 commit e7ae964
Show file tree
Hide file tree
Showing 10 changed files with 1,233 additions and 143 deletions.
58 changes: 1 addition & 57 deletions descarteslabs/catalog/catalog_client.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,18 @@
# jsonapi_document


import json
import os

from descarteslabs.client.auth import Auth
from descarteslabs.client.exceptions import ClientError
from descarteslabs.client.services.service.service import (
JsonApiService,
JsonApiSession,
HttpRequestMethod,
)


HttpRequestMethod = HttpRequestMethod


class _RewriteErrorSession(JsonApiSession):
"""Rewrite JSON ClientErrors that are returned to make them easier to read"""

def request(self, *args, **kwargs):
try:
return super(_RewriteErrorSession, self).request(*args, **kwargs)
except ClientError as client_error:
self._rewrite_error(client_error)
raise

def _rewrite_error(self, client_error):
KEY_ERRORS = "errors"
KEY_TITLE = "title"
KEY_STATUS = "status"
KEY_DETAIL = "detail"
KEY_SOURCE = "source"
KEY_POINTER = "pointer"
message = ""

for arg in client_error.args:
try:
errors = json.loads(arg)[KEY_ERRORS]

for error in errors:
line = ""
seperator = ""

if KEY_TITLE in error:
line += error[KEY_TITLE]
seperator = ": "
elif KEY_STATUS in error:
line += error[KEY_STATUS]
seperator = ": "

if KEY_DETAIL in error:
line += seperator + error[KEY_DETAIL].strip(".")
seperator = ": "

if KEY_SOURCE in error:
source = error[KEY_SOURCE]
if KEY_POINTER in source:
source = source[KEY_POINTER].split("/")[-1]
line += seperator + source

if line:
message += "\n " + line
except Exception:
return

if message:
client_error.args = (message,)


class CatalogClient(JsonApiService):
"""
The CatalogClient handles the HTTP communication with the Descartes Labs catalog.
Expand Down Expand Up @@ -106,7 +50,7 @@ def __init__(self, url=None, auth=None, retries=None):
)

super(CatalogClient, self).__init__(
url, auth=auth, retries=retries, session_class=_RewriteErrorSession
url, auth=auth, retries=retries, rewrite_errors=True
)

@staticmethod
Expand Down
6 changes: 6 additions & 0 deletions descarteslabs/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class NotFoundError(ClientError):
status = 404


class ProxyAuthenticationRequiredError(ClientError):
"""Client request needs proxy authentication."""

status = 407


class ConflictError(ClientError):
"""Client request conflicts with existing state."""

Expand Down
14 changes: 11 additions & 3 deletions descarteslabs/client/services/catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,11 @@ def upload_image(
)
else:
failed, upload_id, error = self._do_upload(
files, product_id, image_id, metadata=metadata, add_namespace=add_namespace
files,
product_id,
image_id,
metadata=metadata,
add_namespace=add_namespace,
)

if failed:
Expand Down Expand Up @@ -1770,7 +1774,9 @@ def _do_multi_file_upload(

return failed, upload_id, error

def _do_upload(self, file_ish, product_id, image_id=None, metadata=None, add_namespace=False):
def _do_upload(
self, file_ish, product_id, image_id=None, metadata=None, add_namespace=False
):
# kwargs are treated as metadata fields and restricted to primitives
# for the key val pairs.
fd = None
Expand Down Expand Up @@ -1808,7 +1814,9 @@ def _do_upload(self, file_ish, product_id, image_id=None, metadata=None, add_nam
return True, upload_id, e

try:
upload_id = image_id or metadata.pop("image_id", None) or os.path.basename(fd.name)
upload_id = (
image_id or metadata.pop("image_id", None) or os.path.basename(fd.name)
)

r = self.session.post(
"/products/{}/images/upload/{}".format(product_id, upload_id),
Expand Down
2 changes: 1 addition & 1 deletion descarteslabs/client/services/metadata/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Metadata (deprecated)


Filtering
---------
~~~~~~~~~

.. py:attribute:: properties
Expand Down
18 changes: 16 additions & 2 deletions descarteslabs/client/services/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .service import Service, JsonApiService, ThirdPartyService, NotFoundError
from .service import (
Service,
Session,
JsonApiService,
JsonApiSession,
ThirdPartyService,
NotFoundError,
)

__all__ = ["Service", "JsonApiService", "ThirdPartyService", "NotFoundError"]
__all__ = [
"Service",
"Session",
"JsonApiService",
"JsonApiSession",
"ThirdPartyService",
"NotFoundError",
]
38 changes: 38 additions & 0 deletions descarteslabs/client/services/service/readme.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.. default-role:: py:obj

Service
-------

.. autosummary::
:nosignatures:

~descarteslabs.client.services.service.Service
~descarteslabs.client.services.service.JsonApiService
~descarteslabs.client.services.service.ThirdPartyService
~descarteslabs.client.services.service.Session
~descarteslabs.client.services.service.JsonApiSession

-----

.. autoclass:: descarteslabs.client.services.service.Service
:autosummary:
:members:

.. autoclass:: descarteslabs.client.services.service.JsonApiService
:autosummary:
:members:
:inherited-members:
:show-inheritance:

.. autoclass:: descarteslabs.client.services.service.ThirdPartyService
:autosummary:
:members:

.. autoclass:: descarteslabs.client.services.service.Session
:autosummary:
:members:

.. autoclass:: descarteslabs.client.services.service.JsonApiSession
:autosummary:
:members:
:show-inheritance:
Loading

0 comments on commit e7ae964

Please sign in to comment.