@@ -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 ('' )
0 commit comments