Moltin Python SDK
项目描述
[网站] ( http://molt.in )
执照:麻省理工学院
版本:1.0
描述
Moltin 电子商务 API 的 Python SDK
安装
$ pip install moltin
用法
使用您的client_id和 client_secret以及可选的特定 API 版本(例如v1 )初始化 Moltin 对象。
from moltin.moltin import Moltin
m = Moltin("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET"[, version="v1"])
验证
要进行身份验证,请调用身份验证方法。
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
使用 refresh_token 字符串进行身份验证:
access_token = m.authenticate(refresh_token="refresh_token_string")
一旦通过身份验证,访问令牌就会自动传递给每个请求。如果您需要传入以前存储的令牌,请使用:
m.set_access_token("access_token_string")
在提出请求之前
进行 API 调用
有一种与大多数端点交互的简单方法:
product = m.Product # Creates a product wrapper
product.list() # lists all products
product.create(params) # creates a product, params passed as a dict
product.find(5) # finds product with id = 5
product.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 params
product.remove(5) # removes product with id = 5
SDK 还提供了一种直接向 API 端点发出 get、post、put 和 delete 请求的方法
例如:
product = m.get('products/5') # get product with id = 5
new_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 product
m.put('products/5', params) # update product with id = 5
m.delete('products/5') # delete product with id = 5
有关更多示例,请参阅完整的 API 文档。