Skip to content

Commit

Permalink
endpoints as dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
denisneuf committed Oct 1, 2021
1 parent d9dc00e commit 22e3035
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
12 changes: 6 additions & 6 deletions ad_api/base/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from .marketplaces import Marketplaces
import sys
import os
# import urllib.request
# import urllib.parse
import requests
from io import BytesIO
import gzip
Expand All @@ -27,12 +25,12 @@ class Client(BaseClient):
grantless_scope = ''
def __init__(
self,
marketplace: Marketplaces = Marketplaces.ES,
*,
refresh_token=None,
account='default',
marketplace: Marketplaces = Marketplaces.EU,
refresh_token=None,
credentials=None
):

super().__init__(account, credentials)
self.endpoint = marketplace.endpoint
self._auth = AccessTokenClient(refresh_token=refresh_token, account=account, credentials=credentials)
Expand All @@ -54,7 +52,6 @@ def auth(self) -> AccessTokenResponse:
@staticmethod
def _download(self, params: dict = None, headers=None) -> ApiResponse:

# logging.info(params)
location = params.get("url")

try:
Expand Down Expand Up @@ -184,6 +181,9 @@ def _download(self, params: dict = None, headers=None) -> ApiResponse:
next_token = None
return ApiResponse(error, next_token, headers=self.headers)

sys.exit()



def _request(self, path: str, *, data: str = None, params: dict = None, headers=None,
add_marketplace=True) -> ApiResponse:
Expand Down
8 changes: 4 additions & 4 deletions ad_api/base/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AdvertisingApiBadRequestException(AdvertisingApiException):
code = 400

def __init__(self, error):
super(SellingApiBadRequestException, self).__init__(error)
super(AdvertisingApiBadRequestException, self).__init__(error)


class AdvertisingApiForbiddenException(AdvertisingApiException):
Expand All @@ -24,7 +24,7 @@ class AdvertisingApiForbiddenException(AdvertisingApiException):
code = 403

def __init__(self, error):
super(SellingApiForbiddenException, self).__init__(error)
super(AdvertisingApiForbiddenException, self).__init__(error)


def get_exception_for_code(code: int):
Expand All @@ -39,5 +39,5 @@ def get_exception_for_code(code: int):

def get_exception_for_content(content: object):
return {
'UNAUTHORIZED': SellingApiUnauthorizedException
}.get(content.get('code'), SellingApiException)
'UNAUTHORIZED': AdvertisingApiForbiddenException
}.get(content.get('code'), AdvertisingApiException)
73 changes: 40 additions & 33 deletions ad_api/base/marketplaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,51 @@ class AWS_ENV(Enum):
PRODUCTION = "PRODUCTION"
SANDBOX = "SANDBOX"

config = dotenv_values(".env")

AWS_ENVIRONMENT = config.get('AWS_ENV')
class Marketplaces(Enum):
NA = {'sandbox': 'advertising-api-test.amazon.com',
'prod': 'advertising-api.amazon.com',
'currency': 'USD',
'token_url': 'api.amazon.com/auth/o2/token'}

BASE_URL_EU = "https://advertising-api-eu.amazon.com"
BASE_URL_US = "https://advertising-api.amazon.com"
EU = {'sandbox': 'advertising-api-test.amazon.com',
'prod': 'advertising-api-eu.amazon.com',
'currency': 'EUR',
'token_url': 'api.amazon.com/auth/o2/token'}

if AWS_ENVIRONMENT is not None:
logging.warning('Running in choosen mode : %s' % AWS_ENVIRONMENT)
GB = {'sandbox': 'advertising-api-test.amazon.com',
'prod': 'advertising-api-eu.amazon.com',
'currency': 'GBP',
'token_url': 'api.amazon.com/auth/o2/token'}

if AWS_ENVIRONMENT is None:
default_mode = os.environ["__MODE__"] = "SANDBOX"
AWS_ENVIRONMENT = default_mode
ES = {'sandbox': 'advertising-api-test.amazon.com',
'prod': 'advertising-api-eu.amazon.com',
'currency': 'EUR',
'token_url': 'api.amazon.com/auth/o2/token'}

if AWS_ENVIRONMENT is not None:
logging.warning('Running in default: %s' % default_mode )
DE = {'sandbox': 'advertising-api-test.amazon.com',
'prod': 'advertising-api-eu.amazon.com',
'currency': 'EUR',
'token_url': 'api.amazon.com/auth/o2/token'}

IT = {'sandbox': 'advertising-api-test.amazon.com',
'prod': 'advertising-api-eu.amazon.com',
'currency': 'EUR',
'token_url': 'api.amazon.com/auth/o2/token'}

if AWS_ENV(AWS_ENVIRONMENT) is AWS_ENV.SANDBOX:
BASE_URL_EU = "https://advertising-api-test.amazon.com"
BASE_URL_US = "https://advertising-api-test.amazon.com"
FR = {'sandbox': 'advertising-api-test.amazon.com',
'prod': 'advertising-api-eu.amazon.com',
'currency': 'EUR',
'token_url': 'api.amazon.com/auth/o2/token'}

class Marketplaces(Enum):
"""Enumeration for MWS marketplaces, containing endpoints and marketplace IDs.
Example, endpoint and ID for UK marketplace:
endpoint = Marketplaces.UK.endpoint
marketplace_id = Marketplaces.UK.marketplace_id
"""

US = (f"{BASE_URL_US}", 'EUR')
ES = (f"{BASE_URL_EU}", 'EUR')
GB = (f"{BASE_URL_EU}", 'GBP')
IT = (f"{BASE_URL_EU}", 'EUR')
FR = (f"{BASE_URL_EU}", 'EUR')
DE = (f"{BASE_URL_EU}", 'EUR')


def __init__(self, endpoint, currency):
"""Easy dot access like: Marketplaces.endpoint ."""
self.endpoint = endpoint
self.currency = currency
def __init__(self, info):

config = dotenv_values(".env")
AWS_ENVIRONMENT = config.get('AWS_ENV') or os.environ.get('API_PASSWORD')
if AWS_ENVIRONMENT=="PRODUCTION":
self.region_url = info.get('prod')
else:
self.region_url = info.get('sandbox')

self.endpoint = 'https://{}'.format(self.region_url)
self.currency = info.get('currency')

0 comments on commit 22e3035

Please sign in to comment.