MWSResponse

New in version 1.0dev15: MWSResponse added

Warning

The following pertains to features added in v1.0dev15 related to MWS requests. These features are disabled by default. To use these features, set flag _use_feature_mwsresponse to True on an API class instance before making any requests:

api_class = Orders(...)
api_class._use_feature_mwsresponse = True

If the flag is False, all requests will return either DictWrapper or DataWrapper objects (deprecated); and parsed XML contents will be returned as an instance of ObjectDict (deprecated).

New features using MWSResponse and DotDict will become the default in v1.0.

MWSResponse acts as a wrapper for requests.Response objects returned from requests made to MWS. When initialized, the response content is automatically parsed for XML content, making it available as a DotDict instance in MWSResponse.parsed.

Parsed content for XML responses

All XML response content is automatically parsed using the xmltodict package. The parsed results are stored as a DotDict accessible from MWSResponse.parsed.

For more details on working with the parsed content, please see DotDict.

Original response access

As MWSResponse wraps a requests.Response object, all data and methods of that underlying object can be accessed from the MWSResponse instance using one of the following:

  • The MWSResponse.original attribute:

    response = api.foo_request(...)
    # response is an instance of MWSResponse
    
    response.original.status_code
    # 200
    response.original.headers
    # {'Content-Type': ...}
    
    response.original.text  # unicode
    # 'Hello world!'
    response.original.content  # bytes
    # b'Hello world!'
    
  • A number of shortcut properties available on MWSResponse itself:

    response.content      # response.original.content
    response.cookies      # response.original.cookies
    response.elapsed      # response.original.elapsed
    response.encoding     # response.original.encoding
    response.headers      # response.original.headers
    response.reason       # response.original.reason
    response.request      # response.original.request
    response.status_code  # response.original.status_code
    response.text         # response.original.text
    

    Each of these shortcuts is a read-only property, with the exception of response.encoding, which includes a setter for convenience when dealing with content encoding issues:

    response.encoding = "iso-8859-1"
    print(response.original.encoding)
    # "iso-8859-1"
    

MWSResponse API

New in version 1.0dev15.

class mws.MWSResponse(response, result_key=None, encoding=None, force_cdata=False)[source]

Wraps a requests.Response object and extracts some known data.

Particularly for XML responses, parsed contents can be found in the .parsed property as a DotDict instance.

Find metadata in .metadata, mainly for accessing .metadata.RequestId; or simply use the .request_id shortcut attr.

Parameters
  • response (request.Response) – Response object returned by a request sent to MWS.

  • result_key (str) – Key to use as the root for .parsed. Typically a tag in the root of the response’s XML document whose name ends in Result. Defaults to None, in which case the full document is presented when using .parsed.

  • force_cdata (bool) – Passed to xmltodict.parse() when parsing the response’s XML document. Defaults to False.

original: requests.Response

Instance of the original requests.Response object. Can be used to get or set data in the original response.

property encoding

Shortcut to .original.encoding. Can also be used as a setter, changing the encoding of the response. This then changes how content is decoded when using .text.

parse_response(force_cdata=False)[source]

Runs .text through xmltodict.parse(), storing the returned Python dictionary as ._dict.

If no XML errors occur during that process, constructs DotDict instances from the parsed XML data, making them available from .parsed and .metadata.

For non-XML responses, does nothing.

Parameters

force_cdata (bool) – Passed to xml_to_dict.parse() when parsing XML content. Defaults to False. Ignored for non-XML responses.

property parsed

Returns a parsed version of the response.

For XML documents, returns a DotDict of the parsed XML content, starting from ._result_key.

For all other types of responses, returns .text instead.

property metadata

Returns a DotDict instance from the response’s ResponseMetadata key, if present. Typically the only key of note here is .metadata.RequestId, which can also be accessed with .request_id.

property content

Shortcut to .original.content, which is bytes.

property cookies

Shortcut to .original.cookies.

property elapsed

Shortcut to .original.elapsed.

property headers

Shortcut to .original.headers.

property reason

Shortcut to .original.reason.

property request

Shortcut to .original.request.

property request_id

Returns the value of a RequestId from .metadata, if present, otherwise None.

property status_code

Shortcut to .original.status_code.

property text

Shortcut to .original.text, which is unicode.