Skip to content

Commit 731bd81

Browse files
authored
Merge pull request #110 from JaviCerveraIngram/CPS-80-directory-marketplace
CPS-80: Added Directory.list_marketplaces and Directory.get_marketplace
2 parents a75d7c1 + 717bd4f commit 731bd81

File tree

7 files changed

+115
-2
lines changed

7 files changed

+115
-2
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/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .directory import Directory
77
from .fulfillment_automation import FulfillmentAutomation
8+
from .marketplace import MarketplaceResource
89
from .template import TemplateResource
910
from .tier_config_automation import TierConfigAutomation
1011
from .usage_automation import UsageAutomation
@@ -17,6 +18,7 @@
1718
__all__ = [
1819
'Directory',
1920
'FulfillmentAutomation',
21+
'MarketplaceResource',
2022
'TemplateResource',
2123
'TierConfigAutomation',
2224
'UsageAutomation',

connect/resources/directory.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from connect.models.product import Product
1111
from connect.models.tier_config import TierConfig
1212
from connect.resources.base import ApiClient
13+
from connect.resources.marketplace import MarketplaceResource
1314
from connect.resources.tier_account import TierAccountResource
1415
from connect.rql import Query
1516

@@ -47,6 +48,24 @@ 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: List of marketplaces matching given filters.
56+
:rtype: list[Marketplace]
57+
"""
58+
return MarketplaceResource(self._config).list(filters)
59+
60+
def get_marketplace(self, marketplace_id):
61+
""" Obtains Marketplace object given its ID.
62+
63+
:param str marketplace_id: The id of the marketplace.
64+
:return: The marketplace with the given id, or ``None`` if such marketplace does not exist.
65+
:rtype: Marketplace|None
66+
"""
67+
return MarketplaceResource(self._config).get(marketplace_id)
68+
5069
def list_products(self, filters=None):
5170
""" List the products.
5271

connect/resources/marketplace.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
5+
6+
from .base import BaseResource
7+
from ..models import Marketplace
8+
9+
10+
class MarketplaceResource(BaseResource):
11+
""" Resource to work with :py:class:`connect.models.Marketplace` models.
12+
:param Config config: Config object or ``None`` to use environment config (default).
13+
"""
14+
resource = 'marketplaces'
15+
model_class = Marketplace
16+
17+
def __init__(self, config=None):
18+
super(MarketplaceResource, self).__init__(config)

tests/data/icon.png

4.61 KB
Loading
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: 40 additions & 1 deletion
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,41 @@ 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+
params={'limit': 100},
106+
timeout=300)
107+
108+
109+
@patch('requests.get')
110+
def test_get_marketplace(get_mock):
111+
get_mock.return_value = _get_marketplace_response()
112+
marketplace = Directory().get_marketplace('MP-12345')
113+
assert isinstance(marketplace, Marketplace)
114+
assert marketplace.id == 'MP-12345'
115+
116+
get_mock.assert_called_with(
117+
url='http://localhost:8080/api/public/v1/marketplaces/MP-12345',
118+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
119+
timeout=300)
120+
121+
122+
@patch('requests.get', MagicMock(return_value=_get_bad_response()))
123+
def test_get_marketplace_bad():
124+
with pytest.raises(ServerError):
125+
Directory().get_marketplace('MP-00000')
126+
127+
89128
@patch('requests.get')
90129
def test_list_products(get_mock):
91130
get_mock.return_value = _get_array_response(_get_product_response())

0 commit comments

Comments
 (0)