forked from tonicpow/go-handcash-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
75 lines (67 loc) · 1.67 KB
/
profile.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package handcash
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
/*
{
"publicProfile": {
"id": "1234567890",
"handle": "MisterZ",
"paymail": "[email protected]",
"displayName": "",
"avatarUrl": "https://beta-cloud.handcash.io/users/profilePicture/MisterZ",
"localCurrencyCode": "USD"
},
"privateProfile": {
"phoneNumber": "+15554443333",
"email": "[email protected]"
}
}
*/
// GetProfile will get the profile for the associated auth token
//
// Specs: https://github.com/HandCash/handcash-connect-sdk-js/blob/master/src/profile/index.js
func (c *Client) GetProfile(ctx context.Context, authToken string) (*Profile, error) {
// Make sure we have an auth token
if len(authToken) == 0 {
return nil, fmt.Errorf("missing auth token")
}
// Get the signed request
signed, err := c.getSignedRequest(
http.MethodGet,
endpointProfileCurrent,
authToken,
&requestBody{authToken: authToken},
currentISOTimestamp(),
)
if err != nil {
return nil, fmt.Errorf("error creating signed request: %w", err)
}
// Make the HTTP request
response := httpRequest(
ctx,
c,
&httpPayload{
Data: []byte(emptyBody),
ExpectedStatus: http.StatusOK,
Method: signed.Method,
URL: signed.URI,
},
signed,
)
// Error in request?
if response.Error != nil {
return nil, response.Error
}
// Unmarshal into the profile
profile := new(Profile)
if err = json.Unmarshal(response.BodyContents, &profile); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
} else if profile == nil || profile.PublicProfile.ID == "" {
return nil, fmt.Errorf("failed to find profile")
}
return profile, nil
}