2010-12-07 36 views
4

这是我问的问题的前奏:我已经开始为我的公司构建一个网关不可知的支付API。目前我只编写代码来支持Authorize.net,并希望得到一些关于Python程序员的API设计清晰度的反馈,这些反馈比我的经验多一点。这是一个Pythonic API设计策略吗?

我选择自己推出,因为其他软件包现在感觉更像是一个想法,或者是Authorize.net特定的(我想用更干净的界面编写更通用的包)。我从包中获得了一些灵感(pythorize),但不喜欢他们的API。

在我开始描述自己在做什么之前,下面是该软件包的bitbucket上公共存储库的链接:paypy(请注意那些可能想要使用它的代码:代码稳定,但文档严重缺乏)。

我目前的策略是使用嵌套字典并将其传递给付款方法类的构造函数。创建于Authorize.net CIM API一个新的用户配置文件的例子:

>>> options = {'tran_key' : 'test_tran_key', 
...   'login' : 'developer_login', 
...   'testing' : True, 
...   'validation': 'testMode', 
...   'customer': {'description': 'Some description of the customer profile', 
...       'id'   : 22, 
...       'email'  : '[email protected]'}, 
...   'billing': [{'type': 'individual', 
...       'profile': {'city'  : 'Carlsbad', 
...          'state'  : 'California', 
...          'zip'  : '92009', 
...          'firstname' : 'John', 
...          'address' : '12 Alicante Rd. Suite 9', 
...          'lastname' : 'Doe', 
...          'country' : 'USA', 
...          'phone'  : '(858) 557-2674'}, 
...       'payment': {'card': {'ccv'  : '524', 
...            'number'  : '4111111111111111', 
...            'expiration' : '2014-04'}}}, 
...      {'type' : 'individual', 
...       'profile' : {'city'  : 'Las Vegas', 
...          'state'  : 'Nevada', 
...          'zip'  : '79112', 
...          'firstname' : 'John', 
...          'address' : '78 Cloud Front', 
...          'lastname' : 'Doe', 
...          'country' : 'USA', 
...          'phone'  : '(858) 557-2674'}, 
...       'payment': {'card': {'ccv'  : '499', 
...            'number'  : '4111111111111111', 
...            'expiration' : '2012-11'}}}, 
...      {'profile': {'city'  : 'Carlsbad', 
...          'state'  : 'California', 
...          'zip'  : '92009', 
...          'firstname' : 'John', 
...          'address' : '12 Alicante Rd. Suite 9', 
...          'lastname' : 'Doe', 
...          'company' : 'Xmarks', 
...          'country' : 'USA', 
...          'phone'  : '(858) 557-2674'}, 
...       'payment': {'bank': {'name_on_account' : 'John Doe', 
...            'account'   : '829330184383', 
...            'type'   : 'checking', 
...            'name'   : 'Bank of America', 
...            'routing'   : '122400724'}}}], 
...   'shipping': [{'city'  : 'Carlsbad', 
...       'state'  : 'California', 
...       'zip'  : '92009', 
...       'firstname' : 'John', 
...       'address' : '12 Alicante Rd. Suite 9', 
...       'lastname' : 'Doe', 
...       'country' : 'USA', 
...       'phone'  : '(858) 557-2674'}]} 
>>> profile = Profile(options) 
>>> result = profile.create() 
>>> result.code 
'I00001' 
>>> print 'Customer Profile ID:' + str(result) 
Customer Profile ID: 2758851 
>>> print 'Customer Payment Profile IDs:' + repr(result.payment_ids) 
Customer Payment Profile IDs: ['2380878', '2380879', '2380880'] 
>>> print 'Customer Shipping Profile IDs:' + repr(result.shipping_ids) 
Customer Shipping Profile IDs: ['2427568'] 
>>> 
>>> 
>>> options = {'id'  : str(result), 
...   'tran_key' : '86U5pvA9TcxZ5b8D', 
...   'testing' : True, 
...   'login' : '5b3PhGX68'} 
>>> profile = Profile(options) 
>>> result = profile.remove() 
>>> result.code 
'I00001' 
>>> ^D 

你会发现我用一对夫妇的魔术方法(如海峡,等...)的结果对象。我也使用AIM和ARB方法的字典策略,并认为这是向付款API传递“选项”的最简单方法 - 因为在某些时候会有适用于GoogleCheckout,Paypal等的适配器...

我想的另一个想法是使用描述符和对象而不是字典来将选项数据传递给适配器。

与所有支付网关API(特别是PayPal和Authorize.net's)一样,接口往往有点杂乱,并且没有任何标准化,所以很难避免一些网关依赖选项。

+1

你为什么在交互式控制台中这样做? – 2010-12-07 19:57:28

+0

其实我没有。这是一个测试的摘录 - 我只是格式化了代码,输出的结果看起来像是在交互式提示符下完成的,因此它会更容易理解。 – Ixmatus 2010-12-07 21:28:35

回答

3

深嵌套字典在Python中可能并不罕见,也许他们是“Pythonic”,但它确实不错,因为我认为它不是Pythonic。

我想做一个类的嵌套层次。这将更加清晰,国际海事组织,也有让你做类型检查的好处。

事实上,我可能会使用一些模式模块来做到这一点。

你应该如何输入数据?人们合理不应该输入Python代码,对吧?