/
http.go
93 lines (85 loc) · 2.36 KB
/
http.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
// Copyright (c) 2020 Tanner Ryan. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
package davisweather
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/tannerryan/davisweather/parser"
)
var (
// httpClient is an HTTP client with keep alives disabled
httpClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
)
// fetchConditionsHTTP fetches weather conditions over HTTP. It returns an error
// if the HTTP response is not formatted correctly, or if the request fails.
func (c *Client) fetchConditionsHTTP(ctx context.Context) (*parser.ConditionsHTTP, error) {
url := fmt.Sprintf("%s%s", c.unit.GetURL(), routeConditions)
// prepare request context
ctx, cancel := context.WithTimeout(ctx, httpTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
// perform request
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// parse body
conditions, err := parser.ParseHTTP(body)
if err != nil {
return nil, err
}
if conditions.Error != nil {
return nil, errors.New(conditions.Error.Message)
}
return conditions, nil
}
// fetchBroadcastResponse fetches the enable UDP broadcast broadcast response
// over HTTP. It returns an error if the HTTP response is not formatted
// correctly, or if the request fails.
func (c *Client) fetchBroadcastResponse(ctx context.Context) (*parser.BroadcastResponse, error) {
url := fmt.Sprintf("%s%s?duration=%.0f",
c.unit.GetURL(), routeBroadcastResponse, udpDuration.Seconds())
// prepare request context
ctx, cancel := context.WithTimeout(ctx, httpTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
// perform request
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// parse body
broadcastResp, err := parser.ParseBroadcastResponse(body)
if err != nil {
return nil, err
}
if broadcastResp.Error != nil {
return nil, errors.New(broadcastResp.Error.Message)
}
return broadcastResp, nil
}