Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert clean data into Firebase via REST API and Python

I have a simple Firebase that I mostly interact with via Javascript, which works really well. However, I also have a Python program that needs to get data from existing children and put/update data on existing children. I tried python-firebasin, which would do what I want, but it is unreliable (hangs, fails, etc.).

So I'm looking at the python-firebase REST wrapper. This seems efficient, and works well. However, every time I try to post() data, I get not just the data I'm posting, but some kind of unique string paired with it, all inserted as a child.

For example, via Javascript, I might say:

db = new Firebase('https://myfirebase.firebaseio.com/testval/');
db.transaction(function(current) { return 1; });

This would then give me a Firebase that looked like:

|---testval: 1

But when I try to do something similar with the Python Firebase REST wrapper, such as:

db = firebase.FirebaseApplication('https://myfirebase.firebaseio.com/')
db.post('/testval/',1)

My Firebase looks something like this:

|---testval:
   |---JI4BiBbICSEAnM9mDXf: 1

In other words, it inserts a new child, gives it a new string, and then appends the data. Is there any way to insert/modify data on my Firebase using the REST wrapper that would do it cleanly like I'm doing with Javascript? Without adding children, without adding these unique strings?

like image 282
mix Avatar asked Mar 15 '14 10:03

mix


People also ask

Can I use REST API with Firebase?

We can use any Firebase Realtime Database URL as a REST endpoint. All we need to do is append . json to the end of the URL and send a request from our favorite HTTPS client.

Can you connect Python to Firebase?

To connect to Firestore, Firebase first performs authentication. To get the credentials for authentication, click on project settings, and click on “service accounts“. In the “Service accounts” tab, you can find a code snippet for connecting to Google Firebase. Select python as the language and copy the code snippet.

How do I get data from Firebase REST API?

Firebase REST APIs allow you to make requests to the Firebase Database for reading, writing, updating, or removing data. You can define any Firebase Realtime Database URL as a REST endpoint by adding . json in the end. Keeping your data safe, you can use the Firebase REST API to send requests via an HTTPS client.


2 Answers

Try this instead:

db.put(1)

db.post() is the equivalent of .push() in the JavaScript API, so it creates a unique ID for you. db.put() is equivalent to .set() and will just set the data, which appears to be what you want.

Note that there is no equivalent for transactions in the REST API, but your example was just using a transaction to do a .set() so hopefully you don't actually need them.

like image 196
Michael Lehenbauer Avatar answered Oct 20 '22 16:10

Michael Lehenbauer


Try this:

db = firebase.FirebaseApplication('https://myfirebase.firebaseio.com/')
db.put('', 'testval', 1)

put takes three arguments : first is url or path, second is the key name or the snapshot name and third is the data(json)

like image 26
Arbaz Siddiqui Avatar answered Oct 20 '22 17:10

Arbaz Siddiqui