-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathoauth_gen.go
115 lines (99 loc) · 2.88 KB
/
oauth_gen.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
103
104
105
106
107
108
109
110
111
112
113
114
115
package slack
// Auto-generated by internal/cmd/genmethods/genmethods.go. DO NOT EDIT!
import (
"context"
"net/url"
"strconv"
"strings"
"github.com/lestrrat-go/slack/objects"
"github.com/pkg/errors"
)
var _ = strconv.Itoa
var _ = strings.Index
var _ = objects.EpochTime(0)
// OAuthAccessCall is created by OAuthService.Access method call
type OAuthAccessCall struct {
service *OAuthService
clientID string
clientSecret string
code string
redirectURI string
}
// Access creates a OAuthAccessCall object in preparation for accessing the oauth.access endpoint
func (s *OAuthService) Access(clientID string, clientSecret string, code string) *OAuthAccessCall {
var call OAuthAccessCall
call.service = s
call.clientID = clientID
call.clientSecret = clientSecret
call.code = code
return &call
}
// RedirectURI sets the value for optional redirectURI parameter
func (c *OAuthAccessCall) RedirectURI(redirectURI string) *OAuthAccessCall {
c.redirectURI = redirectURI
return c
}
// ValidateArgs checks that all required fields are set in the OAuthAccessCall object
func (c *OAuthAccessCall) ValidateArgs() error {
if len(c.clientID) <= 0 {
return errors.New(`required field clientID not initialized`)
}
if len(c.clientSecret) <= 0 {
return errors.New(`required field clientSecret not initialized`)
}
if len(c.code) <= 0 {
return errors.New(`required field code not initialized`)
}
return nil
}
// Values returns the OAuthAccessCall object as url.Values
func (c *OAuthAccessCall) Values() (url.Values, error) {
if err := c.ValidateArgs(); err != nil {
return nil, errors.Wrap(err, `failed validation`)
}
v := url.Values{}
v.Set("client_id", c.clientID)
v.Set("client_secret", c.clientSecret)
v.Set("code", c.code)
if len(c.redirectURI) > 0 {
v.Set("redirect_uri", c.redirectURI)
}
return v, nil
}
// Do executes the call to access oauth.access endpoint
func (c *OAuthAccessCall) Do(ctx context.Context) (*objects.OAuthAccessResponse, error) {
const endpoint = "oauth.access"
v, err := c.Values()
if err != nil {
return nil, err
}
var res struct {
objects.GenericResponse
*objects.OAuthAccessResponse
}
if err := c.service.client.postForm(ctx, endpoint, v, &res); err != nil {
return nil, errors.Wrap(err, `failed to post to oauth.access`)
}
if !res.OK {
return nil, errors.New(res.Error.String())
}
return res.OAuthAccessResponse, nil
}
// FromValues parses the data in v and populates `c`
func (c *OAuthAccessCall) FromValues(v url.Values) error {
var tmp OAuthAccessCall
if raw := strings.TrimSpace(v.Get("client_id")); len(raw) > 0 {
tmp.clientID = raw
}
if raw := strings.TrimSpace(v.Get("client_secret")); len(raw) > 0 {
tmp.clientSecret = raw
}
if raw := strings.TrimSpace(v.Get("code")); len(raw) > 0 {
tmp.code = raw
}
if raw := strings.TrimSpace(v.Get("redirect_uri")); len(raw) > 0 {
tmp.redirectURI = raw
}
*c = tmp
return nil
}