Firebase.jl : Julia Firebase

Julia's Interface to Firebase REST API

Installation

using Pkg
Pkg.clone("https://github.com/ashwani-rathee/Firebase.jl.git")

TODO

Lot of work in these section:

  • [ ] Firebase Authentication
  • [ ] Cloud Firestore
  • [ ] Realtime Database

Cloud Firestore

using Firestore

Firestore.init(path_to_firestore_service_account_key_json)

Firestore.get(collection_id,doc_id) # get single doc
Firestore.get(collection_id) # get all docs in collection w/o sub_collections)
Firestore.get(collection_id,doc_id,sub_collection_id_1,doc_id_2) # get doc in sub collection
Firestore.get(collection_id,doc_id,sub_collection_id_1,doc_id_2,sub_collection_id_2) # get all docs in sub collection

Realtime Database

Initializing the realdb for the base url, reduces the effort later

realdbinit("https://fir-jl-457eb-default-rtdb.asia-southeast1.firebasedatabase.app")
Base Url set:"https://fir-jl-457eb-default-rtdb.asia-southeast1.firebasedatabase.app"

GET - Reading Data

Data from your Realtime Database can be read by issuing an HTTP GET request to an endpoint. The following example demonstrates how you might retrieve a user's name that you had previously stored in Realtime Database.

