-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
185 lines (155 loc) · 5.1 KB
/
api.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package meteolt
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
const baseAPIURL = "https://api.meteo.lt/v1"
// GetWeatherObservation returns weather observation data for a specific place.
//
// Internally calls '/places/{place}/forecasts/{forecast-type}' endpoint.
func GetWeatherForecast(ctx context.Context, place string, forecastType ForecastType) (*Forecast, error) {
url := fmt.Sprintf("%s/places/%s/forecasts/%s", baseAPIURL, place, forecastType)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var forecast Forecast
if err := json.NewDecoder(resp.Body).Decode(&forecast); err != nil {
return nil, err
}
return &forecast, nil
}
// GetAllPlaces returns a list of places for which weather forecast is provided.
//
// Internally calls '/places' endpoint.
func GetAllPlaces(ctx context.Context) ([]Place, error) {
url := fmt.Sprintf("%s/places", baseAPIURL)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var places []Place
if err := json.NewDecoder(resp.Body).Decode(&places); err != nil {
return nil, err
}
return places, nil
}
// GetAllStation returns a list of meteorological stations for which observation data are provided is returned.
//
// Internally calls '/stations' endpoint.
func GetAllStations(ctx context.Context) ([]Station, error) {
url := fmt.Sprintf("%s/stations", baseAPIURL)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var stations []Station
if err := json.NewDecoder(resp.Body).Decode(&stations); err != nil {
return nil, err
}
return stations, nil
}
// GetStationInfo returns information about the meteorological station.
//
// Internally calls '/stations/{station-code}' endpoint.
func GetStationInfo(ctx context.Context, stationCode string) (*StationExtended, error) {
url := fmt.Sprintf("%s/stations/%s", baseAPIURL, stationCode)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var station StationExtended
if err := json.NewDecoder(resp.Body).Decode(&station); err != nil {
return nil, err
}
return &station, nil
}
// GetStationObservationInfo returns information about the meteorological station's observations.
//
// Internally calls '/stations/{station-code}/observations' endpoint.
func GetStationObservationInfo(ctx context.Context, stationCode string) (*StationObservation, error) {
url := fmt.Sprintf("%s/stations/%s/observations", baseAPIURL, stationCode)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var observation StationObservation
if err := json.NewDecoder(resp.Body).Decode(&observation); err != nil {
return nil, err
}
return &observation, nil
}
// GetStationObservationsData returns the observations measured by the station at specified date.
//
// Internally calls '/stations/{station-code}/observations/{date}' endpoint.
func GetStationObservationsDataByDate(ctx context.Context, stationCode string, date time.Time) (*Station, []Observation, error) {
url := fmt.Sprintf("%s/stations/%s/observations/%s", baseAPIURL, stationCode, date.Format("2006-01-02"))
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
tmp := struct {
Station Station `json:"station"`
Observations []Observation `json:"observations"`
}{}
if err := json.NewDecoder(resp.Body).Decode(&tmp); err != nil {
return nil, nil, err
}
return &tmp.Station, tmp.Observations, nil
}
// GetStationObservationsDataLatest returns the observations measured by the station from the last 24 hours.
//
// Internally calls '/stations/{station-code}/observations/latest' endpoint.
func GetStationObservationsDataLatest(ctx context.Context, stationCode string) (*Station, []Observation, error) {
url := fmt.Sprintf("%s/stations/%s/observations/latest", baseAPIURL, stationCode)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
tmp := struct {
Station Station `json:"station"`
Observations []Observation `json:"observations"`
}{}
if err := json.NewDecoder(resp.Body).Decode(&tmp); err != nil {
return nil, nil, err
}
return &tmp.Station, tmp.Observations, nil
}