/
client.go
57 lines (47 loc) · 1.42 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
package extract
import (
"context"
"encoding/json"
"net/http"
"github.com/smartystreets/smartystreets-go-sdk"
)
// Client is responsible for sending requests to the us-extract-api.
type Client struct {
sender sdk.RequestSender
}
// NewClient creates a client with the provided sender.
func NewClient(sender sdk.RequestSender) *Client {
return &Client{sender: sender}
}
// SendBatch sends the batch of inputs, populating the output for each input if the batch was successful.
func (c *Client) SendLookup(lookup *Lookup) error {
return c.SendLookupWithContext(context.Background(), lookup)
}
func (c *Client) SendLookupWithContext(ctx context.Context, lookup *Lookup) error {
if lookup == nil || len(lookup.Text) == 0 {
return nil
}
request := buildRequest(lookup)
request = request.WithContext(ctx)
response, err := c.sender.Send(request)
if err != nil {
return err
} else {
return deserializeResponse(response, lookup)
}
}
func deserializeResponse(response []byte, lookup *Lookup) error {
var extraction Result
err := json.Unmarshal(response, &extraction)
if err != nil {
return err
}
lookup.Result = &extraction
return nil
}
func buildRequest(lookup *Lookup) *http.Request {
request, _ := http.NewRequest("POST", extractURL, nil) // We control the method and the URL. This is safe.
lookup.populate(request)
return request
}
const extractURL = "/" // Remaining parts will be completed later by the sdk.BaseURLClient.