-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckin.go
More file actions
78 lines (68 loc) · 2.01 KB
/
Copy pathcheckin.go
File metadata and controls
78 lines (68 loc) · 2.01 KB
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
package pushreceiver
import (
"bytes"
"context"
"io"
"net/http"
pb "github.com/beeper/push-receiver/pb/checkin"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
func CheckIn(ctx context.Context, opt *GCMCredentials) (*GCMCredentials, error) {
resp, err := checkin(ctx, opt)
if err != nil {
return nil, err
}
creds := &GCMCredentials{
AndroidID: *resp.AndroidId,
SecurityToken: *resp.SecurityToken,
}
return creds, nil
}
func checkin(ctx context.Context, opt *GCMCredentials) (*pb.AndroidCheckinResponse, error) {
id := int64(opt.AndroidID)
r := &pb.AndroidCheckinRequest{
Checkin: &pb.AndroidCheckinProto{
ChromeBuild: &pb.ChromeBuildProto{
Platform: pb.ChromeBuildProto_PLATFORM_LINUX.Enum(),
ChromeVersion: proto.String(chromeVersion),
Channel: pb.ChromeBuildProto_CHANNEL_STABLE.Enum(),
},
Type: pb.DeviceType_DEVICE_CHROME_BROWSER.Enum(),
UserNumber: proto.Int32(0),
},
Fragment: proto.Int32(0),
Version: proto.Int32(3),
UserSerialNumber: proto.Int32(0),
Id: &id,
SecurityToken: &opt.SecurityToken,
}
message, err := proto.Marshal(r)
if err != nil {
return nil, errors.Wrap(err, "marshal GCM checkin request")
}
res, err := postRequest(ctx, checkinURL, bytes.NewReader(message), func(header *http.Header) {
header.Set("Content-Type", "application/x-protobuf")
})
if err != nil {
return nil, errors.Wrap(err, "request GCM checkin")
}
defer closeResponse(res)
// unauthorized error
if res.StatusCode == http.StatusUnauthorized {
return nil, ErrGcmAuthorization
}
if res.StatusCode < 200 || res.StatusCode > 299 {
return nil, errors.Errorf("server error: %s", res.Status)
}
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "read GCM checkin response")
}
var responseProto pb.AndroidCheckinResponse
err = proto.Unmarshal(data, &responseProto)
if err != nil {
return nil, errors.Wrapf(err, "unmarshal GCM checkin response")
}
return &responseProto, nil
}