-
-
Save dolanor/d30e6876d94277b048f1 to your computer and use it in GitHub Desktop.
Query the JCDecaux Vélib' API for a station Id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"encoding/json" | |
"os" | |
) | |
func main() { | |
stationId := os.Args[1] | |
// You need to change that API KEY. Get it here: https://developer.jcdecaux.com/ | |
const yourApiKey = "ae55d65e9bbe2bf740c7fbd3915d583d3aa" | |
res, err := http.Get("https://api.jcdecaux.com/vls/v1/stations/" + stationId + "?contract=Paris&apiKey=" + yourAp | |
iKey) | |
if err != nil { | |
log.Fatal(err) | |
} | |
stationsBytes, err := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// We don't know the JSON "schema", let's get a generic way | |
var jsonstations interface{} | |
jsonerr := json.Unmarshal(stationsBytes, &jsonstations) | |
if jsonerr != nil { | |
log.Fatal(jsonerr) | |
} | |
m := jsonstations.(map[string]interface{}) | |
for k, v := range m { | |
switch vv := v.(type) { | |
case string: | |
fmt.Println(k, "is string:", vv) | |
case int: | |
fmt.Println(k, "is int:", vv) | |
case float64: | |
fmt.Println(k, "is float64:", vv) | |
case []interface{}: | |
fmt.Println(k, "is an array:") | |
for i, u := range vv { | |
fmt.Println(i, u) | |
} | |
case map[string]interface{}: | |
fmt.Println(k, "is a map:") | |
for i, u := range vv { | |
fmt.Println(i, u) | |
} | |
case bool: | |
fmt.Println(k, "is a bool:", vv) | |
default: | |
fmt.Println(k, "is of a type I don't know how to handle") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Un petit coup de "go fmt" ne ferait pas de mal.