5

我从 python 开始并尝试为ebay Web 服务构建 XML 请求:

现在,我的问题是:

说,这是我的功能:

def findBestMatchItemDetailsAcrossStores():
     request = """<?xml version="1.0" encoding="utf-8"?>
     <findBestMatchItemDetailsAcrossStoresRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">
     <siteResultsPerPage>50</siteResultsPerPage>
     <entriesPerPage>50</entriesPerPage>
     <ignoreFeatured>true</ignoreFeatured>
     <keywords>ipod</keywords> <-----REQUIRED
     <itemFilter>
     <paramName>PriceMin</paramName>
     <paramValue>50</paramValue>
     <name>Currency</name>
     <value>USD</value>
     </itemFilter>
     <itemFilter>
     <paramName>PriceMax</paramName>
     <paramValue>100</paramValue>
     </itemFilter>
     </findBestMatchItemDetailsAcrossStoresRequest>"""
     return get_response(findBestMatchItemDetailsAcrossStores.__name__, request)

其中,关键字是唯一的必填字段。那么,我应该如何构造方法呢?方法可以是:

  1. 创建一个对象,将其传递给 func(object) :java方式
  2. 传递所有参数:func(a=val1, b=val2, c=val3, d=val4 etc)
  3. 使用 **kwargs 并相信调用函数的人,他将正确的键与值一起传递,因为我将使用键来实际构造 XML 标记。

更新:

您在请求中看到的所有 xml 标记都需要由用户传递。但是应该传递关键字,如果需要,可以传递其他关键字。

有什么建议么?

4

3 回答 3

7

一个好主意是将所有具有适当默认值(或只是None默认值)的参数放在函数签名中。是的,它需要在函数本身中输入更多内容,但界面将是干净的、自记录的且易于使用,因为您不必在 ebay 文档或函数源中查找可能的参数。这将在以后节省您的时间。

于 2011-06-03T06:21:41.273 回答
0

如何将消息建模为一个类?

class FindBestMatchItemDetailsAcrossStoresRequest:
     def __init__(self,keywords):
         self.keywords = keywords # required parameters in the constructor
         # set up the default values....etc
         self.siteResultsPerPage = 50 
         self.name = 'Currency'


     def send(self):
         # build message from self.xxx
         return get_response()


 #usage
 req = FindBestMatchItemDetailsAcrossStoresRequest('ipod')
 response = req.send()

 #usage with optional args

 req.siteResultsPerPage = 150
 response = req.send()
于 2011-06-03T06:12:48.760 回答
0

我会为所有人使用命名参数。通过这样做,很容易分配默认值,并强制用户提供所需的参数(通过省略默认值)

于 2011-06-03T06:58:17.243 回答