-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
67 lines (59 loc) · 1.63 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
package request
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/interline-io/transitland-lib/tl"
)
type Http struct{}
func (r Http) Download(ctx context.Context, ustr string, secret tl.Secret, auth tl.FeedAuthorization) (io.ReadCloser, int, error) {
u, err := url.Parse(ustr)
if err != nil {
return nil, 0, errors.New("could not parse url")
}
if auth.Type == "query_param" {
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, 0, errors.New("could not parse query string")
}
v.Set(auth.ParamName, secret.Key)
u.RawQuery = v.Encode()
} else if auth.Type == "path_segment" {
u.Path = strings.ReplaceAll(u.Path, "{}", secret.Key)
} else if auth.Type == "replace_url" {
u, err = url.Parse(secret.ReplaceUrl)
if err != nil {
return nil, 0, errors.New("could not parse replacement query string")
}
}
ustr = u.String()
// Prepare HTTP request
req, err := http.NewRequest("GET", ustr, nil)
if err != nil {
return nil, 0, errors.New("invalid request")
}
// Set basic auth, if used
if auth.Type == "basic_auth" {
req.SetBasicAuth(secret.Username, secret.Password)
} else if auth.Type == "header" {
req.Header.Add(auth.ParamName, secret.Key)
}
// Make HTTP request
req = req.WithContext(ctx)
req.Header.Set("User-Agent", fmt.Sprintf("transitland/%s", tl.VERSION))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
// return error directly
return nil, 0, err
}
if resp.StatusCode >= 400 {
resp.Body.Close()
return nil, resp.StatusCode, fmt.Errorf("response status code: %d", resp.StatusCode)
}
return resp.Body, resp.StatusCode, nil
}