Skip to content

Commit

Permalink
Merge pull request #34 from rinchsan/context-support
Browse files Browse the repository at this point in the history
  • Loading branch information
rinchsan authored Oct 9, 2021
2 parents 9e33767 + 1dff921 commit 849f8c0
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
## Installation

```bash
go get github.com/rinchsan/device-check-go
go get github.com/rinchsan/device-check-go/v2
```

## Getting started

### Initialize SDK

```go
import "github.com/rinchsan/device-check-go"
import "github.com/rinchsan/device-check-go/v2"

cred := devicecheck.NewCredentialFile("/path/to/private/key/file") // You can create credential also from raw string/bytes
cfg := devicecheck.NewConfig("ISSUER", "KEY_ID", devicecheck.Development)
Expand Down
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func newAPIWithHTTPClient(client *http.Client, env Environment) api {
}
}

func (api api) do(jwt, path string, requestBody interface{}) (int, string, error) {
func (api api) do(ctx context.Context, jwt, path string, requestBody interface{}) (int, string, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(requestBody); err != nil {
return 0, "", fmt.Errorf("json: %w", err)
}

req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, api.baseURL+path, buf)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, api.baseURL+path, buf)
if err != nil {
return 0, "", fmt.Errorf("http: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devicecheck

import (
"context"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -178,7 +179,7 @@ func TestAPI_do(t *testing.T) {
client: http.DefaultClient,
baseURL: c.baseURL,
}
code, body, err := api.do("jwt", c.path, c.body)
code, body, err := api.do(context.Background(), "jwt", c.path, c.body)

if c.noErr {
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions devicecheck_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package devicecheck_test

import (
"context"
"errors"
"testing"

devicecheck "github.com/rinchsan/device-check-go"
devicecheck "github.com/rinchsan/device-check-go/v2"
)

func Test(t *testing.T) {
Expand All @@ -14,7 +15,7 @@ func Test(t *testing.T) {
cfg := devicecheck.NewConfig("ISSUER", "KEY_ID", devicecheck.Development)
client := devicecheck.New(cred, cfg)

err := client.ValidateDeviceToken("token")
err := client.ValidateDeviceToken(context.Background(), "token")

if !errors.Is(err, devicecheck.ErrUnauthorized) {
t.Error("want 'devicecheck.ErrUnauthorized'")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/rinchsan/device-check-go
module github.com/rinchsan/device-check-go/v2

go 1.16

Expand Down
5 changes: 3 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devicecheck

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (t *Time) UnmarshalJSON(b []byte) error {
}

// QueryTwoBits queries two bits for device token. Returns ErrBitStateNotFound if the bits have not been set.
func (client *Client) QueryTwoBits(deviceToken string, result *QueryTwoBitsResult) error {
func (client *Client) QueryTwoBits(ctx context.Context, deviceToken string, result *QueryTwoBitsResult) error {
key, err := client.cred.key()
if err != nil {
return fmt.Errorf("devicecheck: failed to create key: %w", err)
Expand All @@ -64,7 +65,7 @@ func (client *Client) QueryTwoBits(deviceToken string, result *QueryTwoBitsResul
Timestamp: time.Now().UTC().UnixNano() / int64(time.Millisecond),
}

code, respBody, err := client.api.do(jwt, queryTwoBitsPath, body)
code, respBody, err := client.api.do(ctx, jwt, queryTwoBitsPath, body)
if err != nil {
return fmt.Errorf("devicecheck: failed to query two bits: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devicecheck

import (
"context"
"errors"
"io"
"net/http"
Expand Down Expand Up @@ -163,7 +164,7 @@ func TestClient_QueryTwoBits(t *testing.T) {
t.Parallel()

var result QueryTwoBitsResult
err := c.client.QueryTwoBits("device_token", &result)
err := c.client.QueryTwoBits(context.Background(), "device_token", &result)

if c.noErr {
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devicecheck

import (
"context"
"fmt"
"net/http"
"time"
Expand All @@ -19,7 +20,7 @@ type updateTwoBitsRequestBody struct {
}

// UpdateTwoBits updates two bits for device token.
func (client *Client) UpdateTwoBits(deviceToken string, bit0, bit1 bool) error {
func (client *Client) UpdateTwoBits(ctx context.Context, deviceToken string, bit0, bit1 bool) error {
key, err := client.cred.key()
if err != nil {
return fmt.Errorf("devicecheck: failed to create key: %w", err)
Expand All @@ -38,7 +39,7 @@ func (client *Client) UpdateTwoBits(deviceToken string, bit0, bit1 bool) error {
Bit1: bit1,
}

code, respBody, err := client.api.do(jwt, updateTwoBitsPath, body)
code, respBody, err := client.api.do(ctx, jwt, updateTwoBitsPath, body)
if err != nil {
return fmt.Errorf("devicecheck: failed to update two bits: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion update_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devicecheck

import (
"context"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestClient_UpdateTwoBits(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

err := c.client.UpdateTwoBits("device_token", true, true)
err := c.client.UpdateTwoBits(context.Background(), "device_token", true, true)

if c.noErr {
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions validate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devicecheck

import (
"context"
"fmt"
"net/http"
"time"
Expand All @@ -17,7 +18,7 @@ type validateDeviceTokenRequestBody struct {
}

// ValidateDeviceToken validates a device for device token.
func (client *Client) ValidateDeviceToken(deviceToken string) error {
func (client *Client) ValidateDeviceToken(ctx context.Context, deviceToken string) error {
key, err := client.cred.key()
if err != nil {
return fmt.Errorf("devicecheck: failed to create key: %w", err)
Expand All @@ -34,7 +35,7 @@ func (client *Client) ValidateDeviceToken(deviceToken string) error {
Timestamp: time.Now().UTC().UnixNano() / int64(time.Millisecond),
}

code, respBody, err := client.api.do(jwt, validateDeviceTokenPath, body)
code, respBody, err := client.api.do(ctx, jwt, validateDeviceTokenPath, body)
if err != nil {
return fmt.Errorf("devicecheck: failed to validate device token: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion validate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devicecheck

import (
"context"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestClient_ValidateDeviceToken(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

err := c.client.ValidateDeviceToken("device_token")
err := c.client.ValidateDeviceToken(context.Background(), "device_token")

if c.noErr {
if err != nil {
Expand Down

0 comments on commit 849f8c0

Please sign in to comment.