-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add support for contributors APIs (#12)
Changelog * Add ContributorApisClient class to communicate with contributors api * Add test * Update NavitiaClient with new contributor property * Update README
- Loading branch information
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Any, Sequence | ||
|
||
from navitia_client.client.apis.api_base_client import ApiBaseClient | ||
from navitia_client.entities.contributor import Contributor | ||
|
||
|
||
class ContributorsApiClient(ApiBaseClient): | ||
@staticmethod | ||
def _get_contributors_from_response( | ||
raw_contributors_response: Any, | ||
) -> Sequence[Contributor]: | ||
contributors = [] | ||
for contributor in raw_contributors_response: | ||
contributors.append( | ||
Contributor( | ||
id=contributor.get("id"), | ||
name=contributor.get("name"), | ||
license=contributor.get("license"), | ||
website=contributor.get("website"), | ||
), | ||
) | ||
|
||
return contributors | ||
|
||
def list_contributors(self, region_id: str) -> Sequence[Contributor]: | ||
results = self.get_navitia_api( | ||
f"{self.base_navitia_url}/coverage/{region_id}/contributors" | ||
) | ||
raw_results = results.json()["contributors"] | ||
return ContributorsApiClient._get_contributors_from_response(raw_results) | ||
|
||
def get_contributor_on_dataset( | ||
self, region_id: str, dataset_id: str | ||
) -> Sequence[Contributor]: | ||
results = self.get_navitia_api( | ||
f"{self.base_navitia_url}/coverage/{region_id}/contributors/{dataset_id}" | ||
) | ||
raw_results = results.json()["contributors"] | ||
return ContributorsApiClient._get_contributors_from_response(raw_results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from navitia_client.client.apis.contributors_apis import ContributorsApiClient | ||
from navitia_client.entities.contributor import Contributor | ||
|
||
|
||
@pytest.fixture | ||
def contributors_apis(): | ||
return ContributorsApiClient( | ||
auth_token="foobar", base_navitia_url="https://api.navitia.io/v1/" | ||
) | ||
|
||
|
||
@patch.object(ContributorsApiClient, "get_navitia_api") | ||
def test_list_contributors( | ||
mock_get_navitia_api: MagicMock, contributors_apis: ContributorsApiClient | ||
) -> None: | ||
# Given | ||
mock_response = MagicMock() | ||
mock_response.json.return_value = { | ||
"contributors": [ | ||
{ | ||
"id": "foo:foo-piv", | ||
"license": "Private", | ||
"name": "foo Production", | ||
"website": "", | ||
} | ||
], | ||
} | ||
mock_get_navitia_api.return_value = mock_response | ||
|
||
# When | ||
contributors = contributors_apis.list_contributors(region_id="bar") | ||
|
||
# Then | ||
assert len(contributors) == 1 | ||
assert isinstance(contributors[0], Contributor) | ||
assert contributors[0].name == "foo Production" | ||
assert contributors[0].id == "foo:foo-piv" | ||
assert contributors[0].license == "Private" | ||
assert contributors[0].website == "" | ||
|
||
|
||
@patch.object(ContributorsApiClient, "get_navitia_api") | ||
def test_get_region_by_id( | ||
mock_get_navitia_api: MagicMock, contributors_apis: ContributorsApiClient | ||
) -> None: | ||
# Given | ||
mock_response = MagicMock() | ||
mock_response.json.return_value = { | ||
"contributors": [ | ||
{ | ||
"id": "foo:foo-piv", | ||
"license": "Private", | ||
"name": "foo Production", | ||
"website": "", | ||
} | ||
], | ||
} | ||
mock_get_navitia_api.return_value = mock_response | ||
|
||
# When | ||
contributors = contributors_apis.get_contributor_on_dataset( | ||
region_id="bar", dataset_id="foo:xxx" | ||
) | ||
|
||
# Then | ||
assert len(contributors) == 1 | ||
assert isinstance(contributors[0], Contributor) | ||
assert contributors[0].name == "foo Production" | ||
assert contributors[0].id == "foo:foo-piv" | ||
assert contributors[0].license == "Private" | ||
assert contributors[0].website == "" |