/
main.go
61 lines (51 loc) · 1.21 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// An example on how to use the trafikinfo library to request weather data.
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"os"
"code.dny.dev/trafikinfo"
wmp "code.dny.dev/trafikinfo/trv/weathermeasurepoint/v2"
)
func main() {
req, err := trafikinfo.NewRequest().
APIKey("YOUR_API_KEY").
Query(
trafikinfo.NewQuery(wmp.ObjectType()).Filter(
trafikinfo.Equal("Name", "YOUR_STATION_NAME"),
),
).Build()
if err != nil {
panic(err)
}
httpReq, err := http.NewRequest(http.MethodPost, trafikinfo.Endpoint, bytes.NewBuffer(req))
if err != nil {
panic(err)
}
httpReq.Header.Set("content-type", "text/xml")
resp, err := http.DefaultClient.Do(httpReq)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Could not read response data")
os.Exit(1)
}
var res wmp.Response
if err := xml.Unmarshal(data, &res); err != nil {
fmt.Println("Could not decode API response")
fmt.Println(err)
os.Exit(1)
}
if resp.StatusCode != http.StatusOK || res.HasErrors() {
fmt.Println("Status code", resp.StatusCode, "with errors", res.ErrorMsg())
os.Exit(1)
}
fmt.Println(string(data))
fmt.Printf("%+v\n", res)
}