Skip to main content

Rainbond python cloud native development base library

Project description

Rainbond Python

基于 Rainbond 云原生平台和 Flask 框架的 Python 云原生开发基础库

使用说明

快速开始创建一个 Python 云原生组件:

rainbond -c demo-component

Parameter

处理请求与响应参数的通用类

from rainbond_python.parameter import Parameter

获取请求参数

通过 Parameter 类实例,可以获取以下信息:

  • parameter.method: 请求类型
  • parameter.headers: 请求头
  • parameter.param_url: URL中传递的参数
  • parameter.param_json: Json请求中的参数
  • parameter.param_form: 表单请求中的参数

所有信息均为字典类型,通过 json.dumps() 可以直接作为响应返回:

@app.route('/api/1.0/demo', methods=['GET', 'POST', 'PUT', 'DELETE'])
def api_demo():
    parameter = Parameter(request)
    if parameter.method == 'GET':
        return json.dumps(parameter.param_url, ensure_ascii=False), 200, []
    elif parameter.method == 'POST':
        return json.dumps(parameter.param_json, ensure_ascii=False), 200, []
    elif parameter.method == 'PUT':
        return json.dumps(parameter.param_json, ensure_ascii=False), 200, []
    elif parameter.method == 'DELETE':
        return json.dumps(parameter.param_json, ensure_ascii=False), 200, []

校验参数内容

通过 Parameter 类的 verification() 方法,可以判断参数字典是否符合要求:

    elif parameter.method == 'POST':
        if parameter.verification(checking=parameter.param_json, verify={'name': str, 'age': int}):
            return '请求参数正确', 200, []
        else:
            return '请求参数错误', 400, []

其中 checking 参数是需要校验的参数字典,通常传递 parameter.param_urlparameter.param_jsonparameter.param_form。第二个 verify 参数则是校验内容字典,需要指定 参数名参数类型 作为字典项。

DBConnect

处理 MongoDB 读写行为的通用类

from rainbond_python.db_connect import DBConnect
db = DBConnect(db='db_name', collection='collection_name')

写文档

insert_dict = {'name': 'Xiao Ming', 'age': 23}
if db.write_one_docu(docu=insert_dict):
    print('Insert success')
else:
    print('Insert failure')

文档是否存在

examine_dict = {'name': 'Xiao Ming'}
if db.does_it_exist(docu=examine_dict):
    print('Docu already exists')
else:
    print('Docu does not exist')

更新文档

更新单个匹配文档
find_dict = {'name': 'Xiao Ming'}
modify_dict = {'$set': {'name': 'Xiao Hong'}}
if db.update_docu(find_docu=find_dict, modify_docu=modify_dict):
    print('Update success')
else:
    print('Update failure')
更新全部匹配文档
find_dict = {'age': 23}
modify_dict = {'$set': {'name': '23 year old'}}
if db.update_docu(find_docu=find_dict, modify_docu=modify_dict, many=True):
    print('Update all success')
else:
    print('Update all failure')

参考

  • Restful API : 具体的组件API开发标准
  • 12 Factor : 符合十二要素的才是云原生应用
  • RainBond : 一个开源的云原生平台

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rainbond-python-0.2.9.tar.gz (8.3 kB view details)

Uploaded Source

File details

Details for the file rainbond-python-0.2.9.tar.gz.

File metadata

  • Download URL: rainbond-python-0.2.9.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.25.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for rainbond-python-0.2.9.tar.gz
Algorithm Hash digest
SHA256 52bd5bf81bc9d2030e6d6c8ea5f8526047559ca10a98d2c03a5526ae8bc00479
MD5 c8cbfd3e3958c5be0be80fa4ef796f17
BLAKE2b-256 c9b191847446dbf74651a2c2d8def5c0ab26ae9e5505383bec59a8743d00ca5e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page