Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reference now returns dict, fixes #69 #116

Merged
merged 3 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/conversions/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_convert_to_geography_ids(self):
rotterdam_id = (
"68faf65af1345067f11dc6723b8da32f00e304a6f33c000118fccd81947deb4e"
)
rotterdam_name = Geographies().reference(rotterdam_id)[0]["name"]
rotterdam_name = Geographies().reference(rotterdam_id)["name"]

result = convert_to_geography_ids([rotterdam_name])

Expand Down
2 changes: 1 addition & 1 deletion tests/endpoints/test_products_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def test_lookup_crude(self):
"6f11b0724c9a4e85ffa7f1445bc768f054af755a090118dcf99f14745c261653"
)

assert result[0]["name"] == "Crude"
assert result["name"] == "Crude"
18 changes: 18 additions & 0 deletions tests/endpoints/test_reference_endpoints_real.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from tests.testcases import TestCaseUsingRealAPI
from docs.utils import to_markdown
from vortexasdk import Geographies


class TestGeographiesReal(TestCaseUsingRealAPI):
def test_search(self):
geographies = Geographies().search(term=["Liverpool", "Southampton"])
names = [g["name"] for g in geographies]

assert "Liverpool [GB]" in names

def test_search_to_df(self):
geographies = (
Geographies().search(term=["Liverpool", "Southampton"]).to_df()
)

print(to_markdown(geographies))
5 changes: 3 additions & 2 deletions tests/mock_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class MockVortexaClient(AbstractVortexaClient):
VESSEL_MOVEMENTS_RESOURCE: example_vessel_movements,
}

def get_reference(self, resource: str, id: ID) -> str:
return ""
def get_reference(self, resource: str, id: ID) -> List[Dict]:
entities = MockVortexaClient._results[resource]
return [e for e in entities if e["id"] == id]

def search(self, resource: str, **data) -> List:
return MockVortexaClient._results[resource]
6 changes: 3 additions & 3 deletions vortexasdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import functools
import os
from multiprocessing.pool import ThreadPool
from typing import List
from typing import Dict, List

from requests import Response

Expand All @@ -23,7 +23,7 @@ class VortexaClient(AbstractVortexaClient):
def __init__(self, **kwargs):
self.api_key = kwargs["api_key"]

def get_reference(self, resource: str, id: ID) -> str:
def get_reference(self, resource: str, id: ID) -> List[Dict]:
"""Lookup reference data."""
url = self._create_url(f"{resource}/{id}")
response = retry_get(url)
Expand Down Expand Up @@ -91,7 +91,7 @@ def _send_post_request(url, payload, size, offset):
return response


def _handle_response(response: Response, payload=None) -> dict:
def _handle_response(response: Response, payload=None) -> Dict:
if response.ok:
return response.json()
else:
Expand Down
4 changes: 2 additions & 2 deletions vortexasdk/endpoints/corporations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Corporations Endpoint."""
from typing import List, Union
from typing import List, Union, Dict

from vortexasdk.api import ID
from vortexasdk.endpoints.corporations_result import CorporationsResult
Expand Down Expand Up @@ -55,7 +55,7 @@ def search(self, term: Union[str, List[str]] = None) -> CorporationsResult:
params = convert_values_to_list({"term": term})
return CorporationsResult(super().search(**params))

def reference(self, id: ID):
def reference(self, id: ID) -> Dict:
"""
Perform a corporation lookup.

Expand Down
4 changes: 2 additions & 2 deletions vortexasdk/endpoints/geographies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Geographies Endpoint."""
from typing import List, Union
from typing import List, Union, Dict

from vortexasdk.api import ID
from vortexasdk.endpoints.endpoints import GEOGRAPHIES_REFERENCE
Expand Down Expand Up @@ -51,7 +51,7 @@ def search(self, term: Union[str, List[str]] = None) -> GeographyResult:
params = convert_values_to_list({"term": term})
return GeographyResult(super().search(**params))

def reference(self, id: ID):
def reference(self, id: ID) -> Dict:
"""
Perform a geography lookup.

Expand Down
4 changes: 2 additions & 2 deletions vortexasdk/endpoints/products.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Products Endpoint."""
from typing import List, Union
from typing import List, Union, Dict

from vortexasdk.api.shared_types import ID
from vortexasdk.endpoints.endpoints import PRODUCTS_REFERENCE
Expand Down Expand Up @@ -67,7 +67,7 @@ def search(

return ProductResult(super().search(**search_params))

def reference(self, id: ID):
def reference(self, id: ID) -> Dict:
"""
Perform a product lookup.

Expand Down
4 changes: 2 additions & 2 deletions vortexasdk/endpoints/vessels.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Vessels Endpoint."""
from typing import List, Union
from typing import List, Union, Dict

from vortexasdk.api.id import ID
from vortexasdk.conversions import convert_to_product_ids
Expand Down Expand Up @@ -85,7 +85,7 @@ def search(

return VesselsResult(super().search(**search_params))

def reference(self, id: ID):
def reference(self, id: ID) -> Dict:
"""
Perform a vessel lookup.

Expand Down
4 changes: 4 additions & 0 deletions vortexasdk/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class InvalidAPIDataResponseException(Exception):
"""Vortexa API returned Faulty Data, contact support."""

pass
16 changes: 13 additions & 3 deletions vortexasdk/operations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import List
from typing import Dict, List

from vortexasdk.api.id import ID
from vortexasdk.client import default_client
from vortexasdk.exceptions import InvalidAPIDataResponseException
from vortexasdk.logger import get_logger

logger = get_logger(__name__)
Expand All @@ -20,7 +21,7 @@ def __init__(self, resource):
"""
self._resource = resource

def reference(self, id: ID):
def reference(self, id: ID) -> Dict:
"""
Lookup reference data using ID.

Expand All @@ -38,7 +39,16 @@ def reference(self, id: ID):
logger.info(
f"Looking up {self.__class__.__name__} reference data with id: {id}"
)
return default_client().get_reference(self._resource, id)

data = default_client().get_reference(self._resource, id)

assert len(data) <= 1, InvalidAPIDataResponseException(
f"Server error: more than one record returned matching ID {id}"
)
try:
return data[0]
except IndexError:
return {}


class Search:
Expand Down