Skip to content

Commit 6621f98

Browse files
committed
Add context.Context support to API methods, generate new version
1 parent b49d68a commit 6621f98

File tree

16 files changed

+2347
-1026
lines changed

16 files changed

+2347
-1026
lines changed

generator/go_client.stoneg.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def _generate_client(self, namespace):
3434
for route in namespace.routes:
3535
generate_doc(self, route)
3636
self.emit(self._generate_route_signature(namespace, route))
37+
self.emit(self._generate_route_signature_context(namespace, route))
3738
self.emit()
3839

3940
self.emit('type apiImpl dropbox.Context')
@@ -44,35 +45,46 @@ def _generate_client(self, namespace):
4445
self.emit('ctx := apiImpl(dropbox.NewContext(c))')
4546
self.emit('return &ctx')
4647

47-
def _generate_route_signature(self, namespace, route):
48+
def _generate_route_signature(self, namespace, route, name_suffix="", initial_args=None):
4849
req = fmt_type(route.arg_data_type, namespace)
4950
res = fmt_type(route.result_data_type, namespace, use_interface=True)
5051
fn = fmt_var(route.name)
5152
if route.version != 1:
5253
fn += 'V%d' % route.version
5354
style = route.attrs.get('style', 'rpc')
5455

55-
arg = '' if is_void_type(route.arg_data_type) else 'arg {req}'
56-
ret = '(err error)' if is_void_type(route.result_data_type) else \
57-
'(res {res}, err error)'
58-
signature = '{fn}(' + arg + ') ' + ret
56+
args = []
57+
if initial_args:
58+
args.extend(initial_args)
59+
if not is_void_type(route.arg_data_type):
60+
args.append('arg {req}')
61+
if style == 'upload':
62+
args.append('content io.Reader')
63+
64+
rets = []
65+
if not is_void_type(route.result_data_type):
66+
rets.append('res {res}')
5967
if style == 'download':
60-
signature = '{fn}(' + arg + \
61-
') (res {res}, content io.ReadCloser, err error)'
62-
elif style == 'upload':
63-
signature = '{fn}(' + arg + ', content io.Reader) ' + ret
64-
if is_void_type(route.arg_data_type):
65-
signature = '{fn}(content io.Reader) ' + ret
68+
rets.append('content io.ReadCloser')
69+
rets.append('err error')
70+
71+
signature = '{fn}' + name_suffix + '(' + ", ".join(args) + ') (' + ", ".join(rets) + ')'
6672
return signature.format(fn=fn, req=req, res=res)
6773

6874

75+
def _generate_route_signature_context(self, namespace, route):
76+
return self._generate_route_signature(namespace, route, name_suffix="Context", initial_args=['ctx context.Context'])
77+
78+
6979
def _generate_route(self, namespace, route):
7080
out = self.emit
7181

7282
route_name = route.name
7383
if route.version != 1:
7484
route_name += '_v%d' % route.version
7585

86+
route_style = route.attrs.get('style', '')
87+
7688
fn = fmt_var(route.name)
7789
if route.version != 1:
7890
fn += 'V%d' % route.version
@@ -85,9 +97,9 @@ def _generate_route(self, namespace, route):
8597
out('EndpointError {err} `json:"error"`'.format(err=err))
8698
out()
8799

