佛爺帶你利用知曉雲構建在線數據庫增刪改查接口實現(Python)

知曉雲介紹

官方文檔介紹:
知曉雲是個好用、順手的小程序開發工具。它免去了小程序開發中服務器搭建、域名備案、數據接口實現等繁瑣流程。讓您專注於業務邏輯的實現,使用知曉雲開發小程序,門檻更低,效率更高。

其他定義:
「知曉雲」是國內第一個專注於微信小程序開發的 MBaaS(後端即服務)服務平臺。
只要你有好的想法,只需簡單地在小程序中接入知曉雲的 JS SDK,不用去管什麼 PHP、數據庫等後端邏輯,無需管理服務器或編寫後端代碼,不用擔心自己服務器的負載和運維。

ok,知道知曉雲在做什麼了吧,後端即服務。那跟其他大的雲服務商有啥區別呢?騰訊雲、阿里雲是 IaaS、PaaS 服務,提供虛擬機級別的服務。知曉雲是 MBaaS 服務(後端即服務),應用級別的服務平臺,簡單來說,使用騰訊雲時,要寫後端代碼,使用知曉雲,不用。

所以總結一下,如果你去開發一個小程序,知曉雲絕對是無可厚非的助手。對於本人的需求來說,小程序並不是最優先的。而是知曉雲相當於提供了一個雲數據庫,並且可以是免費的,當然有一定限制,但對於我開發桌面應用級的個人興趣產品來說,它無疑是給我提供了在線賬戶管理平臺,通過它我可以簡單的適配到我的數據管理,說白了,就是數據的增刪改查。

如何註冊,如何創建應用,如何使用數據表,其實其他的教程也沒有必要去看了。官方網站和文檔是最好的教程。

官方網站:https://cloud.minapp.com/
官方文檔:(19.5.1)https://doc.minapp.com/

數據接口的增刪改查(Python版,其中js、php版官方文檔中有)

client_id 自己註冊後查
client_secret 自己註冊後查
TABLE_ID 自己註冊後查

# python3.6   2019.5.1
import json
import requests
from urllib.parse import urlencode

CODE_CONFIG = {
    'url': 'https://cloud.minapp.com/api/oauth2/hydrogen/openapi/authorize/',
    'headers': {'Content-Type': 'application/json'}
}
TOKEN_CONFIG = {
    'url': 'https://cloud.minapp.com/api/oauth2/access_token/',
    'headers': {'Content-Type': 'application/json'}
}
CODE_DATA = {
    'client_id': '*******************',
    'client_secret': '*******************'
}
TOKEN_DATA = {
    'client_id': '*******************',
    'client_secret': '*******************',
    'code': None,
    'grant_type': 'authorization_code'
}

TABLE_ID = '*****'


def get_access_token():
    code_response = requests.post(url=CODE_CONFIG['url'], data=json.dumps(CODE_DATA),
                                  headers=CODE_CONFIG['headers'])
    # 獲取token
    TOKEN_DATA['code'] = code_response.json().get('code')
    token_response = requests.post(url=TOKEN_CONFIG['url'], data=json.dumps(TOKEN_DATA),
                                   headers=TOKEN_CONFIG['headers'])
    return token_response.json().get('access_token')


class Model(object):
    def __init__(self):
        # 獲取code
        self.access_token = get_access_token()

    def add(self, add_data):
        """
        增加數據api
        :param add_data: add_data={
                            "phone": '123456789',
                          }
        :return:
        """
        base_api = 'https://cloud.minapp.com/oserve/v1/table/{0}/record/'.format(TABLE_ID)
        headers = {
            'Authorization': 'Bearer %s' % self.access_token,
            'Content-Type': 'application/json',
            'charset': 'utf-8'
        }
        response = requests.post(url=base_api, headers=headers, data=json.dumps(add_data))
        if response.status_code != 201:
            return None
        return 'Success'

    def delete(self, record_id):
        """
        刪除數據api
        :param record_id: 刪除數據的id
        :return:
        """
        base_api = 'https://cloud.minapp.com/oserve/v1/table/{0}/record/{1}/'.format(TABLE_ID, record_id)
        headers = {
            'Authorization': 'Bearer %s' % self.access_token,
            'Content-Type': 'application/json',
            'charset': 'utf-8'
        }
        response = requests.delete(url=base_api, headers=headers)
        if response.status_code != 204:
            return None
        return 'Success'

    def update(self, record_id, update_data):
        """
        更新數據api 
        :param record_id: 待更新數據id
        :param update_data: 更新的數據
        :return:
        """
        base_api = 'https://cloud.minapp.com/oserve/v1/table/{0}/record/{1}/'.format(TABLE_ID, record_id)
        headers = {
            'Authorization': 'Bearer %s' % self.access_token,
            'Content-Type': 'application/json',
            'charset': 'utf-8'
        }
        response = requests.put(url=base_api, headers=headers, data=json.dumps(update_data))

        if response.status_code != 200:
            return None
        return 'Success'

    def query(self, phone):
        """
        查詢數據api
        :param phone:查詢數據字段
        :return:
        """
        base_api = r'https://cloud.minapp.com/oserve/v1/table/{0}/record/'.format(TABLE_ID)
        headers = {
            'Authorization': 'Bearer %s' % self.access_token
        }
        where_ = {
            'account': {'$eq': phone},
        }

        query_ = urlencode({
            'where': json.dumps(where_),
            'order_by': '-created_at',
            'limit': 1,
            'offset': 0,
        })
        account_api = '?'.join((base_api, query_))
        account_response = requests.get(account_api, headers=headers)
        if account_response.status_code != 200:
            return None
        return account_response.json().get('objects')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章