-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetRailwayInfo.go
39 lines (34 loc) · 925 Bytes
/
getRailwayInfo.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
package main
import (
"encoding/json"
"fmt"
"io"
"michikusa_back/types"
"net/http"
"net/url"
)
func GetRailwayInfo(odptRailway string, odptAPIKey string) (types.OdptRailway, error) {
u, _ := url.Parse("https://api.odpt.org/api/v4/odpt:Railway")
q := u.Query()
q.Set("owl:sameAs", odptRailway)
q.Set("acl:consumerKey", odptAPIKey)
u.RawQuery = q.Encode()
req, _ := http.NewRequest("GET", u.String(), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return types.OdptRailway{}, err
}
if resp.StatusCode != http.StatusOK {
return types.OdptRailway{}, fmt.Errorf("api response is not ok(%d)", resp.StatusCode)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return types.OdptRailway{}, err
}
var railwayInfo []types.OdptRailway
if err := json.Unmarshal(body, &railwayInfo); err != nil {
return types.OdptRailway{}, err
}
return railwayInfo[0], nil
}