Refer:Bmob后端雲REST API接口文檔:http://docs.bmob.cn/data/Restful/a_faststart/doc/index.html
本文使用python對Bmob REST API的基本增刪改查接口進行封裝,方便在小程序開發時批量插入原始數據。
常用操作函數封裝
#!/usr/bin/python
# coding:utf-8
# Bmob后端雲基本REST API封裝
import requests
import json
# 每個應用都會有這兩個ID,以下方法如果不傳入這兩個參數,那么使用這里默認的值
APP_ID = 'XXX'
REST_API_KEY = 'XXX'
# 封裝rest api的get方法,根據對象ID獲取一條數據
# table_name:要查詢的表名
# object_id:要查詢的數據記錄的ID
def query(table_name,object_id,app_id = APP_ID,rest_api_key = REST_API_KEY):
# 構建請求頭
headers = {}
headers['X-Bmob-Application-Id'] = app_id
headers['X-Bmob-REST-API-Key'] = rest_api_key
# 構建url
url = 'https://api.bmob.cn/1/classes/{table_name}/{object_id}'.format(table_name = table_name,object_id = object_id)
# 發起請求
resp = requests.get(url,headers = headers,verify = False)
# 設置響應體編碼
resp.encoding = 'utf-8'
if resp and resp.status_code == 200:
return json.loads(resp.text)
return None
# 封裝rest api的post方法,插入一條記錄
# table_name:表名,如果表名還不存在,則先創建一個表再插入數據
# data:字典,要插入的記錄的各個字段的字段名和值
def insert(table_name,data,app_id = APP_ID,rest_api_key = REST_API_KEY):
# 構建請求頭
headers = {}
headers['X-Bmob-Application-Id'] = app_id
headers['X-Bmob-REST-API-Key'] = rest_api_key
headers['Content-Type'] = 'application/json'
# 構建url
url = 'https://api.bmob.cn/1/classes/{table_name}'.format(table_name = table_name)
# 發起請求
resp = requests.post(url,headers = headers,data = json.dumps(data),verify = False)
# 設置響應體編碼
resp.encoding = 'utf-8'
if resp and resp.status_code == 201:
print 'insert success!'
return json.loads(resp.text)
return None
# 封裝rest api的put方法,傳入記錄ID,修改一條數據
# table_name:要更新的表名
# object_id:要更新的數據記錄的ID
# data:字典類型,要更新的數據的鍵值對
def update(table_name,object_id,data,app_id = APP_ID,rest_api_key = REST_API_KEY):
# 構建請求頭
headers = {}
headers['X-Bmob-Application-Id'] = app_id
headers['X-Bmob-REST-API-Key'] = rest_api_key
headers['Content-Type'] = 'application/json'
# 構建url
url = 'https://api.bmob.cn/1/classes/{table_name}/{object_id}'.format(table_name = table_name,object_id = object_id)
# 發起請求
resp = requests.put(url,headers = headers,data = json.dumps(data),verify = False)
# 設置響應體編碼
resp.encoding = 'utf-8'
if resp and resp.status_code == 200:
print 'update {0} success!'.format(object_id)
return json.loads(resp.text)
return None
# 封裝rest api的delete方法,根據對象ID刪除一條記錄
# table_name:要刪除的記錄所在的表名
# object_id:要刪除的數據記錄的ID
def delete(table_name,object_id,app_id = APP_ID,rest_api_key = REST_API_KEY):
# 構建請求頭
headers = {}
headers['X-Bmob-Application-Id'] = app_id
headers['X-Bmob-REST-API-Key'] = rest_api_key
# 構建url
url = 'https://api.bmob.cn/1/classes/{table_name}/{object_id}'.format(table_name = table_name,object_id = object_id)
# 發起請求
resp = requests.delete(url,headers = headers,verify = False)
# 設置響應體編碼
resp.encoding = 'utf-8'
if resp and resp.status_code == 200:
print 'delete {0} success!'.format(object_id)
return json.loads(resp.text)
return None
# 查詢一個表中的所有數據
# table_name:要查詢的表名
def list(table_name,app_id = APP_ID,rest_api_key = REST_API_KEY):
# 構建請求頭
headers = {}
headers['X-Bmob-Application-Id'] = app_id
headers['X-Bmob-REST-API-Key'] = rest_api_key
# 構建url
url = 'https://api.bmob.cn/1/classes/{table_name}'.format(table_name = table_name)
# 發起請求
resp = requests.get(url,headers = headers,verify = False)
# 設置響應體編碼
resp.encoding = 'utf-8'
if resp and resp.status_code == 200:
return json.loads(resp.text)['results']
return None
# 批量操作:批量創建
# request_data結構:
'''
table_name = 'test'
{
"requests":[
{
"method":"POST",
"path":"/1/classes/{0}".format(table_name),
"body":{
"name":"Tom",
"age":18
}
},
{
"method":"POST",
"path":"/1/classes/{0}".format(table_name),
"body":{
"name":"John",
"age":21
}
}
]
}
'''
def batch_insert(request_data,app_id = APP_ID,rest_api_key = REST_API_KEY):
# 構建請求頭
headers = {}
headers['X-Bmob-Application-Id'] = app_id
headers['X-Bmob-REST-API-Key'] = rest_api_key
headers['Content-Type'] = 'application/json'
# url
url = 'https://api.bmob.cn/1/batch'
# 發起請求
resp = requests.post(url,data = json.dumps(request_data),headers = headers,verify = False)
# 設置響應體編碼
resp.encoding = 'utf-8'
if resp and resp.status_code == 200:
return json.loads(resp.text)
return None
調用示例
#!/usr/bin/python
# coding:utf-8
# 測試工具方法的使用
import bmob_base_utils as utils
def main():
# 測試query方法
#resp = utils.query(table_name = 'monthly', object_id = '2290ce60cc')
#print resp
# 測試insert方法
'''
data = {'name':'Ben','age':18}
resp = utils.insert(table_name = 'test',data = data)
print resp
'''
# 測試update方法
#data = {'age':999}
#utils.update(table_name = 'test',object_id = '79cfd8639b',data = data)
# 測試delete方法
#utils.delete(table_name = 'test', object_id = '79cfd8639b')
# 測試list方法
# print utils.list('test')
# 測試batch_insert方法
request_data = {}
request_data['requests'] = []
table_name = 'test'
data1 = {
"method":"POST",
"path":"/1/classes/{0}".format(table_name),
"body":{
"name":"Tom",
"age":18
}
}
data2 = {
"method":"POST",
"path":"/1/classes/{0}".format(table_name),
"body":{
"name":"John",
"age":21
}
}
request_data['requests'].append(data1)
request_data['requests'].append(data2)
utils.batch_insert(request_data)
if __name__ == '__main__':
main()
補充
-
查詢成功響應:200 OK
-
創建成功響應:201 Created
-
更新成功響應:200 OK
-
刪除成功響應:200 OK