簡體   English   中英

Python中的REST調用

[英]REST Calls in Python

我正在嘗試編寫腳本以將現有數據庫移植到Firebase中。 我的數據存儲在JSON中,我以為我可以提取JSON,然后將其作為數據發送到POST到Firebase。

def Post_And_Recieve_JSON(url, data, headers):
    print("Compiling query...")
    Post_And_Recieve_JSON.url = url
    Post_And_Recieve_JSON.headers = headers
    Post_And_Recieve_JSON.data = (data)

    print("Sending request...")
    request = urllib.request.Request(url=url, data=data,headers=headers)
    print("Recieving response...")
    response = urllib.request.urlopen(request)

    print("Reading response...")
    response_data = response.read()
    print("Converting into usable format...")
    response_data_JSON = json.loads(response_data.decode(encoding='UTF-8'))

    return response_data_JSON

for all_users in existing_database:
    full_data.append(Post_And_Recieve_JSON(...)

for item in full_data:
    url = 'firebaseurlhere ' + item['profileId'] + '.json'
    data = json.dumps(item).encode('ascii')
    Post_And_Recieve_JSON(url, data, headers)

其中full_data是我已從現有數據庫中正確提取的JSON對象的列表。

我收到“ http.client.BadStatusLine:”

我已經使用以下找到的firebase python庫解決了此問題: http : //ozgur.github.io/python-firebase/

我用pip3來安裝它。 我只是希望我可以像執行其他REST調用一樣進行操作,而不需要新的lib。

full_data = []
from firebase import *
firebase = firebase.FirebaseApplication('https://secret_url.firebaseio.com/', None)

for id in id_list:
    print(str(id))
    url = 'from_url'
    try:
        result = firebase.put('/users/ ' + str(id) + '/',    data=Post_And_Recieve_JSON(url, None, headers)["payload"], name='Data')
    except:
        print('Skipping a user')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM