-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccesstoken.go
More file actions
188 lines (165 loc) · 6.49 KB
/
accesstoken.go
File metadata and controls
188 lines (165 loc) · 6.49 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package finchgo
import (
"bytes"
"context"
"errors"
"net/http"
"slices"
"github.com/Finch-API/finch-api-go/internal/apijson"
"github.com/Finch-API/finch-api-go/internal/param"
"github.com/Finch-API/finch-api-go/internal/requestconfig"
"github.com/Finch-API/finch-api-go/option"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// AccessTokenService contains methods and other services that help with
// interacting with the Finch API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewAccessTokenService] method instead.
type AccessTokenService struct {
Options []option.RequestOption
}
// NewAccessTokenService generates a new service that applies the given options to
// each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewAccessTokenService(opts ...option.RequestOption) (r *AccessTokenService) {
r = &AccessTokenService{}
r.Options = opts
return
}
// Exchange the authorization code for an access token
func (r *AccessTokenService) New(ctx context.Context, body AccessTokenNewParams, opts ...option.RequestOption) (res *CreateAccessTokenResponse, err error) {
opts = slices.Concat(r.Options, opts)
opts = append(opts[:], requestconfig.RequestOptionFunc(func(rc *requestconfig.RequestConfig) (err error) {
if body, ok := rc.Body.(*bytes.Buffer); ok {
b := body.Bytes()[:]
bodyClientID := gjson.GetBytes(b, "client_id")
if !bodyClientID.Exists() {
if rc.ClientID == "" {
return errors.New("client_id must be provided as an argument or with the FINCH_CLIENT_ID environment variable")
}
b, err = sjson.SetBytes(b, "client_id", rc.ClientID)
if err != nil {
return err
}
rc.Body = bytes.NewBuffer(b)
}
bodyClientSecret := gjson.GetBytes(b, "client_secret")
if !bodyClientSecret.Exists() {
if rc.ClientSecret == "" {
return errors.New("client_secret must be provided as an argument or with the FINCH_CLIENT_SECRET environment variable")
}
b, err = sjson.SetBytes(b, "client_secret", rc.ClientSecret)
if err != nil {
return err
}
rc.Body = bytes.NewBuffer(b)
}
}
return nil
}))
path := "auth/token"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
type CreateAccessTokenResponse struct {
// The access token for the connection
AccessToken string `json:"access_token,required"`
// The type of application associated with a token.
ClientType CreateAccessTokenResponseClientType `json:"client_type,required"`
// The Finch UUID of the connection associated with the `access_token`
ConnectionID string `json:"connection_id,required"`
// The type of the connection associated with the token.
//
// - `provider` - connection to an external provider
// - `finch` - finch-generated data.
ConnectionType CreateAccessTokenResponseConnectionType `json:"connection_type,required"`
// An array of the authorized products associated with the `access_token`
Products []string `json:"products,required"`
// The ID of the provider associated with the `access_token`
ProviderID string `json:"provider_id,required"`
// The RFC 8693 token type (Finch uses `bearer` tokens)
TokenType string `json:"token_type,required"`
// [DEPRECATED] Use `connection_id` to identify the connection instead of this
// account ID
//
// Deprecated: deprecated
AccountID string `json:"account_id"`
// [DEPRECATED] Use `connection_id` to identify the connection instead of this
// company ID
//
// Deprecated: deprecated
CompanyID string `json:"company_id"`
// The ID of your customer you provided to Finch when a connect session was created
// for this connection
CustomerID string `json:"customer_id,nullable"`
JSON createAccessTokenResponseJSON `json:"-"`
}
// createAccessTokenResponseJSON contains the JSON metadata for the struct
// [CreateAccessTokenResponse]
type createAccessTokenResponseJSON struct {
AccessToken apijson.Field
ClientType apijson.Field
ConnectionID apijson.Field
ConnectionType apijson.Field
Products apijson.Field
ProviderID apijson.Field
TokenType apijson.Field
AccountID apijson.Field
CompanyID apijson.Field
CustomerID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CreateAccessTokenResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r createAccessTokenResponseJSON) RawJSON() string {
return r.raw
}
// The type of application associated with a token.
type CreateAccessTokenResponseClientType string
const (
CreateAccessTokenResponseClientTypeDevelopment CreateAccessTokenResponseClientType = "development"
CreateAccessTokenResponseClientTypeProduction CreateAccessTokenResponseClientType = "production"
CreateAccessTokenResponseClientTypeSandbox CreateAccessTokenResponseClientType = "sandbox"
)
func (r CreateAccessTokenResponseClientType) IsKnown() bool {
switch r {
case CreateAccessTokenResponseClientTypeDevelopment, CreateAccessTokenResponseClientTypeProduction, CreateAccessTokenResponseClientTypeSandbox:
return true
}
return false
}
// The type of the connection associated with the token.
//
// - `provider` - connection to an external provider
// - `finch` - finch-generated data.
type CreateAccessTokenResponseConnectionType string
const (
CreateAccessTokenResponseConnectionTypeFinch CreateAccessTokenResponseConnectionType = "finch"
CreateAccessTokenResponseConnectionTypeProvider CreateAccessTokenResponseConnectionType = "provider"
)
func (r CreateAccessTokenResponseConnectionType) IsKnown() bool {
switch r {
case CreateAccessTokenResponseConnectionTypeFinch, CreateAccessTokenResponseConnectionTypeProvider:
return true
}
return false
}
type AccessTokenNewParams struct {
// The authorization code received from the authorization server
Code param.Field[string] `json:"code,required"`
// The client ID for your application
ClientID param.Field[string] `json:"client_id" format:"uuid"`
// The client secret for your application
ClientSecret param.Field[string] `json:"client_secret"`
// The redirect URI used in the authorization request (optional)
RedirectUri param.Field[string] `json:"redirect_uri"`
}
func (r AccessTokenNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}