julia> realdb_get("/firebase_test")
ulia> realdb_get("/firebase_test")
FINAL URL: "https://fir-jl-457eb-default-rtdb.asia-southeast1.firebasedatabase.app/firebase_test.json"
GET successful
Dict{String, Any} with 3 entries:
  "firebase_get"  => Dict{String, Any}("firebase_get_3"=>3, "firebase_get_2"=>2, "firebase_get_1"=>1)
  "firebase_new"  => Dict{String, Any}("-McEjFxupzSbZwojItzL"=>Dict{String, Any}("name"=>"real_db_test"), "-McEjCHubimhfipKm2yo"=>Dict{String, Any}("name"=>"real_db_test"), "-McEjNy8-8VjqSH3P8dO"=>Dict{String, Any}…
  "firebase_post" => Dict{String, Any}("firebase_post_1"=>1, "firebase_post_3"=>3, "firebase_post_2"=>2, "input"=>Dict{String, Any}("-McEQ7vGcvji7Myab182"=>Dict{String, Any}("stockAmount"=>350, "price"=>"7.5 TL", "…

POST - Pushing Data

To accomplish the equivalent of the JavaScript push() method (see Lists of Data), you can issue a POST request. A successful request is indicated by a 200 OK HTTP status code. The response contains the child name of the new data specified in the POST request.

julia> realdb_post("/firebase_test/firebase_new")
FINAL URL: "https://fir-jl-457eb-default-rtdb.asia-southeast1.firebasedatabase.app/firebase_test/firebase_new.json"
POST successful
Dict{String, Any} with 1 entry:
  "name" => "-McEkk8qvdT4UHSUQALx"

julia> realdb_post("/firebase_test/firebase_new/","""{"hello":1}""")
FINAL URL: "https://fir-jl-457eb-default-rtdb.asia-southeast1.firebasedatabase.app/firebase_test/firebase_new.json"
POST successful
Dict{String, Any} with 1 entry:
  "name" => "-McElRx3ZBP-l7pCF0LN"

DELETE - Removing Data

You can delete data with a DELETE request A successful DELETE request is indicated by a 200 OK HTTP status code with a response containing JSON null.

julia> realdb_delete("/firebase_test/firebase_new")
https://fir-jl-457eb-default-rtdb.asia-southeast1.firebasedatabase.app/firebase_test/firebase_new.json
DELETE successful

PUT - Writing Data

You can write data with a PUT request. A successful request is indicated by a 200 OK HTTP status code. The response contains the data specified in the PUT request.

julia> realdb_put("/firebase_test/firebase_new/","""{"hello":1}""")
FINAL URL: "https://fir-jl-457eb-default-rtdb.asia-southeast1.firebasedatabase.app/firebase_test/firebase_new.json"
PUT successful
Dict{String, Any} with 1 entry:
  "name" => "-McElRx3ZBP-l7pCF0LN"
Firebase.firebase_changeemailFunction

firebasechangeemail(newemail="",firebaseidtoken= FIREBASEID_TOKEN)

Change email You can change a user's email by issuing an HTTP POST request to the Auth setAccountInfo endpoint.

Example

julia data = firebase_signin("ab669522@gmail.com,"helloworld!!") firebase_changeemail("ab6695221@gmail.com",data["idToken"]) data = firebase_signin("ab6695221@gmail.com,"helloworld!!") firebase_changeemail("ab669522@gmail.com",data["idToken"])`

Firebase.firebase_changepasswordFunction

firebasechangepassword(newpassword="", firebaseidtoken=FIREBASEID_TOKEN)

Change password You can change a user's password by issuing an HTTP POST request to the Auth setAccountInfo endpoint.

Example

julia data = firebase_signin("ab669522@gmail.com","helloworld!!") firebase_changepassword("hellopaaji!!",data["idToken"]) data = firebase_signin("ab6695221@gmail.com","hellopaaji!!") firebase_changepassword("helloworld!!",data["idToken"])`

Firebase.firebase_deleteuserFunction

firebasedeleteuser(firebaseidtoken=FIREBASEID_TOKEN)

Delete account You can delete a current user by issuing an HTTP POST request to the Auth deleteAccount endpoint.

Example

data = firebase_signup("ashwani121231@gmail.com","hello!!123")
firebase_deleteuser(data["idToken"])
Firebase.firebase_fetchprovidersFunction

firebase_fetchproviders(email="")

Fetch providers for email You can look all providers associated with a specified email by issuing an HTTP POST request to the Auth createAuthUri endpoint.

Example

julia> firebase_fetchproviders("ashwani1@gmail.com")
#= 
Dict{String, Any} with 5 entries:
  "allProviders"  => Any["password"]
  "kind"          => "identitytoolkit#CreateAuthUriResponse"
  "sessionId"     => "k8iBdCSLEeAbhhgKz6E4PC-ltCs"
  "signinMethods" => Any["password"]
  "registered"    => true
=#
Firebase.firebase_getuserinfoFunction

firebasegetuserinfo(firebaseidtoken=FIREBASEID_TOKEN)

Get user data You can get a user's data by issuing an HTTP POST request to the Auth getAccountInfo endpoint.

Example

data = firebase_signin("ab669522@gmail.com","helloworld!!")
firebase_getuserinfo(data["idToken"])
Firebase.firebase_linkoauthMethod

Link with OAuth credential You can link an OAuth credential to a user by issuing an HTTP POST request to the Auth verifyAssertion endpoint.

Firebase.firebase_linkuserinfoFunction

Link with email/password You can link an email/password to a current user by issuing an HTTP POST request to the Auth setAccountInfo endpoint.

Example

data = firebase_signin("ab669522@gmail.com","helloworld!!")
firebase_linkuserinfo("ash@gmail.com", "hello!!123as",data["idToken"])
data = firebase_signin("ash@gmail.com", "hello!!123as")
firebase_linkuserinfo("ab669522@gmail.com","helloworld!!",data["idToken"])
Firebase.firebase_passwordresetFunction

firebase_passwordreset(email="ab669522@gmail.com")

Send password reset email You can send a password reset email by issuing an HTTP POST request to the Auth getOobConfirmationCode endpoint.

Example

firebase_passwordreset(email="ab669522@gmail.com")
# email will recieve the reset password email
Firebase.firebase_signinFunction

firebase_signin(email::String="data", password::String="data", returnSecureToken::Bool= true)

Sign in with email / password You can sign in a user with an email and password by issuing an HTTP POST request to the Auth verifyPassword endpoint.

Example

firebase_signin("ashwani1@gmail.com" , "hello1231!!")
#=
Dict{String, Any} with 8 entries:
  "refreshToken" => "AGEhc0C1F9smilhBPdT5TYClumCwdzGI9q43s5r-MsqW-bJTeiIw1doG7zRF_VMX2a0WELR3zRB…
  "kind"         => "identitytoolkit#VerifyPasswordResponse"
  "localId"      => "zgrI4g3ualVJW7IEiRlPFg5jItA2"
  "displayName"  => ""
  "idToken"      => "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRjNGQwMGJjM2NiZWE4YjU0NTMzMWQxZjFjOTZmZDRlNjd…
  "registered"   => true
  "expiresIn"    => "3600"
  "email"        => "ashwani1@gmail.com"
=#
Firebase.firebase_signinanonFunction

firebase_signinanon(returnSecureToken::Bool= true)

Sign in anonymously You can sign in a user anonymously by issuing an HTTP POST request to the Auth signupNewUser endpoint.

Exampleemail::String="", password::String="",

julia> firebase_signinanon()
#=
Dict{String, Any} with 5 entries:
  "refreshToken" => "AGEhc0DiE1zelLdCW2KG9wMRwZKcZI6EuSef4hsIc6uOZp0I3SYUTf0Hib_XgvtgY1yeqgFNqJ0…
  "kind"         => "identitytoolkit#SignupNewUserResponse"
  "localId"      => "3T8r7DD99zO9qKL1k56boC2iiXF2"
  "expiresIn"    => "3600"
  "idToken"      => "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRjNGQwMGJjM2NiZWE4YjU0NTMzMWQxZjFjOTZmZDRlNjd…
=#
Firebase.firebase_signinoauthMethod

firebase_signinoauth()

Sign in with OAuth credential You can sign in a user with an OAuth credential by issuing an HTTP POST request to the Auth verifyAssertion endpoint.

This doesn't work today

Example

firebase_signinoauth()
Firebase.firebase_signupFunction

firebase_signup(email::String="", password::String="", returnSecureToken::Bool= true)

Sign up with email / password You can create a new email and password user by issuing an HTTP POST request to the Auth signupNewUser endpoint.

Example

set_webapikey("WEB_API_KEY")
firebase_signup("ashwani@gmail.com","hello!!")
#=
Dict{String, Any} with 6 entries:
  "refreshToken" => "AGEhc0AjgXJevwy_rkJ3r4VUHc4EEORngVF-rwEtOuO007AymRqbAzVnn1iAMCZNHqzwnCI1X8fkJ0OPE2fTuGN7ameU6P7SmibwIFs8sOZxUI8C_uklOkk1IG5DFdWXAC45mK_0Hy4tuJXJUvI8Kuy6LS-GERZ5Bc…
  "kind"         => "identitytoolkit#SignupNewUserResponse"
  "localId"      => "GRCBj28Pkhat0mYsJS0BVrlVmJd2"
  "expiresIn"    => "3600"
  "idToken"      => "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRjNGQwMGJjM2NiZWE4YjU0NTMzMWQxZjFjOTZmZDRlNjdjNTFlODkiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vZmlyLWp…
  "email"        => "ashwani@gmail.com"
=#
Firebase.firebase_unlinkprovideFunction

Unlink provider You can unlink a provider from a current user by issuing an HTTP POST request to the Auth setAccountInfo endpoint.

Example

set_webapikey()

data = firebase_signup("ashwani12123@gmail.com","hello!!123")
#=
Dict{String, Any} with 6 entries:
  "refreshToken" => "AGEhc0A4Y-wFC0S2Cub-7TY2XNfZMYQf-YKzTdQWl9_SjrRpUW4d3QymQy5P1EN4Me8-SE695sPE_MBXoMoqTzKLy76nBvDGmybGmP4wnWLyrJql-HfoVPRtG1emeJ3E61DnDbUIZOvV-Y3GuzpJ57XZojHBM1aoMp…
  "kind"         => "identitytoolkit#SignupNewUserResponse"
  "localId"      => "neXn8Mg562VFgENRtA2SEC0NEBk1"
  "expiresIn"    => "3600"
  "idToken"      => "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRjNGQwMGJjM2NiZWE4YjU0NTMzMWQxZjFjOTZmZDRlNjdjNTFlODkiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vZmlyLWp…
  "email"        => "ashwani12123@gmail.com"
=#
firebase_unlinkprovide(data["idToken"], "google.com")
#=
Dict{String, Any} with 6 entries:
  "kind"             => "identitytoolkit#SetAccountInfoResponse"
  "localId"          => "neXn8Mg562VFgENRtA2SEC0NEBk1"
  "passwordHash"     => "UkVEQUNURUQ="
  "emailVerified"    => false
  "providerUserInfo" => Any[Dict{String, Any}("rawId"=>"ashwani12123@gmail.com", "providerId"=>"password", "federatedId"=>"ashwani12123@gmail.com", "email"=>"ashwani12123@gmail.com")]
  "email"            => "ashwani12123@gmail.com"
=#
Firebase.firebase_updateprofileFunction

Update profile You can update a user's profile (display name / photo URL) by issuing an HTTP POST request to the Auth setAccountInfo endpoint.

TODO: delete attribute

Example

julia data = firebase_signin("ab669522@gmail.com","helloworld!!") firebase_updateprofile("ash","https://i.imgur.com/zgDkgSR.jpeg",data["idToken"]) #= Dict{String, Any} with 8 entries: "kind" => "identitytoolkit#SetAccountInfoResponse" "photoUrl" => "https://i.imgur.com/zgDkgSR.jpeg" "localId" => "GovyExPqFWS70uynnuBzVnYgLAi2" "displayName" => "ash" "passwordHash" => "UkVEQUNURUQ=" "emailVerified" => false "providerUserInfo" => Any[Dict{String, Any}("rawId"=>"ab669522@gmail.com", "photoUrl"=>"https:… "email" => "ab669522@gmail.com" =#

Firebase.firebase_verifyemailFunction

firebaseverifyemail(firebaseidtoken=FIREBASEID_TOKEN)

Send email verification You can send an email verification for the current user by issuing an HTTP POST request to the Auth getOobConfirmationCode endpoint.

Example

data = firebase_signin("ab669522@gmail.com","helloworld!!")
firebase_verifyemail((data["idToken"])
Firebase.firestore_batchgetFunction

firestore_batchget(batch=body)

Gets multiple documents Documents returned by this method are not guaranteed to be returned in the same order that they were requested.

Example

init("[FIREBASE_ADMIN_SDK].json")
res = firestore_batchget()
Firebase.firestore_batchwriteFunction

firestore_batchwrite(batch=body)

Applies a batch of write operations. The documents.batchWrite method does not apply the write operations atomically and can apply them out of order. Method does not allow more than one write per document. Each write succeeds or fails independently. See the BatchWriteResponse for the success status of each write.

If you require an atomically applied set of writes, use documents.commit instead.

Example

init("[FIREBASE_ADMIN_SDK].json")
res = firestore_batchwrite()
Firebase.firestore_beginTransactionMethod

firestore_beginTransaction()

Starts a new transaction. "options" The options for the transaction. Defaults to a read-write transaction.

Example

init("[FIREBASE_ADMIN_SDK].json")
firestore_beginTransaction()
Firebase.firestore_commitFunction

firestore_commit(transactionid="",body =body)

Commits a transaction, while optionally updating documents.

Example

init("[FIREBASE_ADMIN_SDK].json")
data = firestore_beginTransaction()
firestore_commit(data["transaction"])
Firebase.firestore_createdocFunction

firestore_createdoc(url, body)

Example

init("[FIREBASE_ADMIN_SDK].json")
firestore_createdoc("/firebase_test/firebase_post/two") 
firestore_createdoc("/firebase_test/firebase_post/three";query ="hello") # with document id name in query
Firebase.firestore_deletedocMethod

firestore_deletedoc(url)

Deletes a document

Example

init("[FIREBASE_ADMIN_SDK].json")
firestore_createdoc("/firebase_test/firebase_post/three";query ="hello") # creates the document and then we delete it
firestore_deletedoc("/firebase_test/firebase_post/three/hello")
Firebase.firestore_getcollectionidsMethod

firestore_getcollectionids(url;query="")

Lists all the collection IDs underneath a document.

Example

init("[FIREBASE_ADMIN_SDK].json")
firestore_getcollectionids("/firebase_test/firebase_post")
Firebase.firestore_getdoclistMethod

firestore_getdoclist(url)

Lists documents

Example

init("[FIREBASE_ADMIN_SDK].json")
firestore_getdoclist("/firebase_test")
Firebase.firestore_partitionqueryFunction

firestore_partitionquery(url)

Partitions a query by returning partition cursors that can be used to run the query in parallel. The returned partition cursors are split points that can be used by documents.runQuery as starting/end points for the query results.

Firebase.firestore_patchFunction

firestore_patch()

Updates or inserts a document.

Example

init("[FIREBASE_ADMIN_SDK].json")
firestore_patch("/firebase_test/firebase_patch")
Firebase.firestore_runqueryFunction

firestore_runquery()

Runs a query.

Example

init("[FIREBASE_ADMIN_SDK].json")
firestore_runquery("/firebase_test/firebase_post")
Firebase.get_requestMethod

get_request(url::String)

Get request which is general in nature and gets any document or collection

Example

init("[FIREBASE_ADMIN_SDK].json")
res = get_request("/firebase_test/firebase_get") # document get
res = get_request("/firebase_test/firebase_get/firebase_get_collection") # collection fetch
Firebase.realdb_deleteFunction

realdbdelete(url, body = Dict("name"=> "realdb_test"); query = Dict())

DELETE request to an endpoint

Example

realdb_init("https://[PROJECT_ID].asia-southeast1.firebasedatabase.app")
realdb_delete("/users/jack/name/last")
Firebase.realdb_getMethod

realdb_get(url; query = Dict())

GET request to an endpoint.

Example

realdb_init("https://[PROJECT_ID].asia-southeast1.firebasedatabase.app")
realdb_get("/users/jack/name")
Firebase.realdb_initMethod

realdbinit(baseurl; query = Dict())

Initialize the realtimedb with baseurl to make things easier

Example

realdb_init("https://[PROJECT_ID].asia-southeast1.firebasedatabase.app")
Firebase.realdb_patchFunction

realdbpatch(url, body = Dict("name"=> "realdb_test"); query = Dict())

PATCH request to an endpoint.

Example

realdb_init("https://[PROJECT_ID].asia-southeast1.firebasedatabase.app")
body =Dict("last"=>"Jones")
realdb_patch("/users/jack/name/",body)
Firebase.realdb_postFunction

realdbpost(url, body = "{"name": "realdb_test"}"; query = Dict())

POST request to an endpoint.

Example

realdb_init("https://[PROJECT_ID].asia-southeast1.firebasedatabase.app")
# realdb_post("/message_list")
body =Dict("user_id" => "jack", "text" => "Ahoy!")
realdb_post("/message_list",body)

Notes:

According to the REST API documentation for Realtime Database, a POST is the equivalent of a "push" operation when using the client SDKs. This push operation always involves adding data under a random ID. There is no avoiding that for a push.

If you know the name of the node where you want to add data, you should use a PUT instead. This is the equivalent of using "set" operation with the client SDKs.

Firebase.realdb_putFunction

realdbput(url, body = Dict("name"=> "realdb_test"); query = Dict())

PUT request to a endpoint.

Example

realdb_init("https://[PROJECT_ID].asia-southeast1.firebasedatabase.app")
body = Dict("first"=>"Ash", "last"=>"Sparrow")
realdb_put("/users/jack/name",body)
Firebase.set_webapikeyFunction

set_webapikey(key ="")

sets WEBAPIKEY

Example

set_webapikey("asjdlajsj1j32lj312")
Firebase.verify_passwordresetcodeMethod

verify_passwordresetcode()

Verify password reset code You can verify a password reset code by issuing an HTTP POST request to the Auth resetPassword endpoint.

Example

Reference