-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
91 lines (66 loc) · 1.67 KB
/
client.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
package pip
import (
"bytes"
"context"
"encoding/json"
"net/http"
)
// SPRResults and SPRResult are simplified versions of
// data-structures defined in:
// https://github.com/whosonfirst/go-whosonfirst-spr/
// https://github.com/whosonfirst/go-whosonfirst-sqlite-spr/
type SPRResults struct {
Places []*SPRResult `json:"places"`
}
type SPRResult struct {
Id string `json:"wof:id"`
ParentId string `json:"wof:parent_id"`
Name string `json:"wof:name"`
Placetype string `json:"wof:placetype"`
}
// PointInPolygonRequest is a simplified version of an
// equivalent data-structure defined in:
// https://github.com/whosonfirst/go-whosonfirst-spatial-pip/blob/main/pip.go
type PointInPolygonRequest struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
IsCurrent []int64 `json:"is_current,omitempty"`
}
type Client struct {
http_client *http.Client
}
func NewClient() (*Client, error) {
http_client := &http.Client{}
cl := &Client{
http_client: http_client,
}
return cl, nil
}
func (cl *Client) Query(ctx context.Context, lat float64, lon float64) (*SPRResults, error) {
req := PointInPolygonRequest{
Latitude: lat,
Longitude: lon,
IsCurrent: []int64{1},
}
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
br := bytes.NewReader(body)
http_req, err := http.NewRequest("POST", "http://localhost:8080/api/point-in-polygon", br)
if err != nil {
return nil, err
}
rsp, err := cl.http_client.Do(http_req)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
var spr *SPRResults
dec := json.NewDecoder(rsp.Body)
err = dec.Decode(&spr)
if err != nil {
return nil, err
}
return spr, nil
}