'How to properly read and parse data from Firebase in Nim?
I have a database in Firebase which currently I can access through CURL like this
curl "https://mydb.firebaseio.com/my_data.json?auth=XKJYED78634jsvdffwu7riugwer"
I want to make a Nim script to actively listen to this data for any changes and then generate web pages accordingly, what would be the best way to do that ? I am not able to find any library for Firebase support in Nim.
Thanks @jason, based on your response, this is what I did:
import std/[httpclient, json]
const url = "https://mydb.firebaseio.com/my_data.json?auth=XKJYED78634jsvdffwu7riugwer"
var client = newHttpClient()
let parsedJsonObj = parseJson(client.getContent(url))
client.close()
echo parsedJsonObj
Then I compiled this with -d:ssl flag and this is successfully reading the JSON data and dumping it to stdout. Another problem I am facing is that this JSON data is unstructured, is there any way to iterate through all records without knowing the key names ?
Solution 1:[1]
I'm no expert in webdev, but given that's a link that returns a json formatted file you can do something like the following:
import std/[httpclient, htmlgen, asyncdispatch, json]
const url = "https://mydb.firebaseio.com/my_data.json?auth=XKJYED78634jsvdffwu7riugwer" # do this safer using dotenvs or similar
proc buildSite(): Future[string] {.async.} =
let
myHttpClient = newAsyncHttpClient()
data = parseJson(await(myHttpClient.getContent(url)))
# Do stuff with `data, we'll just make a header for an example
result = h1("Hellow World")
myHttpClient.close()
proc main() {.async.} =
var isWatchingSite = true
while isWatchingSite:
try:
let site = await buildSite()
except HttpRequestError as err:
echo err.msg
await sleepAsync(10000)
waitFor main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jason |
