-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonReader.go
43 lines (40 loc) · 1.14 KB
/
jsonReader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"encoding/json"
"log"
"net/http"
"fmt"
)
// Fetches and decodes JSON data from the URL.
func fetchData[T any](url string) (T,error) {
resp, err := http.Get(url)
if err != nil {
log.Print(err)
}
defer resp.Body.Close()
//Solution :the response body is unexpectedly empty or prematurely closed
if resp.StatusCode != http.StatusOK {
return *new(T), fmt.Errorf("error: received status code %d", resp.StatusCode)
}
var result T
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
log.Print(err)
}
return result,nil
}
// Fetches complete artist data including locations, dates, and relations.
func fetchCompleteArtistData(id string) (*Artist,bool) {
artist,err := fetchData[Artist](ArtistsURL + "/" + id)
// Check for error or if artist is empty
if err != nil || artist.Name == "" {
return new(Artist),true
}
locations,_ := fetchData[Location](LocationsURL + "/" + id)
concertDates,_ := fetchData[Date](DatesURL + "/" + id)
relation,_ := fetchData[Relation](RelationURL + "/" + id)
artist.Locations = locations
artist.ConcertDates = concertDates
artist.Relations = relation
return &artist,false
}