Fetch .json from URL
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Stefan Hoffmann 2021-02-06 00:57:51 +01:00
parent c8569db91a
commit 0e84f0460a
3 changed files with 32 additions and 10 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api module git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api
go 1.16 go 1.16
require git.nils.zone/nils/prettify v0.0.4

16
go.sum
View File

@ -0,0 +1,16 @@
git.nils.zone/nils/prettify v0.0.4 h1:y014Ejp0yhFAc9vu5U0nJRFxtXN6k6eWW6LiUkwn4NQ=
git.nils.zone/nils/prettify v0.0.4/go.mod h1:q4ydEhSvXuywHe5U6uSEoEeS9f2Upz/n8z0MJylLz5c=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

24
main.go
View File

@ -10,10 +10,11 @@ import (
"log" "log"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
_ "git.nils.zone/nils/prettify"
) )
// types // types
@ -45,6 +46,7 @@ func main() {
if *ucUser == "" { if *ucUser == "" {
log.Fatalln("Please specify an API User via the flag '-ucUser'") log.Fatalln("Please specify an API User via the flag '-ucUser'")
} }
//getFile()
// start API processing (runs in a loop) // start API processing (runs in a loop)
go processAPIs() go processAPIs()
@ -280,22 +282,24 @@ func processAPIs() {
log.Println("...done") log.Println("...done")
} }
} }
func getFile(url string) []byte {
resp, err := http.Get(url)
if err != nil {
fmt.Printf("error")
}
data := resp.Body
byteValue, _ := ioutil.ReadAll(data)
return byteValue
}
func getDevices(file string) (devices, error) { func getDevices(file string) (devices, error) {
// get devices from JSON file // get devices from JSON file
jsonFile, err := os.Open(file) jsonFile := getFile("https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/" + file)
if err != nil {
return devices{}, errors.New("can't open devices.json")
}
defer jsonFile.Close()
// read file to bytes // read file to bytes
byteValue, _ := ioutil.ReadAll(jsonFile)
// variable for d // variable for d
var d devices var d devices
// unmarshal to struct // unmarshal to struct
json.Unmarshal(byteValue, &d) json.Unmarshal(jsonFile, &d)
return d, nil return d, nil
} }