Files
go-gui/api-receiver.go
2024-06-03 00:14:40 +02:00

45 lines
904 B
Go

package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
// "fmt"
)
type Record struct {
Time int `json: "time"`
Artist string `json: "artist"`
Title string `json: "title"`
Album string `json: "album"`
Year string `json: "year"`
Cover string `json: "cover"`
}
func readAPI(url string) (msg Record, err error) {
respons, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer respons.Body.Close()
if respons.StatusCode != http.StatusOK {
log.Fatalf("status code error: %d %s", respons.StatusCode, respons.Status)
}
data, err := ioutil.ReadAll(respons.Body)
if err != nil {
log.Fatal(err)
}
var myrecord Record
err = json.Unmarshal([]byte(data), &myrecord)
if err != nil {
log.Fatalf("JSON decode error!", err)
return
}
//fmt.Printf("api-receiver: API title en album is: %s : %s \n", myrecord.Title, myrecord.Album)
return myrecord, err
}