-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreq.go
37 lines (34 loc) · 890 Bytes
/
req.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
package osm
import (
"context"
"encoding/json"
"errors"
"net/http"
)
const apiUrl = "https://nominatim.openstreetmap.org/"
func runRequest[T any](ctx context.Context, url string, opts ...optFunc) (*T, error) {
o := mergeOpts(opts)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept-Language", o.Locale)
req.Header.Set("User-Agent", o.UserAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var handled ErrorResult
if err := json.NewDecoder(resp.Body).Decode(&handled); err == nil {
return nil, &handled
}
return nil, errors.New("unexpected status code: " + resp.Status)
}
var result T
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}