Skip to content

Commit 733c83c

Browse files
Added Directory.list_marketplaces, Directory.get_marketplace and unit tests
1 parent 86388fc commit 733c83c

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

connect/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, api_url=None, api_key=None, products=None, file=None):
3535
# Load config from file name
3636
if file:
3737
if not os.path.exists(file):
38-
raise IOError('No file `{}` on directory'.format(file))
38+
raise IOError('No file `{}` on directory `{}`'.format(file, os.getcwd()))
3939

4040
with open(file) as config_file:
4141
configs = config_file.read()

connect/resources/directory.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from connect.config import Config
99
from connect.models.asset import Asset
10+
from connect.models.marketplace import Marketplace
1011
from connect.models.product import Product
1112
from connect.models.tier_config import TierConfig
1213
from connect.resources.base import ApiClient
@@ -47,6 +48,27 @@ def get_asset(self, asset_id):
4748
text, code = ApiClient(self._config, 'assets/' + asset_id).get()
4849
return Asset.deserialize(text)
4950

51+
def list_marketplaces(self, filters=None):
52+
""" List the marketplaces.
53+
54+
:param dict|Query filters: Filters to pass to the request.
55+
:return: A list with the marketplaces that match the given filters.
56+
:rtype: list[Asset]
57+
"""
58+
query = self._get_filters_query(filters, False)
59+
text, code = ApiClient(self._config, 'marketplaces' + query.compile()).get()
60+
return Marketplace.deserialize(text)
61+
62+
def get_marketplace(self, marketplace_id):
63+
""" Returns the marketplace with the given id.
64+
65+
:param str marketplace_id: The id of the marketplace.
66+
:return: The asset with the given id, or ``None`` if such asset does not exist.
67+
:rtype: Asset|None
68+
"""
69+
text, code = ApiClient(self._config, 'marketplaces/' + marketplace_id).get()
70+
return Marketplace.deserialize(text)
71+
5072
def list_products(self, filters=None):
5173
""" List the products.
5274
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"id": "MP-12345",
3+
"name": "France and territories",
4+
"description": "Use the marketplace to distribute services in France and it's territories.",
5+
"active_contracts": 34,
6+
"icon": "/media/PA-123-123/marketplaces/MP-12345/image.png",
7+
"owner": {
8+
"id": "PA-123-123",
9+
"name": "Awesome Provider"
10+
},
11+
"hubs": [
12+
{
13+
"hub": {
14+
"id": "HB-1234-1234",
15+
"name": "Staging OA 7.4"
16+
},
17+
"external_id": "20190101"
18+
},
19+
{
20+
"hub": {
21+
"id": "HB-4321-4321",
22+
"name": "Staging OA 7.3"
23+
},
24+
"external_id": "70190121"
25+
}
26+
],
27+
"countries": [
28+
{
29+
"id": "IN",
30+
"name": "India",
31+
"icon": "/media/countries/india.png",
32+
"zone": "Asia"
33+
}
34+
]
35+
}

tests/test_directory.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212

1313
from connect.exceptions import ServerError
14-
from connect.models import Asset, Product, TierConfig, ProductConfiguration
14+
from connect.models import Asset, Marketplace, Product, TierConfig, ProductConfiguration
1515
from connect.resources import Directory
1616
from .common import Response, load_str
1717

@@ -20,6 +20,10 @@ def _get_asset_response():
2020
return _get_response_from_file('response_asset.json')
2121

2222

23+
def _get_marketplace_response():
24+
return _get_response_from_file('response_marketplace.json')
25+
26+
2327
def _get_product_response():
2428
return _get_response_from_file('response_product.json')
2529

@@ -86,6 +90,40 @@ def test_get_asset_bad():
8690
Directory().get_asset('AS-9861-7949-8492')
8791

8892

93+
@patch('requests.get')
94+
def test_list_marketplaces(get_mock):
95+
get_mock.return_value = _get_array_response(_get_marketplace_response())
96+
marketplaces = Directory().list_marketplaces()
97+
assert isinstance(marketplaces, list)
98+
assert len(marketplaces) == 1
99+
assert isinstance(marketplaces[0], Marketplace)
100+
assert marketplaces[0].id == 'MP-12345'
101+
102+
get_mock.assert_called_with(
103+
url='http://localhost:8080/api/public/v1/marketplaces',
104+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
105+
timeout=300)
106+
107+
108+
@patch('requests.get')
109+
def test_get_marketplace(get_mock):
110+
get_mock.return_value = _get_marketplace_response()
111+
marketplace = Directory().get_marketplace('MP-12345')
112+
assert isinstance(marketplace, Marketplace)
113+
assert marketplace.id == 'MP-12345'
114+
115+
get_mock.assert_called_with(
116+
url='http://localhost:8080/api/public/v1/marketplaces/MP-12345',
117+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
118+
timeout=300)
119+
120+
121+
@patch('requests.get', MagicMock(return_value=_get_bad_response()))
122+
def test_get_marketplace_bad():
123+
with pytest.raises(ServerError):
124+
Directory().get_marketplace('MP-00000')
125+
126+
89127
@patch('requests.get')
90128
def test_list_products(get_mock):
91129
get_mock.return_value = _get_array_response(_get_product_response())
@@ -167,4 +205,3 @@ def test_search_tier_account_bad():
167205
def test_get_tier_account_bad():
168206
with pytest.raises(ServerError):
169207
Directory().get_tier_account('TAR-0000-0000-0000-000-000')
170-

0 commit comments

Comments
 (0)