88-
signature = 'func (dbx *apiImpl) ' + self._generate_route_signature(
100+
signature_context = 'func (dbx *apiImpl) ' + self._generate_route_signature_context(
89101
namespace, route)
90-
with self.block(signature):
102+
with self.block(signature_context):
91103
if route.deprecated is not None:
92104
out('log.Printf("WARNING: API `%s` is deprecated")' % fn)
93105
if route.deprecated.by is not None:
@@ -116,8 +128,8 @@ def _generate_route(self, namespace, route):
116128

117129
out("var resp []byte")
118130
out("var respBody io.ReadCloser")
119-
out("resp, respBody, err = (*dropbox.Context)(dbx).Execute(req, {body})".format(
120-
body="content" if route.attrs.get('style', '') == 'upload' else "nil"))
131+
out("resp, respBody, err = (*dropbox.Context)(dbx).Execute(ctx, req, {body})".format(
132+
body="content" if route_style == 'upload' else "nil"))
121133
with self.block("if err != nil"):
122134
out("var appErr {fn}APIError".format(fn=fn))
123135
out("err = {auth}ParseError(err, &appErr)".format(
@@ -144,9 +156,20 @@ def _generate_route(self, namespace, route):
144156
else:
145157
out("_ = resp")
146158

147-
if route.attrs.get('style', 'rpc') == "download":
159+
if route_style == "download":
148160
out("content = respBody")
149161
else:
150162
out("_ = respBody")
151163
out('return')
152164
out()
165+
166+
signature = 'func (dbx *apiImpl) ' + self._generate_route_signature(
167+
namespace, route)
168+
with self.block(signature):
169+
args = ["context.Background()"]
170+
if not is_void_type(route.arg_data_type):
171+
args.append('arg')
172+
if route_style == "upload":
173+
args.append('content')
174+
out('return dbx.' + fn + 'Context(' + ", ".join(args) + ');')
175+
out('')

generator/go_rsrc/sdk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ type Request struct {
167167
ExtraHeaders map[string]string
168168
}
169169

170-
func (c *Context) Execute(req Request, body io.Reader) ([]byte, io.ReadCloser, error) {
170+
func (c *Context) Execute(ctx context.Context, req Request, body io.Reader) ([]byte, io.ReadCloser, error) {
171171
url := c.URLGenerator(req.Host, req.Namespace, req.Route)
172-
httpReq, err := http.NewRequest("POST", url, body)
172+
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, body)
173173
if err != nil {
174174
return nil, nil, err
175175
}

v6/dropbox/account/client.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package account
2222

2323
import (
24+
"context"
2425
"encoding/json"
2526
"io"
2627

@@ -32,17 +33,18 @@ import (
3233
type Client interface {
3334
// SetProfilePhoto : Sets a user's profile photo.
3435
SetProfilePhoto(arg *SetProfilePhotoArg) (res *SetProfilePhotoResult, err error)
36+
SetProfilePhotoContext(ctx context.Context, arg *SetProfilePhotoArg) (res *SetProfilePhotoResult, err error)
3537
}
3638

3739
type apiImpl dropbox.Context
3840

39-
//SetProfilePhotoAPIError is an error-wrapper for the set_profile_photo route
41+
// SetProfilePhotoAPIError is an error-wrapper for the set_profile_photo route
4042
type SetProfilePhotoAPIError struct {
4143
dropbox.APIError
4244
EndpointError *SetProfilePhotoError `json:"error"`
4345
}
4446

45-
func (dbx *apiImpl) SetProfilePhoto(arg *SetProfilePhotoArg) (res *SetProfilePhotoResult, err error) {
47+
func (dbx *apiImpl) SetProfilePhotoContext(ctx context.Context, arg *SetProfilePhotoArg) (res *SetProfilePhotoResult, err error) {
4648
req := dropbox.Request{
4749
Host: "api",
4850
Namespace: "account",
@@ -55,7 +57,7 @@ func (dbx *apiImpl) SetProfilePhoto(arg *SetProfilePhotoArg) (res *SetProfilePho
5557

5658
var resp []byte
5759
var respBody io.ReadCloser
58-
resp, respBody, err = (*dropbox.Context)(dbx).Execute(req, nil)
60+
resp, respBody, err = (*dropbox.Context)(dbx).Execute(ctx, req, nil)
5961
if err != nil {
6062
var appErr SetProfilePhotoAPIError
6163
err = auth.ParseError(err, &appErr)
@@ -74,6 +76,10 @@ func (dbx *apiImpl) SetProfilePhoto(arg *SetProfilePhotoArg) (res *SetProfilePho
7476
return
7577
}
7678

79+
func (dbx *apiImpl) SetProfilePhoto(arg *SetProfilePhotoArg) (res *SetProfilePhotoResult, err error) {
80+
return dbx.SetProfilePhotoContext(context.Background(), arg)
81+
}
82+
7783
// New returns a Client implementation for this namespace
7884
func New(c dropbox.Config) Client {
7985
ctx := apiImpl(dropbox.NewContext(c))

v6/dropbox/auth/client.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package auth
2222

2323
import (
24+
"context"
2425
"encoding/json"
2526
"io"
2627

@@ -32,22 +33,24 @@ type Client interface {
3233
// TokenFromOauth1 : Creates an OAuth 2.0 access token from the supplied
3334
// OAuth 1.0 access token.
3435
TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAuth1Result, err error)
36+
TokenFromOauth1Context(ctx context.Context, arg *TokenFromOAuth1Arg) (res *TokenFromOAuth1Result, err error)
3537
// TokenRevoke : Disables the access token used to authenticate the call. If
3638
// there is a corresponding refresh token for the access token, this
3739
// disables that refresh token, as well as any other access tokens for that
3840
// refresh token.
3941
TokenRevoke() (err error)
42+
TokenRevokeContext(ctx context.Context) (err error)
4043
}
4144

4245
type apiImpl dropbox.Context
4346

44-
//TokenFromOauth1APIError is an error-wrapper for the token/from_oauth1 route
47+
// TokenFromOauth1APIError is an error-wrapper for the token/from_oauth1 route
4548
type TokenFromOauth1APIError struct {
4649
dropbox.APIError
4750
EndpointError *TokenFromOAuth1Error `json:"error"`
4851
}
4952

50-
func (dbx *apiImpl) TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAuth1Result, err error) {
53+
func (dbx *apiImpl) TokenFromOauth1Context(ctx context.Context, arg *TokenFromOAuth1Arg) (res *TokenFromOAuth1Result, err error) {
5154
req := dropbox.Request{
5255
Host: "api",
5356
Namespace: "auth",
@@ -60,7 +63,7 @@ func (dbx *apiImpl) TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAut
6063

6164
var resp []byte
6265
var respBody io.ReadCloser
63-
resp, respBody, err = (*dropbox.Context)(dbx).Execute(req, nil)
66+
resp, respBody, err = (*dropbox.Context)(dbx).Execute(ctx, req, nil)
6467
if err != nil {
6568
var appErr TokenFromOauth1APIError
6669
err = ParseError(err, &appErr)
@@ -79,13 +82,17 @@ func (dbx *apiImpl) TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAut
7982
return
8083
}
8184

82-
//TokenRevokeAPIError is an error-wrapper for the token/revoke route
85+
func (dbx *apiImpl) TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAuth1Result, err error) {
86+
return dbx.TokenFromOauth1Context(context.Background(), arg)
87+
}
88+
89+
// TokenRevokeAPIError is an error-wrapper for the token/revoke route
8390
type TokenRevokeAPIError struct {
8491
dropbox.APIError
8592
EndpointError struct{} `json:"error"`
8693
}
8794

88-
func (dbx *apiImpl) TokenRevoke() (err error) {
95+
func (dbx *apiImpl) TokenRevokeContext(ctx context.Context) (err error) {
8996
req := dropbox.Request{
9097
Host: "api",
9198
Namespace: "auth",
@@ -98,7 +105,7 @@ func (dbx *apiImpl) TokenRevoke() (err error) {
98105

99106
var resp []byte
100107
var respBody io.ReadCloser
101-
resp, respBody, err = (*dropbox.Context)(dbx).Execute(req, nil)
108+
resp, respBody, err = (*dropbox.Context)(dbx).Execute(ctx, req, nil)
102109
if err != nil {
103110
var appErr TokenRevokeAPIError
104111
err = ParseError(err, &appErr)
@@ -113,6 +120,10 @@ func (dbx *apiImpl) TokenRevoke() (err error) {
113120
return
114121
}
115122

123+
func (dbx *apiImpl) TokenRevoke() (err error) {
124+
return dbx.TokenRevokeContext(context.Background())
125+
}
126+
116127
// New returns a Client implementation for this namespace
117128
func New(c dropbox.Config) Client {
118129
ctx := apiImpl(dropbox.NewContext(c))

v6/dropbox/check/client.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package check
2222

2323
import (
24+
"context"
2425
"encoding/json"
2526
"io"
2627

@@ -37,24 +38,26 @@ type Client interface {
3738
// least part of the Dropbox API infrastructure is working and that the app
3839
// key and secret valid.
3940
App(arg *EchoArg) (res *EchoResult, err error)
41+
AppContext(ctx context.Context, arg *EchoArg) (res *EchoResult, err error)
4042
// User : This endpoint performs User Authentication, validating the
4143
// supplied access token, and returns the supplied string, to allow you to
4244
// test your code and connection to the Dropbox API. It has no other effect.
4345
// If you receive an HTTP 200 response with the supplied query, it indicates
4446
// at least part of the Dropbox API infrastructure is working and that the
4547
// access token is valid.
4648
User(arg *EchoArg) (res *EchoResult, err error)
49+
UserContext(ctx context.Context, arg *EchoArg) (res *EchoResult, err error)
4750
}
4851

4952
type apiImpl dropbox.Context
5053

51-
//AppAPIError is an error-wrapper for the app route
54+
// AppAPIError is an error-wrapper for the app route
5255
type AppAPIError struct {
5356
dropbox.APIError
5457
EndpointError struct{} `json:"error"`
5558
}
5659

57-
func (dbx *apiImpl) App(arg *EchoArg) (res *EchoResult, err error) {
60+
func (dbx *apiImpl) AppContext(ctx context.Context, arg *EchoArg) (res *EchoResult, err error) {
5861
req := dropbox.Request{
5962
Host: "api",
6063
Namespace: "check",
@@ -67,7 +70,7 @@ func (dbx *apiImpl) App(arg *EchoArg) (res *EchoResult, err error) {
6770

6871
var resp []byte
6972
var respBody io.ReadCloser
70-
resp, respBody, err = (*dropbox.Context)(dbx).Execute(req, nil)
73+
resp, respBody, err = (*dropbox.Context)(dbx).Execute(ctx, req, nil)
7174
if err != nil {
7275
var appErr AppAPIError
7376
err = auth.ParseError(err, &appErr)
@@ -86,13 +89,17 @@ func (dbx *apiImpl) App(arg *EchoArg) (res *EchoResult, err error) {
8689
return
8790
}
8891

89-
//UserAPIError is an error-wrapper for the user route
92+
func (dbx *apiImpl) App(arg *EchoArg) (res *EchoResult, err error) {
93+
return dbx.AppContext(context.Background(), arg)
94+
}
95+
96+
// UserAPIError is an error-wrapper for the user route
9097
type UserAPIError struct {
9198
dropbox.APIError
9299
EndpointError struct{} `json:"error"`
93100
}
94101

95-
func (dbx *apiImpl) User(arg *EchoArg) (res *EchoResult, err error) {
102+
func (dbx *apiImpl) UserContext(ctx context.Context, arg *EchoArg) (res *EchoResult, err error) {
96103
req := dropbox.Request{
97104
Host: "api",
98105
Namespace: "check",
@@ -105,7 +112,7 @@ func (dbx *apiImpl) User(arg *EchoArg) (res *EchoResult, err error) {
105112

106113
var resp []byte
107114
var respBody io.ReadCloser
108-
resp, respBody, err = (*dropbox.Context)(dbx).Execute(req, nil)
115+
resp, respBody, err = (*dropbox.Context)(dbx).Execute(ctx, req, nil)
109116
if err != nil {
110117
var appErr UserAPIError
111118
err = auth.ParseError(err, &appErr)
@@ -124,6 +131,10 @@ func (dbx *apiImpl) User(arg *EchoArg) (res *EchoResult, err error) {
124131
return
125132
}
126133

134+
func (dbx *apiImpl) User(arg *EchoArg) (res *EchoResult, err error) {
135+
return dbx.UserContext(context.Background(), arg)
136+
}
137+
127138
// New returns a Client implementation for this namespace
128139
func New(c dropbox.Config) Client {
129140
ctx := apiImpl(dropbox.NewContext(c))

0 commit comments

Comments
 (0)