-
Notifications
You must be signed in to change notification settings - Fork 0
/
Authorization.go
102 lines (85 loc) · 2.58 KB
/
Authorization.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package metrafin
import (
"errors"
)
// Authorization represents an authorization created by an Application
type Authorization struct {
application *Application
AccessToken string
}
// TokenInfoRes represents responses from a documented endpoint: https://github.com/metrafin/documentation#get-v1token
type TokenInfoRes struct {
Error string `json:"error"`
Scopes []string `json:"scopes"`
UserID string `json:"userId"`
Expires string `json:"expires"`
}
// A home address
type homeAddress struct {
Full string `json:"full"`
Line1 string `json:"line1"`
Line2 string `json:"line2"`
City string `json:"city"`
AdministrativeDivision string `json:"administrativeDivision"`
AdministrativeRegion string `json:"administrativeRegion"`
PostalCode string `json:"postalCode"`
CountryCode string `json:"countryCode"`
}
// ProfileRes represents responses from a documented endpoint: https://github.com/metrafin/documentation#get-v1userprofile
type ProfileRes struct {
Error string `json:"error"`
UserID string `json:"userId"`
Username string `json:"username"`
Created string `json:"created"`
Verified struct {
FirstName string `json:"firstName"`
MiddleName string `json:"middleName"`
LastName string `json:"lastName"`
Country string `json:"country"`
HomeAddress homeAddress `json:"homeAddress"`
Age int `json:"age"`
Phone string `json:"phone"`
}
}
// FetchInfo retrieves stats about authorization.
func (a *Authorization) FetchInfo() (info *TokenInfoRes, err error) {
auth := *a
app := *auth.application
parsed := &TokenInfoRes{}
err = doRequest(request{
URL: "https://api.metrafin.com/v1/token",
Method: "GET",
Data: &[]byte{},
Headers: &map[string]string{
"Authorization": app.PrivateToken + ":" + auth.AccessToken,
},
}, nil, parsed)
if err != nil {
return nil, err
}
if parsed.Error != "" {
return nil, errors.New(parsed.Error)
}
return parsed, nil
}
// FetchProfile retrieves profile information of user.
func (a *Authorization) FetchProfile() (profile *ProfileRes, err error) {
auth := *a
app := *auth.application
parsed := &ProfileRes{}
err = doRequest(request{
URL: "https://api.metrafin.com/v1/user/profile",
Method: "GET",
Data: &[]byte{},
Headers: &map[string]string{
"Authorization": app.PrivateToken + ":" + auth.AccessToken,
},
}, nil, parsed)
if err != nil {
return nil, err
}
if parsed.Error != "" {
return nil, errors.New(parsed.Error)
}
return parsed, nil
}