用python实现rest api客户端
fulfil_client的Python项目详细描述
fulfil io python客户端
在python中实现rest api客户端
- 免费软件:ISC许可证
- 文档:https://fulfil-python-api.readthedocs.org。
- 示例:https://github.com/fulfilio/fulfil-python-api/tree/master/examples。
功能
- 能够调用模型
安装
pip install fulfil_client
快速启动
fromfulfil_clientimportClientclient=Client('<subdomain>','<api_key>')Product=client.model('product.product')# find productssome_products=Product.find()# find products that have a name similar to iphoneiphones=Product.find(['name','ilike','iphone'])
联系人
联系人可以有多个地址和联系机制,即电话, 电子邮件。
fromfulfil_clientimportClientclient=Client('<subdomain>','<api_key>')Contact=client.model('party.party')Country=client.model('country.country')Subdivision=client.model('country.subdivision')country_usa,=Country.find([('code','=','US')])state_california,=Subdivision.find([('code','=','US-CA')])# Creating a contact with address and contact mechanismscontact,=Contact.create([{'name':'Jon Doe','addresses':[('create',[{'name':'Jone Doe Apartment','street':'9805 Kaiden Grove','city':'New Leland','zip':'57726','country':country_usa['id'],'subdivision':state_california['id']}])],'contact_mechanisms':[('create',[{'type':'phone','value':'243243234'},{'email':'email','value':'hello@jondoe.com'}])]}])printcontact# Searching for a contactcontact,=Contact.find([('name','=','Jon Doe')])printcontact# Get a contact by IDcontact=Contact.get(contact['id'])printcontact
产品
产品按模板分组,模板共享公共信息 产品A.K.A.变型。
fromdecimalimportDecimal# Creating a Product TemplateTemplate=client.model('product.template')iphone,=Template.create([{'name':'iPhone','account_category':True,}])# Creating productsProduct=client.model('product.product')iphone6,=Product.create([{'template':iphone['id'],'variant_name':'iPhone 6','code':'IPHONE-6','list_price':Decimal('699'),'cost_price':Decimal('599'),}])# Another variationiphone6s,=Product.create([{'template':iphone['id'],'variant_name':'iPhone 6S','code':'IPHONE-6S','list_price':Decimal('899'),'cost_price':Decimal('699'),}])
销售
contact=Contact.get(contact['id'])iphone6=Product.get(iphone6['id'])iphone6s=Product.get(iphone6s['id'])# Creating a SaleSale=client.model('sale.sale')sale,=Sale.create([{'party':contact['id'],'shipment_address':contact['addresses'][0],'invoice_address':contact['addresses'][0],'lines':[('create',[{'product':iphone6['id'],'description':iphone6['rec_name'],'unit':iphone6['default_uom'],'unit_price':iphone6['list_price'],'quantity':3},{'product':iphone6s['id'],'description':iphone6s['rec_name'],'unit':iphone6['default_uom'],'unit_price':iphone6s['list_price'],'quantity':1}])]}])
获取交互式报表(按月销售)
可以获取报表数据(包括呈现)信息 通过API。
下面是按月份获取销售报告的示例代码。
report=client.interactive_report('sales_by_month.ireport')data=report.execute(start_date=date(2017,1,1),end_date=date(2017,12,1))
使用会话验证
fromfulfil_clientimportClient,SessionAuthclient=Client('subdomain')user_id,session=client.login('username','password')client.set_auth(SessionAuth(user_id,session))
使用承载认证
fromfulfil_clientimportClient,BearerAuthclient=Client('subdomain')client.set_auth(BearerAuth(bearer_token))
使用OAuth会话
烧瓶示例
fromfulfil_client.oauthimportSessionfromfulfil_clientimportClient,BearerAuthSession.setup(CLIENT_ID,CLIENT_SECRET)fulfil_session=Session('localhost')# Provide subdomain@app.route('/')defindex():callback_url=url_for('authorized')if'oauth_token'notinsession:authorization_url,state=fulfil_session.create_authorization_url(redirect_uri=callback_url,scope=['user_session'])session['oauth_state']=statereturnredirect(authorization_url)client=Client('subdomain')client.set_auth(BearerAuth(session['oauth_token']['access_token']))Party=client.model('party.party')returnjsonify(Party.find())@app.route('/authorized')defauthorized():"""Callback route to fetch access token from grant code """token=fulfil_session.get_token(code=request.args.get('code'))session['oauth_token']=tokenreturnjsonify(oauth_token=token)
测试
libary还提供了一个由mock库支持的mock函数 Python。
例如,如果要测试下面的函数
defapi_calling_method():client=fulfil_client.Client('apple','apples-api-key')Product=client.model('product.product')products=Product.search_read_all([],None,['id'])Product.write([p['id']forpinproducts],{'active':False})returnclient
然后测试用例可以模拟api调用
deftest_mock_1():withMockFulfil('fulfil_client.Client')asmocked_fulfil:Product=mocked_fulfil.model('product.product')# Set the return value of the search call without# hitting the server.Product.search_read_all.return_value=[{'id':1},{'id':2},{'id':3},]# Call the functionapi_calling_method()# Now assertProduct.search_read_all.assert_called()Product.search_read_all.assert_called_with([],None,['id'])Product.write.assert_called_with([1,2,3],{'active':False})
返回的产品对象是一个mock.mock对象,支持所有 在assertions supported中 通过python模拟对象。
学分
fulfil.io公司
历史记录
0.2.0(2016-6-22)
更改search方法的行为。
该方法现在反映了fulfil search api,它返回 身份证。返回id为且 记录名(记录名)现在称为查找。
0.1.1(2016-3-3)
- 添加方法以创建资源。
- 添加有关如何创建联系人、产品和销售的示例。
0.1.0(2016-1-22)
- pypi上的第一个版本。