Skip to content

Commit

Permalink
refactor: deprecate old upload endpoints and use pictor instead (#185)
Browse files Browse the repository at this point in the history
* deprecate old upload endpoints and use pictor instead 
* refactor the code to use requests library instead of asyncio
  • Loading branch information
CamiloInx authored Apr 2, 2024
1 parent af9de1b commit 790dd20
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 318 deletions.
95 changes: 43 additions & 52 deletions landingai/data_management/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
from functools import lru_cache
from importlib.metadata import version
from typing import Any, Dict, Optional, Tuple, cast

import aiohttp
import requests


from landingai.data_management.utils import to_camel_case
from landingai.exceptions import HttpError
from landingai.utils import load_api_credential
from requests_toolbelt.multipart.encoder import MultipartEncoder

METADATA_ITEMS = "metadata_items"
METADATA_UPDATE = "metadata_update"
METADATA_GET = "metadata_get"
MEDIA_LIST = "media_list"
MEDIA_REGISTER = "media_register"
MEDIA_SIGN = "media_sign"
MEDIA_UPLOAD = "media_upload"
MEDIA_DETAILS = "media_details"
MEDIA_UPDATE_SPLIT = "media_update_split"
GET_PROJECT_SPLIT = "get_project_split"
Expand Down Expand Up @@ -69,14 +68,9 @@
"endpoint": "api/{version}/object/metadata",
"method": requests.get,
},
MEDIA_REGISTER: {
"root_url": "LANDING_API",
"endpoint": "api/{version}/medias/new",
"method": requests.post,
},
MEDIA_SIGN: {
MEDIA_UPLOAD: {
"root_url": "LANDING_API",
"endpoint": "api/{version}/medias/sign",
"endpoint": "pictor/{version}/upload",
"method": requests.post,
},
MEDIA_LIST: {
Expand Down Expand Up @@ -142,57 +136,54 @@ def _project_id(self) -> int:
def _api_key(self) -> str:
return self.api_key

async def _api_async(
def _api_async(
self,
route_name: str,
params: Optional[Dict[str, Any]] = None,
form_data: Optional[Dict[str, Any]] = None,
resp_with_content: Optional[Dict[str, Any]] = None,
url_replacements: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""Returns a response from the LandingLens API"""
assert resp_with_content is not None
is_form_data = form_data is not None
assert resp_with_content is not None if not is_form_data else True

endpoint, headers, params, root_url, route = self._api_common_setup(
resp_with_content, params, route_name, url_replacements
route_name, url_replacements, resp_with_content, params
)
if is_form_data:
# Create a MultipartEncoder for the form data
form = MultipartEncoder(fields=form_data) if form_data is not None else None
headers["Content-Type"] = form.content_type

async with aiohttp.ClientSession() as session:
# TODO: should be library agnostic
if route["method"] == requests.get:
method = session.get
elif route["method"] == requests.post:
method = session.post
else:
raise NotImplementedError()

async with method(
endpoint,
try:
response = requests.request(
method=route["method"].__name__,
url=endpoint,
headers=headers,
json=resp_with_content,
json=resp_with_content if not is_form_data else None,
params=params,
ssl=True,
) as resp:
_LOGGER.debug("Request URL: ", resp.request_info.url)
_LOGGER.debug("Response Code: ", resp.status)
_LOGGER.debug("Response Reason: ", resp.reason)
data=form if is_form_data else None,
)

_LOGGER.debug("Request URL: ", response.url)
_LOGGER.debug("Response Code: ", response.status_code)
_LOGGER.debug("Response Reason: ", response.reason)

if resp.status != 200:
try:
content = await resp.text()
error_message = json.load(io.StringIO(content))["message"]
except Exception as e:
error_message = e
raise HttpError(
"HTTP request to LandingLens server failed with "
f"code {resp.status}-{resp.reason} and error message: \n"
f"{error_message}"
)
resp_with_content = await resp.json()
_LOGGER.debug(
"Response Content (500 chars): ",
json.dumps(resp_with_content)[:500],
)
assert resp_with_content is not None
return resp_with_content
resp_with_content = response.json()
_LOGGER.debug(
"Response Content (500 chars): ",
json.dumps(resp_with_content)[:500],
)
except requests.exceptions.RequestException as e:
raise HttpError(
"HTTP request to LandingLens server failed with error message: \n"
f"{str(e)}"
)
except Exception as e:
raise HttpError(f"An error occurred during the HTTP request: {str(e)}")
assert resp_with_content is not None
return resp_with_content

def _api(
self,
Expand All @@ -203,7 +194,7 @@ def _api(
) -> Dict[str, Any]:
"""Returns a response from the LandingLens API"""
endpoint, headers, params, root_url, route = self._api_common_setup(
data, params, route_name, url_replacements
route_name, url_replacements, data, params
)
resp = route["method"](
endpoint,
Expand Down Expand Up @@ -233,10 +224,10 @@ def _api(

def _api_common_setup(
self,
data: Optional[Dict[str, Any]],
params: Optional[Dict[str, Any]],
route_name: str,
url_replacements: Optional[Dict[str, Any]],
data: Optional[Dict[str, Any]] = None,
params: Optional[Dict[str, Any]] = None,
) -> Tuple[str, Dict[str, Any], Dict[str, Any], str, Dict[str, Any]]:
route = ROUTES[route_name]
headers = {
Expand Down
Loading

0 comments on commit 790dd20

Please sign in to comment.