python-amazon-mws is a Python connector to Amazon Marketplace Web Services (or MWS). It provides a simple way to build and send requests to MWS, allowing access to all that MWS can do from your Python application.
We're working on new features in the run-up to releasing v1.0. If you are using the latest develop branch version of the package, you can help us test these new features in your own environment.
- Dependencies have been updated: you may need to re-install requirements when upgrading.
DictWrapperandDataWrapperare deprecated, and will be removed in v1.1.- Starting in v1.0,
mws.response.MWSResponsewill be returned from all requests, instead.
- Starting in v1.0,
ObjectDictand its aliasobject_dictare deprecated, and will be removed in v1.1.- The
.parsedinterface ofDictWrapperandDataWrapperis preserved inMWSResponse, but will return an instance ofmws.utils.collections.DotDictinstead ofObjectDict.DotDictis a more general-purpose object that subclassesdictand provides a similar interface, while still allowing keys to be accessed as attr 8000 ibutes. New features of this object include the ability to assign values to existing keys, and anydictassigned to a key in aDotDictwill automatically be wrapped in its ownDotDictwith no need for additional processing.
- The
XML2Dictand its aliasxml2dictare deprecated, and will be removed in v1.1.- We will no longer perform XML parsing with our own methods. Instead, we are adding a dependency to
xmltodict, which performs the same task a bit more cleanly. - Parsed content will no longer include superfluous extra dictionaries with
valuekeys. If your code looks for thevaluekey, you may start seeing errors when testing new features.
- We will no longer perform XML parsing with our own methods. Instead, we are adding a dependency to
All features related to deprecations in v1.0dev15 are locked behind a feature flag. Unless you explicitly set the feature flag as follows, your application should operate the same as before this update.
If your application worked on a prior develop version and breaks when upgrading to v1.0dev15, please raise an issue so we may investigate.
To enable and test the new features, first instantiate your API class as normal, then set _use_feature_mwsresponse to True on the API instance:
from mws import Orders
api = Orders(...)
api._use_feature_mwsresponse = True
# Requests can be sent as normal:
response = api.list_orders()
# `response` should be an instance of `MWSResponse`,
# instead of the deprecated `DictWrapper` or `DataWrapper`.
# Use `.parsed` as before:
for order in response.parsed.Orders.Order:
print(order.AmazonOrderId)
# etc.With this flag set, any request made through api will be wrapped in the new MWSResponse object; XML content will be parsed by the xmltodict dependency; and response.parsed will return a DotDict instance with your parsed data.
Note: While
MWSResponsemaintains the same.parsedinterface, other interfaces have changed compared toDictWrapperandDataWrapper. For instance,DictWrapper.originalreturned the original bytes of the response; whereasMWSResponse.originalreturns the originalrequests.Responseinstance. This provides better access to the full range of that response's data. So, to get the bytes content, you can useMWSResponse.original.content.
Additionally,
MWSResponseincludes shortcut methods to some of the properties ofrequests.Response, including.content,.text,.status_code,.headers, etc. So, you can useMWSResponse.contentas an equivalent to callingMWSResponse.original.content.
More details will be provided in a documentation update, coming soon.
⚡ Thank you! ⚡
Two versions are currently available:
- Installing
mwsfrom PyPI, you will have version 0.8.x, which is built from ourmasterbranch.- This is a close match to the original package by czpython, with some small tweaks to add critical functionality.
- Supports Python 2.7 and 3.4+.
- The updated 1.0devXY version must be installed from this repo's
developbranch.- This includes additional API coverage that may be missing in 0.8.x, as well as other new features.
- Some methods have new or updated arguments compared to 0.8.x, and much of the original monolithic
mwsmodule has been broken down into separate components (such as themws.apiscollection of modules). - Supports Python 3.5+.
Warning: If you are using version 0.8.x in a production system, note that our eventual 1.0 release will be backwards-incompatible, and may break programs that depend on the 0.8.x version. We advise users pin their Pip-installed version in requirements as
mws~=0.8.9.
Install the mws package using Pip:
pip install mwsAlternatively, you can install direct from this repo's master branch, like so:
pip install git+https://github.com/python-amazon-mws/python-amazon-mws.git@master#egg=mwsOur develop version can be installed directly from the repo using:
pip install git+https://github.com/python-amazon-mws/python-amazon-mws.git@develop#egg=mwsNote that code may be updated at any time as development continues, so please use at your own risk.
Export your API credentials as environment variables in your shell.
export MWS_ACCOUNT_ID=...
export MWS_ACCESS_KEY=...
export MWS_SECRET_KEY=...Now you can experiment with the API from within an interactive Python shell e.g.
>>> import mws, os
>>> orders_api = mws.Orders(
... access_key=os.environ['MWS_ACCESS_KEY'],
... secret_key=os.environ['MWS_SECRET_KEY'],
... account_id=os.environ['MWS_ACCOUNT_ID'],
... region='UK', # defaults to 'US'
... )
>>> service_status = orders_api.get_service_status()
>>> service_status
<mws.mws.DictWrapper object at 0x1063a2160>
>>> service_status.original
'<?xml version="1.0"?>\n<GetServiceStatusResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">\n <GetServiceStatusResult>\n <Status>GREEN</Status>\n <Timestamp>2017-06-14T16:39:12.765Z</Timestamp>\n </GetServiceStatusResult>\n <ResponseMetadata>\n <RequestId>affdec68-05d2-4bc5-a8a4-bb40f307dd6b</RequestId>\n </ResponseMetadata>\n</
GetServiceStatusResponse>\n'
>>> service_status.parsed
{'value': '\n ', 'Status': {'value': 'GREEN'}, 'Timestamp': {'value': '2017-06-14T16:39:12.765Z'}}
>>> service_status.response
<Response [200]>All dependencies for developing on python-amazon-mws, including testing and documentation building, can be installed using:
pip install -r requirements-dev.txtThis project uses the pre-commit framework. This framework installs a Git pre-commit hook that runs scripts as detailed in .pre-commit-config.yaml on commits in your local clone of the repo. These hooks are used to ensure code quality when contributing to the project.
The pre-commit package should already be installed along with installing development requirements (above), but is "opt-in" by design. We highly encourage using it in your local environment. To do so, install the hooks with:
pre-commit installPre-commit hook scripts will only run against the files that you change within a commit for speed purposes. To run the hooks against all files in the project, use:
pre-commit run --all-filesTests are run with pytest. To run tests, simply install our dev requirements and then run:
pytestSee pytest docs for details on selecting specific tests, rather than the entire test suite, as needed.
We also perform coverage reporting using pytest-cov. You can generate a coverage report locally using:
pytest --cov=mwsYou may also want to generate a local HTML report to navigate the code and see where coverage is missing:
pytest --cov=mws --cov-report htmlThis will create a htmlcov/ directory, and you can open htmlcov/index.html to view the report in your browser.
The test suite and coverage reporting to Codecov are run automatically in the repo on pushes and pull requests, using GitHub Actions workflows. We test on latest versions of Python 3.5+, and on latest Ubuntu, Mac, and Windows OSes.
Docs are built using Sphinx.
To build docs locally, use make:
make htmlThe output HTML documentation will be in docs/build/.
To run a live reloading server serving the HTML documentation (on localhost:8000 or 127.0.0.1:8000 by default):
make livehtmlmake may not be available on Windows, but you can still build documentation with sphinx-build and sphinx-autobuild.
To build the docs locally, use sphinx-build:
sphinx-build -b html docs/source docs/buildYou can also run a live-reloading server using sphinx-autobuild (on localhost:8000 or 127.0.0.1:8000 by default):
sphinx-autobuild docs/source docs/buildPlease make pull requests to develop. Code coverage isn't necessary but encouraged where possible.
For support using the package, please join our Slack and post in the #help channel.
For support using MWS itself, we advise using the MWS documentation