How can I return XML from boto calls?
Asked Answered
S

2

7

I am using boto 2.32.1 with Python 2.7.6. I am having problems with the list_orders function of boto's mws modules to get the XML data of my Amazon orders.

Here is the call I make:

response = connection.list_orders(CreatedAfter='2014-08-26T05:53:44+00:00', 
   MarketplaceId=['XXXXXXXXXXXXX'])

Here is the value of response (with personal information removed obviously)

ListOrdersResponse{u'xmlns': u'https://mws.amazonservices.com/Orders/2013-09-01'}
(ListOrdersResult: ListOrdersResult{}
(CreatedBefore: u'2014-08-26T10:06:10Z', Orders: ^Orders^{}
(Order: [Order{}(***ORDER INFORMATION***)])), 
ResponseMetadata: ^ResponseMetadata^{}(RequestId: u'xxxxxxxxxx'))

This isn't ideal because I have another module for eBay sales that works with the XML returned by ebaysdk-python. Amazon formats their XML similarly to eBay so if I could get the XML returned by Amazon instead of the format above, it would be brilliant.

I have noticed that if I use boto.set_stream_logger('boto'), it will print the XML returned so maybe I could take advantage of that somehow?

This is what I would like to be returned:

<ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
  <ListOrdersResult>
    <Orders>
      <Order>
        ***ORDER INFORMATION***
      </Order>
    </Orders>
    <CreatedBefore>2014-08-26T10:18:59Z</CreatedBefore>
  </ListOrdersResult>
  <ResponseMetadata>
    <RequestId>XXXXXXXXXXXXXXXXXXXXXXXXXXXXX</RequestId>
  </ResponseMetadata>
</ListOrdersResponse>

I could then use Python's xml.etree.ElementTree to parse and work with the data.

Straightaway answered 26/8, 2014 at 10:28 Comment(1)
Hi I too want to get orders from mws. but keep running into errors. If it is okay with you can you share your code? It can help me on the problem.ThanksDomingodominguez
A
3

I don't think there's an officially supported method to do this, but you can do this to get the raw XML response back easily:

# Set up
from boto.mws.connection import MWSConnection
MWSConnection._parse_response = lambda s, x, y, z: z

# Usage
result = az.get_matching_product_for_id(MarketplaceId="ATVPDKIKX0DER",
                                            SearchIndex="Books",
                                            IdType="ASIN",
                                            IdList=[0439023521])
# <?xml version="1.0"?>\n<GetMatchingProductForIdResponse xmlns...
Accad answered 4/7, 2015 at 15:45 Comment(0)
S
0

For boto3 and s3, based on docs:

import boto3

s3 = boto3.client('s3')

# Create a function that prints the after-call event args.
def print_after_call_args(http_response, parsed, model, **kwargs):
    #print xml response
    print(http_response.text) 

# Register the function to an event
s3.meta.events.register('after-call.s3.ListObjectsV2', print_after_call_args)

s3.list_objects_v2(Bucket='my-bucket')

# <?xml version="1.0" encoding="UTF-8"?>...
Sold answered 13/5 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.