-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate.go
52 lines (42 loc) · 1.27 KB
/
update.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
package devicecheck
import (
"context"
"fmt"
"net/http"
"time"
"github.com/google/uuid"
)
const updateTwoBitsPath = "/update_two_bits"
type updateTwoBitsRequestBody struct {
DeviceToken string `json:"device_token"`
TransactionID string `json:"transaction_id"`
Timestamp int64 `json:"timestamp"`
Bit0 bool `json:"bit0"`
Bit1 bool `json:"bit1"`
}
// UpdateTwoBits updates two bits for device token.
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)
}
jwt, err := client.jwt.generate(key)
if err != nil {
return fmt.Errorf("devicecheck: failed to generate jwt: %w", err)
}
body := updateTwoBitsRequestBody{
DeviceToken: deviceToken,
TransactionID: uuid.New().String(),
Timestamp: time.Now().UTC().UnixNano() / int64(time.Millisecond),
Bit0: bit0,
Bit1: bit1,
}
code, respBody, err := client.api.do(ctx, jwt, updateTwoBitsPath, body)
if err != nil {
return fmt.Errorf("devicecheck: failed to update two bits: %w: %s", err, respBody)
}
if code != http.StatusOK {
return fmt.Errorf("devicecheck: %w", newError(code, respBody))
}
return nil
}