Skip to content

Commit

Permalink
multi account support & config file support && param support
Browse files Browse the repository at this point in the history
  • Loading branch information
saleweaver committed Jan 28, 2021
1 parent c4e3c70 commit 4866baf
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 33 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ Orders().get_orders(CreatedAfter=(datetime.utcnow() - timedelta(days=7)).isoform
Orders(account='ANOTHER_ACCOUNT').get_orders(CreatedAfter=(datetime.utcnow() - timedelta(days=7)).isoformat())
```

#### Credentials by params:

Pass a dict like below to the client:

```
credentials=dict(
refresh_token='<refresh_token>',
lwa_app_id='<lwa_app_id>',
lwa_client_secret='<lwa_client_secret>',
aws_secret_key='<aws_secret_access_key>',
aws_access_key='<aws_access_key_id>',
role_arn='<role_arn>',
)
Orders(credentials=credentials).get_orders(CreatedAfter=(datetime.utcnow() - timedelta(days=7)).isoformat())
```

The refresh token can be passed directly to the client, too. You don't need to pass the whole credentials if all that changes is the refresh token.

Credentials are looked up in the following order:
1. Credential dict
2. Env variables
3. Config File

---
### DISCLAIMER
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='python-amazon-sp-api',
version='0.1.1',
version='0.1.2',
install_requires=[
"requests",
"six~=1.15.0",
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class Catalog(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@sp_endpoint('/catalog/v0/items/{}')
def get_item(self, asin, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/feeds/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@


class Feeds(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)


@sp_endpoint('/feeds/2020-09-04/feeds', method='POST')
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/finances/finances.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class Finances(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@sp_endpoint('/finances/v0/orders/{}/financialEvents')
def get_financial_events_for_order(self, order_id, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/notifications/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Notifications(Client):
/notifications/v1/destinations/{destinationId}
"""

def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@deprecated
def add_subscription(self, notification_type: NotificationType or str, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/orders/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


class Orders(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@sp_endpoint('/orders/v0/orders')
def get_orders(self, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/product_fees/product_fees.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


class ProductFees(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@sp_endpoint('/products/fees/v0/listings/{}/feesEstimate', method='POST')
def get_product_fees_estimate_for_sku(self, seller_sku, price: float, shipping_price=None, currency='USD',
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/products/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


class Products(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

def get_product_pricing_for_skus(self, seller_sku_list, **kwargs):
"""
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/reports/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


class Reports(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@sp_endpoint('/reports/2020-09-04/reports', method='POST')
def create_report(self, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/sales/sales.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


class Sales(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@sp_endpoint('/sales/v1/orderMetrics')
def get_order_metrics(self, interval: tuple, granularity: Granularity, granularityTimeZone: str = None, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions sp_api/api/sellers/sellers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class Sellers(Client):
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default'):
super().__init__(marketplace, refresh_token, account)
def __init__(self, marketplace=Marketplaces.US, *, refresh_token=None, account='default', credentials=None):
super().__init__(marketplace, refresh_token, account, credentials)

@sp_endpoint('/sellers/v1/marketplaceParticipations')
def get_marketplace_participation(self, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions sp_api/auth/access_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class AccessTokenClient(BaseClient):
grant_type = 'refresh_token'
path = '/auth/o2/token'

def __init__(self, refresh_token=None, account='default'):
super().__init__(account)
def __init__(self, refresh_token=None, account='default', credentials=None):
print('access', credentials)
super().__init__(account, credentials)
self.cred = Credentials(refresh_token, self.credentials)

def get_auth(self) -> AccessTokenResponse:
Expand Down
4 changes: 2 additions & 2 deletions sp_api/base/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class BaseClient:
content_type = 'application/x-www-form-urlencoded;charset=UTF-8'
user_agent = 'python-sp-api'

def __init__(self, account='default'):
def __init__(self, account='default', credentials=None):
try:
import pkg_resources
version = pkg_resources.require("python-amazon-sp-api")[0].version
self.user_agent += f'-{version}'
except:
pass
self.credentials = CredentialProvider(account).credentials
self.credentials = CredentialProvider(account, credentials).credentials
8 changes: 4 additions & 4 deletions sp_api/base/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class Client(BaseClient):
def __init__(self,
marketplace: Marketplaces,
refresh_token=None,
account='default'
account='default',
credentials=None
):
super().__init__(account)
super().__init__(account, credentials)
self.boto3_client = boto3.client(
'sts',
aws_access_key_id=self.credentials.aws_access_key,
Expand All @@ -37,8 +38,7 @@ def __init__(self,
self.endpoint = marketplace.endpoint
self.marketplace_id = marketplace.marketplace_id
self.region = marketplace.region
self._auth = AccessTokenClient(refresh_token, account)

self._auth = AccessTokenClient(refresh_token=refresh_token, account=account, credentials=credentials)

def set_role(self):
role = self.boto3_client.assume_role(
Expand Down
12 changes: 10 additions & 2 deletions sp_api/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ class MissingCredentials(Exception):
class CredentialProvider:
credentials = None

def __init__(self, account='default'):
def __init__(self, account='default', credentials=None):
self.account = account
self.from_env()
if credentials:
print('hier')
self.credentials = self.Config(**credentials)
missing = self.credentials.check_config()
if len(missing):
raise MissingCredentials('Your credentials are incomplete!')
else:
print('hhhkalsflkajsfdlö')
self.from_env()

def from_env(self):
account_data = dict(
Expand Down
4 changes: 2 additions & 2 deletions tests/api/sales/test_sales.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import pytz

from sp_api.api import Sales
from sp_api.base import Granularity, Marketplaces
from sp_api.base import Granularity, Marketplaces, CredentialProvider
import logging

tz = pytz.timezone('US/Central')
fmt = '%Y-%m-%dT%H:%M:%S%z'

interval = ( datetime.now(tz) - timedelta(days=185) ), ( datetime.now(tz) )
interval = (datetime.now(tz) - timedelta(days=185)), (datetime.now(tz))


def test_sales_granularity_hour():
Expand Down

0 comments on commit 4866baf

Please sign in to comment.