Skip to content

Commit 13d9671

Browse files
committed
WithContext option now available for all clients.
1 parent b5cd111 commit 13d9671

File tree

15 files changed

+92
-18
lines changed

15 files changed

+92
-18
lines changed

examples/international-street-api/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"os"
@@ -32,7 +33,7 @@ func main() {
3233
PostalCode: "02516-050",
3334
}
3435

35-
if err := client.SendLookup(lookup); err != nil {
36+
if err := client.SendLookupWithContext(context.Background(), lookup); err != nil {
3637
log.Fatal("Error sending batch:", err)
3738
}
3839

examples/us-autocomplete-api/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"os"
@@ -30,7 +31,7 @@ func main() {
3031
PreferRatio: 0.3333333333,
3132
}
3233

33-
if err := client.SendLookup(lookup); err != nil {
34+
if err := client.SendLookupWithContext(context.Background(), lookup); err != nil {
3435
log.Fatal("Error sending batch:", err)
3536
}
3637

examples/us-extract-api/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"log"
@@ -29,7 +30,7 @@ func main() {
2930
AddressesPerLine: 1,
3031
}
3132

32-
if err := client.SendLookup(lookup); err != nil {
33+
if err := client.SendLookupWithContext(context.Background(), lookup); err != nil {
3334
log.Fatal("Error sending batch:", err)
3435
}
3536

examples/us-street-api/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"os"
@@ -55,7 +56,7 @@ func main() {
5556
batch.Append(lookup2)
5657
batch.Append(lookup3)
5758

58-
if err := client.SendBatch(batch); err != nil {
59+
if err := client.SendBatchWithContext(context.Background(), batch); err != nil {
5960
log.Fatal("Error sending batch:", err)
6061
}
6162

examples/us-zipcode-api/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"os"
@@ -38,7 +39,7 @@ func main() {
3839

3940
fmt.Println("\nBatch full, preparing to send inputs:", batch.Length())
4041

41-
if err := client.SendBatch(batch); err != nil {
42+
if err := client.SendBatchWithContext(context.Background(), batch); err != nil {
4243
log.Fatal("Error sending batch:", err)
4344
}
4445

international-street-api/client.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package street
22

33
import (
4+
"context"
45
"encoding/json"
56
"net/http"
67

@@ -28,6 +29,21 @@ func (c *Client) SendLookup(lookup *Lookup) error {
2829
}
2930
}
3031

32+
func (c *Client) SendLookupWithContext(ctx context.Context, lookup *Lookup) error {
33+
if lookup == nil || (len(lookup.Address1) == 0 && len(lookup.Freeform) == 0) {
34+
return nil
35+
}
36+
37+
request := buildRequest(lookup)
38+
request = request.WithContext(ctx)
39+
response, err := c.sender.Send(request)
40+
if err != nil {
41+
return err
42+
} else {
43+
return deserializeResponse(response, lookup)
44+
}
45+
}
46+
3147
func deserializeResponse(response []byte, lookup *Lookup) error {
3248
var candidates []*Candidate
3349
err := json.Unmarshal(response, &candidates)

international-street-api/client_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package street
22

33
import (
4+
"context"
45
"errors"
56
"github.com/smartystreets/assertions/should"
67
"github.com/smartystreets/gunit"
@@ -35,12 +36,14 @@ func (f *ClientFixture) TestAddressLookupSerializedAndSent__ResponseSuggestionsI
3536
]`
3637
f.input.Freeform = "42"
3738

38-
err := f.client.SendLookup(f.input)
39+
ctx := context.WithValue(context.Background(), "key", "value")
40+
err := f.client.SendLookupWithContext(ctx, f.input)
3941

4042
f.So(err, should.BeNil)
4143
f.So(f.sender.request, should.NotBeNil)
4244
f.So(f.sender.request.Method, should.Equal, "GET")
4345
f.So(f.sender.request.URL.Path, should.Equal, verifyURL)
46+
f.So(f.sender.request.Context(), should.Resemble, ctx)
4447
f.So(string(f.sender.request.URL.Query().Get("freeform")), should.Equal, "42")
4548
f.So(f.sender.request.URL.String(), should.Equal, verifyURL+"?freeform=42")
4649
f.So(f.input.Results, should.Resemble, []*Candidate{
@@ -91,7 +94,7 @@ func (f *ClientFixture) TestDeserializationErrorPreventsDeserialization() {
9194
f.So(f.input.Results, should.BeEmpty)
9295
}
9396

94-
func (f *ClientFixture) FocusTestFullJSONResponseDeserialization() {
97+
func (f *ClientFixture) TestFullJSONResponseDeserialization() {
9598
f.sender.response = `[
9699
{
97100
"input_id": "blah",

us-autocomplete-api/client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package autocomplete
22

33
import (
4+
"context"
45
"encoding/json"
56
"net/http"
67

@@ -19,9 +20,18 @@ func NewClient(sender sdk.RequestSender) *Client {
1920

2021
// SendBatch sends the batch of inputs, populating the output for each input if the batch was successful.
2122
func (c *Client) SendLookup(lookup *Lookup) error {
23+
return c.SendLookupWithContext(context.Background(), lookup)
24+
}
25+
26+
func (c *Client) SendLookupWithContext(ctx context.Context, lookup *Lookup) error {
2227
if lookup == nil || len(lookup.Prefix) == 0 {
2328
return nil
24-
} else if response, err := c.sender.Send(buildRequest(lookup)); err != nil {
29+
}
30+
31+
request := buildRequest(lookup)
32+
request = request.WithContext(ctx)
33+
response, err := c.sender.Send(request)
34+
if err != nil {
2535
return err
2636
} else {
2737
return deserializeResponse(response, lookup)

us-autocomplete-api/client_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package autocomplete
22

33
import (
4+
"context"
45
"errors"
56
"net/http"
67
"testing"
@@ -28,22 +29,24 @@ func (f *ClientFixture) Setup() {
2829
f.input = new(Lookup)
2930
}
3031

31-
func (f *ClientFixture) TestAddressLookupSerializedAndSent__ResponseSuggestionsIncorporatedIntoLookup() {
32+
func (f *ClientFixture) TestAddressLookupSerializedAndSentWithContext__ResponseSuggestionsIncorporatedIntoLookup() {
3233
f.sender.response = `{"suggestions":[
3334
{"text": "1"},
3435
{"text": "2"},
3536
{"text": "3"}
3637
]}`
3738
f.input.Prefix = "42"
3839

39-
err := f.client.SendLookup(f.input)
40+
ctx := context.WithValue(context.Background(), "key", "value")
41+
err := f.client.SendLookupWithContext(ctx, f.input)
4042

4143
f.So(err, should.BeNil)
4244
f.So(f.sender.request, should.NotBeNil)
4345
f.So(f.sender.request.Method, should.Equal, "GET")
4446
f.So(f.sender.request.URL.Path, should.Equal, suggestURL)
4547
f.So(string(f.sender.request.URL.Query().Get("prefix")), should.Equal, "42")
4648
f.So(f.sender.request.URL.String(), should.Equal, suggestURL+"?prefix=42")
49+
f.So(f.sender.request.Context(), should.Resemble, ctx)
4750

4851
f.So(f.input.Results, should.Resemble, []*Suggestion{{Text: "1"}, {Text: "2"}, {Text: "3"}})
4952
}

us-extract-api/client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package extract
22

33
import (
4+
"context"
45
"encoding/json"
56
"net/http"
67

@@ -19,9 +20,18 @@ func NewClient(sender sdk.RequestSender) *Client {
1920

2021
// SendBatch sends the batch of inputs, populating the output for each input if the batch was successful.
2122
func (c *Client) SendLookup(lookup *Lookup) error {
23+
return c.SendLookupWithContext(context.Background(), lookup)
24+
}
25+
26+
func (c *Client) SendLookupWithContext(ctx context.Context, lookup *Lookup) error {
2227
if lookup == nil || len(lookup.Text) == 0 {
2328
return nil
24-
} else if response, err := c.sender.Send(buildRequest(lookup)); err != nil {
29+
}
30+
31+
request := buildRequest(lookup)
32+
request = request.WithContext(ctx)
33+
response, err := c.sender.Send(request)
34+
if err != nil {
2535
return err
2636
} else {
2737
return deserializeResponse(response, lookup)

0 commit comments

Comments
 (0)