欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > Bmob基本REST API的python封装

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:505

HTML5影视传媒文化公司类网

2020-05-12   浏览:502

Bmob基本REST API的python封装

发布时间:2020-11-18  

本文使用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

调用示例

本文标签

: