Skip to content
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 connect/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, api_url=None, api_key=None, products=None, file=None):
# Load config from file name
if file:
if not os.path.exists(file):
raise IOError('No file `{}` on directory'.format(file))
raise IOError('No file `{}` on directory `{}`'.format(file, os.getcwd()))

with open(file) as config_file:
configs = config_file.read()
Expand Down
2 changes: 2 additions & 0 deletions connect/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .directory import Directory
from .fulfillment_automation import FulfillmentAutomation
from .marketplace import MarketplaceResource
from .template import TemplateResource
from .tier_config_automation import TierConfigAutomation
from .usage_automation import UsageAutomation
Expand All @@ -17,6 +18,7 @@
__all__ = [
'Directory',
'FulfillmentAutomation',
'MarketplaceResource',
'TemplateResource',
'TierConfigAutomation',
'UsageAutomation',
Expand Down
19 changes: 19 additions & 0 deletions connect/resources/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from connect.models.product import Product
from connect.models.tier_config import TierConfig
from connect.resources.base import ApiClient
from connect.resources.marketplace import MarketplaceResource
from connect.resources.tier_account import TierAccountResource
from connect.rql import Query

Expand Down Expand Up @@ -47,6 +48,24 @@ def get_asset(self, asset_id):
text, code = ApiClient(self._config, 'assets/' + asset_id).get()
return Asset.deserialize(text)

def list_marketplaces(self, filters=None):
""" List the marketplaces.

:param dict|Query filters: Filters to pass to the request.
:return: List of marketplaces matching given filters.
:rtype: list[Marketplace]
"""
return MarketplaceResource(self._config).list(filters)

def get_marketplace(self, marketplace_id):
""" Obtains Marketplace object given its ID.

:param str marketplace_id: The id of the marketplace.
:return: The marketplace with the given id, or ``None`` if such marketplace does not exist.
:rtype: Marketplace|None
"""
return MarketplaceResource(self._config).get(marketplace_id)

def list_products(self, filters=None):
""" List the products.

Expand Down
18 changes: 18 additions & 0 deletions connect/resources/marketplace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-

# This file is part of the Ingram Micro Cloud Blue Connect SDK.
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.

from .base import BaseResource
from ..models import Marketplace


class MarketplaceResource(BaseResource):
""" Resource to work with :py:class:`connect.models.Marketplace` models.
:param Config config: Config object or ``None`` to use environment config (default).
"""
resource = 'marketplaces'
model_class = Marketplace

def __init__(self, config=None):
super(MarketplaceResource, self).__init__(config)
Binary file added tests/data/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions tests/data/response_marketplace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"id": "MP-12345",
"name": "France and territories",
"description": "Use the marketplace to distribute services in France and it's territories.",
"active_contracts": 34,
"icon": "/media/PA-123-123/marketplaces/MP-12345/image.png",
"owner": {
"id": "PA-123-123",
"name": "Awesome Provider"
},
"hubs": [
{
"hub": {
"id": "HB-1234-1234",
"name": "Staging OA 7.4"
},
"external_id": "20190101"
},
{
"hub": {
"id": "HB-4321-4321",
"name": "Staging OA 7.3"
},
"external_id": "70190121"
}
],
"countries": [
{
"id": "IN",
"name": "India",
"icon": "/media/countries/india.png",
"zone": "Asia"
}
]
}
41 changes: 40 additions & 1 deletion tests/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest

from connect.exceptions import ServerError
from connect.models import Asset, Product, TierConfig, ProductConfiguration
from connect.models import Asset, Marketplace, Product, TierConfig, ProductConfiguration
from connect.resources import Directory
from .common import Response, load_str

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


def _get_marketplace_response():
return _get_response_from_file('response_marketplace.json')


def _get_product_response():
return _get_response_from_file('response_product.json')

Expand Down Expand Up @@ -86,6 +90,41 @@ def test_get_asset_bad():
Directory().get_asset('AS-9861-7949-8492')


@patch('requests.get')
def test_list_marketplaces(get_mock):
get_mock.return_value = _get_array_response(_get_marketplace_response())
marketplaces = Directory().list_marketplaces()
assert isinstance(marketplaces, list)
assert len(marketplaces) == 1
assert isinstance(marketplaces[0], Marketplace)
assert marketplaces[0].id == 'MP-12345'

get_mock.assert_called_with(
url='http://localhost:8080/api/public/v1/marketplaces',
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
params={'limit': 100},
timeout=300)


@patch('requests.get')
def test_get_marketplace(get_mock):
get_mock.return_value = _get_marketplace_response()
marketplace = Directory().get_marketplace('MP-12345')
assert isinstance(marketplace, Marketplace)
assert marketplace.id == 'MP-12345'

get_mock.assert_called_with(
url='http://localhost:8080/api/public/v1/marketplaces/MP-12345',
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
timeout=300)


@patch('requests.get', MagicMock(return_value=_get_bad_response()))
def test_get_marketplace_bad():
with pytest.raises(ServerError):
Directory().get_marketplace('MP-00000')


@patch('requests.get')
def test_list_products(get_mock):
get_mock.return_value = _get_array_response(_get_product_response())
Expand Down