@@ -34,6 +34,7 @@ def _generate_client(self, namespace):
34
34
for route in namespace .routes :
35
35
generate_doc (self , route )
36
36
self .emit (self ._generate_route_signature (namespace , route ))
37
+ self .emit (self ._generate_route_signature_context (namespace , route ))
37
38
self .emit ()
38
39
39
40
self .emit ('type apiImpl dropbox.Context' )
@@ -44,35 +45,46 @@ def _generate_client(self, namespace):
44
45
self .emit ('ctx := apiImpl(dropbox.NewContext(c))' )
45
46
self .emit ('return &ctx' )
46
47
47
- def _generate_route_signature (self , namespace , route ):
48
+ def _generate_route_signature (self , namespace , route , name_suffix = "" , initial_args = None ):
48
49
req = fmt_type (route .arg_data_type , namespace )
49
50
res = fmt_type (route .result_data_type , namespace , use_interface = True )
50
51
fn = fmt_var (route .name )
51
52
if route .version != 1 :
52
53
fn += 'V%d' % route .version
53
54
style = route .attrs .get ('style' , 'rpc' )
54
55
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}' )
59
67
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 ) + ')'
66
72
return signature .format (fn = fn , req = req , res = res )
67
73
68
74
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
+
69
79
def _generate_route (self , namespace , route ):
70
80
out = self .emit
71
81
72
82
route_name = route .name
73
83
if route .version != 1 :
74
84
route_name += '_v%d' % route .version
75
85
86
+ route_style = route .attrs .get ('style' , '' )
87
+
76
88
fn = fmt_var (route .name )
77
89
if route .version != 1 :
78
90
fn += 'V%d' % route .version
@@ -85,9 +97,9 @@ def _generate_route(self, namespace, route):
85
97
out ('EndpointError {err} `json:"error"`' .format (err = err ))
86
98
out ()
87
99
88
- signature = 'func (dbx *apiImpl) ' + self ._generate_route_signature (
100
+ signature_context = 'func (dbx *apiImpl) ' + self ._generate_route_signature_context (
89
101
namespace , route )
90
- with self .block (signature ):
102
+ with self .block (signature_context ):
91
103
if route .deprecated is not None :
92
104
out ('log.Printf("WARNING: API `%s` is deprecated")' % fn )
93
105
if route .deprecated .by is not None :
@@ -116,8 +128,8 @@ def _generate_route(self, namespace, route):
116
128
117
129
out ("var resp []byte" )
118
130
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" ))
121
133
with self .block ("if err != nil" ):
122
134
out ("var appErr {fn}APIError" .format (fn = fn ))
123
135
out ("err = {auth}ParseError(err, &appErr)" .format (
@@ -144,9 +156,20 @@ def _generate_route(self, namespace, route):
144
156
else :
145
157
out ("_ = resp" )
146
158
147
- if route . attrs . get ( 'style' , 'rpc' ) == "download" :
159
+ if route_style == "download" :
148
160
out ("content = respBody" )
149
161
else :
150
162
out ("_ = respBody" )
151
163
out ('return' )
152
164
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 ('' )
0 commit comments