Moltin Python开发包
moltin的Python项目详细描述
[网站](http://molt.in)
许可证:麻省理工学院
版本:1.0
说明
用于moltin电子商务api的python sdk
安装
$ pip install moltin
用法
使用client_id初始化moltin对象,然后 client_secret,以及可选的特定api版本(例如v1)。
frommoltin.moltinimportMoltinm=Moltin("YOUR_CLIENT_ID","YOUR_CLIENT_SECRET"[,version="v1"])
认证
要进行身份验证,请调用authenticate方法。
access_token=m.authenticate()# This returns an AccessToken object# access_token.token: the token string# access_token.has_expired(): has the token expired## The access token is automatically passed to subsequent requests,# so you shouldn't normally need to use the returned token# except when persisting in a session or db
使用用户名/密码进行身份验证:
access_token,refresh_token=m.authenticate(username="your_username",password="your_password")# refresh_token is a RefreshToken object.# refresh_token.token: the token string# Use this to re-authenticate without needing a user/pass
使用刷新令牌字符串进行身份验证:
access_token=m.authenticate(refresh_token="refresh_token_string")
一旦通过身份验证,访问令牌将自动传递给 请求。如果需要传入以前存储的令牌,请使用:
m.set_access_token("access_token_string")
在提出请求之前
进行api调用
有一种与大多数端点交互的简单方法:
product=m.Product# Creates a product wrapperproduct.list()# lists all productsproduct.create(params)# creates a product, params passed as a dictproduct.find(5)# finds product with id = 5product.find_by(params)# finds a single product by params passed as a dict,# e.g. {"title": "Banana"}product.update(5,params)# updates product with id = 5 with new paramsproduct.remove(5)# removes product with id = 5
sdk还提供了一种向 直接API端点
例如:
product=m.get('products/5')# get product with id = 5new_product=m.post('products',{"sku":"123456789","title":"My first product","slug":"my-first-product","price":9.99,"status":1,"category":2,"stock_level":15,"stock_status":6,"description":"This is my first product on Moltin","requires_shipping":0})# create a new productm.put('products/5',params)# update product with id = 5m.delete('products/5')# delete product with id = 5
有关更多示例,请参阅完整的api文档。