-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
101 lines (91 loc) · 2.58 KB
/
Copy pathclient.go
File metadata and controls
101 lines (91 loc) · 2.58 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ftapi
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/nikunicke/ftapi/oauth2"
)
// Service includes an http client with authorization to communicate with the
// 42 API.
type Service struct {
c *http.Client
baseURL string
}
// ServerResponse includes data from the response in its original form
type ServerResponse struct {
Body io.ReadCloser
Header http.Header
StatusCode int
}
// GrantType specifies the gran type to be used for authentication
type GrantType int
const (
// ClientCredentials grant type is used by client to obtain an access token
// outside of the context of a user. This is typically used by clients to
// access resources about themselves rather than to access a users's
// resources.
ClientCredentials GrantType = iota
// AuthorizationCode grant type is used by confidential and public client to
// exchange an authorization code for an access token. After the user
// returns to the client vie the redirect URL, the application will get the
// authorization code from the URL and use it to request an access token.
AuthorizationCode
)
// NewService returns an Service or an error. The grant type must be specified:
// - ClientCredentials
// - AuthorizationCode: default
func NewService(gt GrantType) (*Service, error) {
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
return nil, err
}
switch gt {
case ClientCredentials:
return client(&oauth2.ClientCredentials{B: b})
default:
return client(&oauth2.AuthorizationCode{B: b})
}
}
// Me gets the current users userdata. Exactly one of *UsersItem or error will
// be non-nil. Any non 2xx status code is an error
func (s *Service) Me() (*UsersItem, error) {
urls := s.baseURL + "/me"
req, err := http.NewRequest(http.MethodGet, urls, nil)
if err != nil {
return nil, err
}
res, err := s.Do(req)
if err != nil {
return nil, err
}
if err := checkResponse(res); err != nil {
return nil, err
}
ret := &UsersItem{
ServerResponse: ServerResponse{
Header: res.Header,
StatusCode: res.StatusCode,
Body: res.Body,
},
}
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
// Do commits a request to the URL specified in the request
func (s *Service) Do(req *http.Request) (*http.Response, error) {
resp, err := s.c.Do(req)
if err != nil {
return nil, err
}
return resp, err
}
func client(config oauth2.Config) (*Service, error) {
token, err := config.Token()
if err != nil {
return nil, err
}
return &Service{c: config.Client(token), baseURL: "https://api.intra.42.fr/v2"}, nil
}