我从python开始,并试图为ebay网络服务构建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)Where,关键字是唯一需要的字段。那么,我应该如何构造这个方法呢?这些方法可以是:
更新:
您在请求中看到的所有xml标记都需要由用户传递。但是关键字应该被传递,其他的也可能在需要的时候被通过。
有什么建议吗?
发布于 2011-06-03 06:21:41
一个好主意是将所有具有适当默认值(或仅为None默认值)的参数放在函数签名中。是的,它需要在函数本身中输入更多的内容,但是界面将是干净的,自我记录的,而且使用简单,因为您不必在ebay文档或函数源中查找可能的参数。它以后会节省你的时间。
发布于 2011-06-03 06:12:48
将消息建模为类如何?
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-03 06:58:17
我会为所有人使用命名参数。通过这样做,可以很容易地分配默认值,并迫使用户提供所需的参数(省略默认值)。
https://stackoverflow.com/questions/6223561
复制相似问题