-
Notifications
You must be signed in to change notification settings - Fork 0
/
Historical.go
86 lines (66 loc) · 2.16 KB
/
Historical.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
package weatherstack
import (
"fmt"
"net/url"
"time"
"cloud.google.com/go/civil"
errortools "github.com/leapforce-libraries/go_errortools"
go_http "github.com/leapforce-libraries/go_http"
utilities "github.com/leapforce-libraries/go_utilities"
)
type HistoricalResponse struct {
Request Request `json:"request"`
Location Location `json:"location"`
Current CurrentWeather `json:"current"`
Historical map[string]Weather `json:"historical"`
}
type GetHistoricalWeatherConfig struct {
Query string
StartDate civil.Date
EndDate *civil.Date
Hourly *Hourly
Interval *Interval
Units *Units
Language *string
}
func (service *Service) GetHistoricalWeather(config GetHistoricalWeatherConfig) (*HistoricalResponse, *errortools.Error) {
values := url.Values{}
startDate := utilities.DateToTime(config.StartDate)
if config.EndDate == nil {
values.Add("historical_date", startDate.Format(dateFormat))
} else {
endDate := utilities.DateToTime(*config.EndDate)
if startDate.After(endDate) {
return nil, errortools.ErrorMessage("StartDate must be smaller or equal to EndDate.")
}
maxEndDate := startDate.Add(time.Duration(MaxDaysPerCall-1) * 24 * time.Hour)
if endDate.After(maxEndDate) {
return nil, errortools.ErrorMessage("Maximum time frame of 60 days exceeded.")
}
values.Add("historical_date_start", startDate.Format(dateFormat))
values.Add("historical_date_end", endDate.Format(dateFormat))
}
values.Add("query", config.Query)
if config.Hourly != nil {
values.Add("hourly", fmt.Sprintf("%v", int64(*config.Hourly)))
}
if config.Interval != nil {
values.Add("interval", fmt.Sprintf("%v", int64(*config.Interval)))
}
if config.Units != nil {
values.Add("units", fmt.Sprintf("%s", string(*config.Units)))
}
if config.Language != nil {
values.Add("language", *config.Language)
}
historicalResponse := HistoricalResponse{}
requestConfig := go_http.RequestConfig{
URL: service.url(fmt.Sprintf("historical?%s", values.Encode())),
ResponseModel: &historicalResponse,
}
_, _, e := service.get(&requestConfig)
if e != nil {
return nil, e
}
return &historicalResponse, nil
}