|
| 1 | +Quickstart |
| 2 | +========== |
| 3 | + |
| 4 | +After setting up your credentials, you're ready to make the first call. |
| 5 | +This quickstart uses credentials passed as a dict, if you've setup a config file or environment variables, this can be omitted. |
| 6 | + |
| 7 | +.. code-block:: python |
| 8 | +
|
| 9 | + credentials = dict( |
| 10 | + refresh_token='your_refresh_token', |
| 11 | + lwa_app_id='your_lwa_app_id', |
| 12 | + lwa_client_secret='your_lwa_client_secret', |
| 13 | + aws_access_key='your_aws_access_key', |
| 14 | + aws_secret_key='your_aws_secret_key' |
| 15 | + ) |
| 16 | +
|
| 17 | +The default Marketplace this library uses is the US. |
| 18 | +You can pass the marketplace when initiating a new client, or set another default as environment variable. |
| 19 | +For example, to use the German Marketplace as the default, set `SP_API_DEFAULT_MARKETPLACE` as an environment variable: |
| 20 | + |
| 21 | +.. code-block:: bash |
| 22 | +
|
| 23 | + SP_API_DEFAULT_MARKETPLACE=DE |
| 24 | +
|
| 25 | +.. note:: |
| 26 | + |
| 27 | + All endpoints have the following signature and default values: |
| 28 | + |
| 29 | + |
| 30 | + .. code-block:: python |
| 31 | +
|
| 32 | + SomeClient( |
| 33 | + marketplace=Marketplaces.US, *, |
| 34 | + refresh_token=None, |
| 35 | + account='default', |
| 36 | + credentials=None, |
| 37 | + restricted_data_token=None |
| 38 | + ) |
| 39 | +
|
| 40 | +Example Usage |
| 41 | +------------- |
| 42 | + |
| 43 | +Let's download a single order, then a list of orders. |
| 44 | +First, create a client with your credentials and Marketplace: |
| 45 | + |
| 46 | +.. note:: |
| 47 | + |
| 48 | + You don't need to pass a marketplace if you want to request your default marketplace |
| 49 | + |
| 50 | + |
| 51 | +.. note:: |
| 52 | + |
| 53 | + You don't need to pass credentials if you've set them up as environment variables or in a config file |
| 54 | + |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + from sp_api.base import Marketplaces |
| 59 | + from sp_api.api import Orders |
| 60 | +
|
| 61 | + order_client = Orders(credentials=credentials, marketplace=Marketplaces.DE) |
| 62 | + order = order_client.get_order('your-order-id') |
| 63 | + print(order) # `order` is an `ApiResponse` |
| 64 | + print(order.payload) # `payload` contains the original response |
| 65 | +
|
| 66 | +
|
| 67 | +If you want to get a list of orders, you can make use of `load_all_pages`_, and optionally together with one of the `retry`_ decorators. |
| 68 | +Below code will print all orders for your credentials, for the last 7 days: |
| 69 | + |
| 70 | +.. _load_all_pages: https://sp-api-docs.saleweaver.com/utils/load_all_pages/ |
| 71 | +.. _retry: https://sp-api-docs.saleweaver.com/utils/retry/ |
| 72 | + |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + from datetime import datetime, timedelta |
| 77 | + from sp_api.base import Marketplaces |
| 78 | + from sp_api.api import Orders |
| 79 | + from sp_api.util import throttle_retry, load_all_pages |
| 80 | +
|
| 81 | +
|
| 82 | + @throttle_retry() |
| 83 | + @load_all_pages() |
| 84 | + def load_all_orders(**kwargs): |
| 85 | + """ |
| 86 | + a generator function to return all pages, obtained by NextToken |
| 87 | + """ |
| 88 | + return Orders().get_orders(**kwargs) |
| 89 | +
|
| 90 | +
|
| 91 | + for page in load_all_orders(LastUpdatedAfter=(datetime.utcnow() - timedelta(days=7)).isoformat()): |
| 92 | + for order in page.payload.get('Orders'): |
| 93 | + print(order) |
| 94 | +
|
| 95 | +
|
| 96 | +Creating a report is just as easy: |
| 97 | + |
| 98 | +.. note:: |
| 99 | + |
| 100 | + This time, `Reports` is using credentials from a config file (or environment variables), and the default marketplace |
| 101 | + |
| 102 | +.. code-block:: python |
| 103 | +
|
| 104 | + from datetime import datetime, timedelta |
| 105 | + from sp_api.api import ReportsV2 |
| 106 | + from sp_api.base.reportTypes import ReportType, Marketplaces |
| 107 | +
|
| 108 | + res = ReportsV2().create_report( |
| 109 | + reportType=ReportType.GET_FLAT_FILE_ALL_ORDERS_DATA_BY_LAST_UPDATE_GENERAL, |
| 110 | + # optionally, you can set a start and end time for your report |
| 111 | + dataStartTime=(datetime.utcnow() - timedelta(days=7)).isoformat() |
| 112 | + dataStartTime=(datetime.utcnow() - timedelta(days=1)).isoformat() |
| 113 | + ) |
| 114 | + print(res) |
| 115 | + print(res.payload) # object containing a report id |
0 commit comments