/
weather_clients.go
147 lines (119 loc) · 4.15 KB
/
weather_clients.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
package server
import (
"fmt"
"net/http"
"time"
"github.com/calvinmclean/automated-garden/garden-app/pkg/storage"
"github.com/calvinmclean/automated-garden/garden-app/pkg/weather"
"github.com/calvinmclean/babyapi"
"github.com/go-chi/render"
)
const (
weatherClientsBasePath = "/weather_clients"
weatherClientIDLogField = "weather_client_id"
)
// WeatherClientsAPI encapsulates the structs and dependencies necessary for the WeatherClients API
// to function, including storage and configuring
type WeatherClientsAPI struct {
*babyapi.API[*weather.Config]
storageClient *storage.Client
}
// NewWeatherClientsAPI creates a new WeatherClientsResource
func NewWeatherClientsAPI(storageClient *storage.Client) (*WeatherClientsAPI, error) {
api := &WeatherClientsAPI{
storageClient: storageClient,
}
api.API = babyapi.NewAPI[*weather.Config]("WeatherClients", weatherClientsBasePath, func() *weather.Config { return &weather.Config{} })
api.SetStorage(api.storageClient.WeatherClientConfigs)
api.SetOnCreateOrUpdate(func(_ *http.Request, wc *weather.Config) *babyapi.ErrResponse {
// make sure a valid WeatherClient can still be created
_, err := weather.NewClient(wc, func(map[string]interface{}) error { return nil })
if err != nil {
return babyapi.ErrInvalidRequest(fmt.Errorf("invalid request to update WeatherClient: %w", err))
}
return nil
})
api.SetResponseWrapper(func(wc *weather.Config) render.Renderer {
return &WeatherClientResponse{Config: wc}
})
api.AddCustomIDRoute(http.MethodGet, "/test", http.HandlerFunc(api.testWeatherClient))
api.SetBeforeDelete(func(r *http.Request) *babyapi.ErrResponse {
id := api.GetIDParam(r)
waterSchedules, err := storageClient.GetWaterSchedulesUsingWeatherClient(id)
if err != nil {
return babyapi.InternalServerError(fmt.Errorf("unable to get WaterSchedules using WeatherClient %q: %w", id, err))
}
if len(waterSchedules) > 0 {
return babyapi.ErrInvalidRequest(fmt.Errorf("unable to delete WeatherClient used by %d WaterSchedules", len(waterSchedules)))
}
return nil
})
return api, nil
}
func (api *WeatherClientsAPI) testWeatherClient(w http.ResponseWriter, r *http.Request) {
logger := babyapi.GetLoggerFromContext(r.Context())
logger.Info("received request to test WeatherClient")
weatherClient, httpErr := api.GetRequestedResource(r)
if httpErr != nil {
logger.Error("error getting requested resource", "error", httpErr.Error())
render.Render(w, r, httpErr)
return
}
wc, err := weather.NewClient(weatherClient, func(weatherClientOptions map[string]interface{}) error {
weatherClient.Options = weatherClientOptions
return api.storageClient.WeatherClientConfigs.Set(r.Context(), weatherClient)
})
if err != nil {
logger.Error("unable to get WeatherClient", "error", err)
render.Render(w, r, InternalServerError(err))
return
}
rd, err := wc.GetTotalRain(72 * time.Hour)
if err != nil {
logger.Error("unable to get total rain in the last 72 hours", "error", err)
render.Render(w, r, InternalServerError(err))
return
}
td, err := wc.GetAverageHighTemperature(72 * time.Hour)
if err != nil {
logger.Error("unable to get average high temperature in the last 72 hours", "error", err)
render.Render(w, r, InternalServerError(err))
return
}
resp := &WeatherClientTestResponse{WeatherData: WeatherData{
Rain: &RainData{
MM: rd,
},
Temperature: &TemperatureData{
Celsius: td,
},
}}
if err := render.Render(w, r, resp); err != nil {
logger.Error("unable to render WeatherClientResponse", "error", err)
render.Render(w, r, ErrRender(err))
}
}
// WeatherClientTestResponse is used to return WeatherData from testing that the client works
type WeatherClientTestResponse struct {
WeatherData
}
// Render ...
func (resp *WeatherClientTestResponse) Render(_ http.ResponseWriter, _ *http.Request) error {
return nil
}
type WeatherClientResponse struct {
*weather.Config
Links []Link `json:"links,omitempty"`
}
// Render ...
func (resp *WeatherClientResponse) Render(_ http.ResponseWriter, _ *http.Request) error {
if resp != nil {
resp.Links = append(resp.Links,
Link{
"self",
fmt.Sprintf("%s/%s", "/weather_clients", resp.ID),
},
)
}
return nil
}