Skip to content

Commit 9e3a187

Browse files
authored
Merge pull request #564 from jonamireh/feature/request
Add an option to generate API calls using a DbxRequest wrapper
2 parents d499d4e + 511564e commit 9e3a187

12 files changed

Lines changed: 291 additions & 0 deletions

File tree

core/api/core.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ public class com/dropbox/core/DbxPKCEWebAuth {
135135
public fun finishFromRedirect (Ljava/lang/String;Lcom/dropbox/core/DbxSessionStore;Ljava/util/Map;)Lcom/dropbox/core/DbxAuthFinish;
136136
}
137137

138+
public abstract interface class com/dropbox/core/DbxRequest {
139+
public abstract fun call ()Ljava/lang/Object;
140+
}
141+
138142
public class com/dropbox/core/DbxRequestConfig {
139143
public fun <init> (Ljava/lang/String;)V
140144
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ tasks.named("generateTestStone", StoneTask) {
306306
),
307307
new StoneConfig(
308308
packageName: packageName,
309+
generateRequest: true,
309310
client: new ClientSpec(
310311
name: 'DbxClientV2Base',
311312
javadoc: 'TestClass.',

core/build/generated_stone_source/test/src/com/dropbox/core/stone/test/DbxTestTestRequests.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.dropbox.core.DbxApiException;
77
import com.dropbox.core.DbxDownloader;
88
import com.dropbox.core.DbxException;
9+
import com.dropbox.core.DbxRequest;
910
import com.dropbox.core.DbxUploader;
1011
import com.dropbox.core.DbxWrappedException;
1112
import com.dropbox.core.http.HttpRequestor;
@@ -77,6 +78,21 @@ public DbxDownloader<Fish> testDownload(String name, String breed) throws DbxApi
7778
return testDownload(_arg, Collections.<HttpRequestor.Header>emptyList());
7879
}
7980

81+
/**
82+
* See {@link DbxTestTestRequests#testDownload(String,String)}.
83+
*
84+
* @param name Used in {@link
85+
* DbxTestTestRequests#testRouteV2(String,Date)}. Must not be {@code
86+
* null}.
87+
* @param breed Must not be {@code null}.
88+
*
89+
* @return A {@link DbxRequest} that can be executed later to obtain the
90+
* {@code DbxDownloader&lt;Fish&gt;}.
91+
*/
92+
public DbxRequest<DbxDownloader<Fish>> testDownloadRequest(String name, String breed) {
93+
return () -> testDownload(name, breed);
94+
}
95+
8096
/**
8197
*
8298
* @param name Used in {@link
@@ -138,6 +154,20 @@ public DbxDownloader<Fish> testDownloadV2(UninitializedReason reason, String ses
138154
return testDownloadV2(_arg, Collections.<HttpRequestor.Header>emptyList());
139155
}
140156

157+
/**
158+
* See {@link
159+
* DbxTestTestRequests#testDownloadV2(UninitializedReason,String)}.
160+
*
161+
* @param reason Must not be {@code null}.
162+
* @param sessionId Must not be {@code null}.
163+
*
164+
* @return A {@link DbxRequest} that can be executed later to obtain the
165+
* {@code DbxDownloader&lt;Fish&gt;}.
166+
*/
167+
public DbxRequest<DbxDownloader<Fish>> testDownloadV2Request(UninitializedReason reason, String sessionId) {
168+
return () -> testDownloadV2(reason, sessionId);
169+
}
170+
141171
/**
142172
*
143173
* @param reason Must not be {@code null}.
@@ -175,6 +205,15 @@ public void testRoute() throws DbxApiException, DbxException {
175205
}
176206
}
177207

208+
/**
209+
* See {@link DbxTestTestRequests#testRoute}.
210+
*
211+
* @return A {@link DbxRequest} that can be executed later.
212+
*/
213+
public DbxRequest<java.lang.Void> testRouteRequest() {
214+
return () -> { testRoute(); return null; };
215+
}
216+
178217
//
179218
// route 2/test/test_route_v2
180219
//
@@ -211,6 +250,19 @@ public void testRouteV2(String name) throws ParentUnionException, DbxException {
211250
testRouteV2(_arg);
212251
}
213252

253+
/**
254+
* See {@link DbxTestTestRequests#testRouteV2(String,Date)}.
255+
*
256+
* @param name Used in {@link
257+
* DbxTestTestRequests#testRouteV2(String,Date)}. Must not be {@code
258+
* null}.
259+
*
260+
* @return A {@link DbxRequest} that can be executed later.
261+
*/
262+
public DbxRequest<java.lang.Void> testRouteV2Request(String name) {
263+
return () -> { testRouteV2(name); return null; };
264+
}
265+
214266
/**
215267
*
216268
* @param name Used in {@link
@@ -225,6 +277,19 @@ public void testRouteV2(String name, Date born) throws ParentUnionException, Dbx
225277
testRouteV2(_arg);
226278
}
227279

280+
/**
281+
* See {@link DbxTestTestRequests#testRouteV2(String,Date)}.
282+
*
283+
* @param name Used in {@link
284+
* DbxTestTestRequests#testRouteV2(String,Date)}. Must not be {@code
285+
* null}.
286+
*
287+
* @return A {@link DbxRequest} that can be executed later.
288+
*/
289+
public DbxRequest<java.lang.Void> testRouteV2Request(String name, Date born) {
290+
return () -> { testRouteV2(name, born); return null; };
291+
}
292+
228293
//
229294
// route 2/test/test_upload
230295
//
@@ -258,6 +323,19 @@ public TestUploadUploader testUpload(UninitializedReason reason, String sessionI
258323
return testUpload(_arg);
259324
}
260325

326+
/**
327+
* See {@link DbxTestTestRequests#testUpload(UninitializedReason,String)}.
328+
*
329+
* @param reason Must not be {@code null}.
330+
* @param sessionId Must not be {@code null}.
331+
*
332+
* @return A {@link DbxRequest} that can be executed later to obtain the
333+
* {@code TestUploadUploader}.
334+
*/
335+
public DbxRequest<TestUploadUploader> testUploadRequest(UninitializedReason reason, String sessionId) {
336+
return () -> testUpload(reason, sessionId);
337+
}
338+
261339
//
262340
// route 2/test/test_upload_v2
263341
//
@@ -293,6 +371,21 @@ public TestUploadV2Uploader testUploadV2(String name, String breed) throws DbxEx
293371
return testUploadV2(_arg);
294372
}
295373

374+
/**
375+
* See {@link DbxTestTestRequests#testUploadV2(String,String)}.
376+
*
377+
* @param name Used in {@link
378+
* DbxTestTestRequests#testRouteV2(String,Date)}. Must not be {@code
379+
* null}.
380+
* @param breed Must not be {@code null}.
381+
*
382+
* @return A {@link DbxRequest} that can be executed later to obtain the
383+
* {@code TestUploadV2Uploader}.
384+
*/
385+
public DbxRequest<TestUploadV2Uploader> testUploadV2Request(String name, String breed) {
386+
return () -> testUploadV2(name, breed);
387+
}
388+
296389
/**
297390
*
298391
* @param name Used in {@link
@@ -346,6 +439,21 @@ public TestUploadV3Uploader testUploadV3(String name, String breed) throws DbxEx
346439
return testUploadV3(_arg);
347440
}
348441

442+
/**
443+
* See {@link DbxTestTestRequests#testUploadV3(String,String)}.
444+
*
445+
* @param name Used in {@link
446+
* DbxTestTestRequests#testRouteV2(String,Date)}. Must not be {@code
447+
* null}.
448+
* @param breed Must not be {@code null}.
449+
*
450+
* @return A {@link DbxRequest} that can be executed later to obtain the
451+
* {@code TestUploadV3Uploader}.
452+
*/
453+
public DbxRequest<TestUploadV3Uploader> testUploadV3Request(String name, String breed) {
454+
return () -> testUploadV3(name, breed);
455+
}
456+
349457
/**
350458
*
351459
* @param name Used in {@link

core/build/generated_stone_source/test/src/com/dropbox/core/stone/test/DbxTestTestUploadV3Builder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.dropbox.core.stone.test;
55

66
import com.dropbox.core.DbxException;
7+
import com.dropbox.core.DbxRequest;
78
import com.dropbox.core.util.LangUtil;
89
import com.dropbox.core.v2.DbxUploadStyleBuilder;
910

@@ -65,4 +66,14 @@ public TestUploadV3Uploader start() throws ParentUnionException, DbxException {
6566
Dog arg_ = this._builder.build();
6667
return _client.testUploadV3(arg_);
6768
}
69+
70+
/**
71+
* See {@link DbxTestTestRequests#testUploadV3(String,String)}.
72+
*
73+
* @return A {@link DbxRequest} that can be executed later to obtain the
74+
* {@code TestUploadV3Uploader}.
75+
*/
76+
public DbxRequest<TestUploadV3Uploader> startRequest() {
77+
return () -> start();
78+
}
6879
}

core/build/generated_stone_source/test/src/com/dropbox/core/stone/test/TestDownloadBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.dropbox.core.DbxApiException;
77
import com.dropbox.core.DbxDownloader;
88
import com.dropbox.core.DbxException;
9+
import com.dropbox.core.DbxRequest;
910
import com.dropbox.core.util.LangUtil;
1011
import com.dropbox.core.v2.DbxDownloadStyleBuilder;
1112

@@ -67,4 +68,14 @@ public DbxDownloader<Fish> start() throws DbxApiException, DbxException {
6768
Dog arg_ = this._builder.build();
6869
return _client.testDownload(arg_, getHeaders());
6970
}
71+
72+
/**
73+
* See {@link DbxTestTestRequests#testDownload(String,String)}.
74+
*
75+
* @return A {@link DbxRequest} that can be executed later to obtain the
76+
* {@code DbxDownloader&lt;Fish&gt;}.
77+
*/
78+
public DbxRequest<DbxDownloader<Fish>> startRequest() {
79+
return () -> start();
80+
}
7081
}

core/build/generated_stone_source/test/src/com/dropbox/core/stone/test/TestDownloadV2Builder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.dropbox.core.DbxDownloader;
77
import com.dropbox.core.DbxException;
8+
import com.dropbox.core.DbxRequest;
89
import com.dropbox.core.v2.DbxDownloadStyleBuilder;
910

1011
/**
@@ -46,4 +47,15 @@ public DbxDownloader<Fish> start() throws ParentUnionException, DbxException {
4647
Uninitialized arg_ = new Uninitialized(reason, sessionId);
4748
return _client.testDownloadV2(arg_, getHeaders());
4849
}
50+
51+
/**
52+
* See {@link
53+
* DbxTestTestRequests#testDownloadV2(UninitializedReason,String)}.
54+
*
55+
* @return A {@link DbxRequest} that can be executed later to obtain the
56+
* {@code DbxDownloader&lt;Fish&gt;}.
57+
*/
58+
public DbxRequest<DbxDownloader<Fish>> startRequest() {
59+
return () -> start();
60+
}
4961
}

core/build/generated_stone_source/test/src/com/dropbox/core/stone/test/TestUploadV2Builder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.dropbox.core.stone.test;
55

66
import com.dropbox.core.DbxException;
7+
import com.dropbox.core.DbxRequest;
78
import com.dropbox.core.util.LangUtil;
89
import com.dropbox.core.v2.DbxUploadStyleBuilder;
910

@@ -65,4 +66,14 @@ public TestUploadV2Uploader start() throws ParentUnionException, DbxException {
6566
Dog arg_ = this._builder.build();
6667
return _client.testUploadV2(arg_);
6768
}
69+
70+
/**
71+
* See {@link DbxTestTestRequests#testUploadV2(String,String)}.
72+
*
73+
* @return A {@link DbxRequest} that can be executed later to obtain the
74+
* {@code TestUploadV2Uploader}.
75+
*/
76+
public DbxRequest<TestUploadV2Uploader> startRequest() {
77+
return () -> start();
78+
}
6879
}

core/generator/java/java.stoneg.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,8 @@ def __lt__(self, other):
621621
'exist.')
622622
_CMDLINE_PARSER.add_argument('--unused-classes-to-generate', default=None, help='Specify types ' +
623623
'that we want to generate regardless of whether they are used.')
624+
_CMDLINE_PARSER.add_argument('--generate-request', action="store_true", default=False,
625+
help='Generate additional *Request methods that return DbxRequest<T>.')
624626

625627

626628
class JavaCodeGenerator(CodeBackend):
@@ -729,6 +731,8 @@ def add_imports_for_namespace(self, namespace):
729731
'java.util.HashMap',
730732
'java.util.Map',
731733
)
734+
if self._j._args.generate_request:
735+
self.add_imports('com.dropbox.core.DbxRequest')
732736
for route in namespace.routes:
733737
self.add_imports_for_route(route)
734738

@@ -795,6 +799,9 @@ def add_imports_for_route_builder(self, route):
795799
elif j.request_style(route) == 'upload':
796800
self.add_imports('com.dropbox.core.v2.DbxUploadStyleBuilder')
797801

802+
if self._j._args.generate_request:
803+
self.add_imports('com.dropbox.core.DbxRequest')
804+
798805
def add_imports_for_route_uploader(self, route):
799806
self.add_imports(
800807
'com.dropbox.core.DbxWrappedException',
@@ -2800,6 +2807,12 @@ def generate_route_base(self, route, force_public=False):
28002807
else:
28012808
assert False, "unrecognized route request style: %s" % j.request_style(route)
28022809

2810+
if is_public and self.g.args.generate_request:
2811+
request_args = w.fmt('%s arg', j.java_class(route.arg_data_type)) if j.has_arg(route) else ''
2812+
request_arg_names = 'arg' if j.has_arg(route) else ''
2813+
self._emit_request_method_body(route, request_args, request_arg_names, return_class,
2814+
params=params)
2815+
28032816
def generate_route(self, route, required_only=True):
28042817
assert isinstance(route, ApiRoute), repr(route)
28052818

@@ -2914,6 +2927,15 @@ def generate_route(self, route, required_only=True):
29142927
else:
29152928
w.out('%s(_arg);', j.route_method(route))
29162929

2930+
if self.g.args.generate_request:
2931+
request_args = ', '.join(
2932+
w.fmt('%s %s', j.java_class(f), j.param_name(f)) for f in fields
2933+
)
2934+
request_arg_names = ', '.join(j.param_name(f) for f in fields)
2935+
request_params = w._javadoc_fields(fields, route, allow_defaults=False)
2936+
self._emit_request_method_body(route, request_args, request_arg_names, return_class,
2937+
params=request_params)
2938+
29172939
def generate_route_builder_method(self, route):
29182940
assert isinstance(route, ApiRoute), repr(route)
29192941

@@ -2953,6 +2975,40 @@ def generate_route_builder_method(self, route):
29532975
else:
29542976
w.out('return new %s(this, %s);', return_class, builder_args)
29552977

2978+
def _emit_request_method_body(self, route, args, arg_names, return_class,
2979+
fields=(), params=(), method_name=None, delegate=None):
2980+
"""Emit the request method signature and body."""
2981+
w = self.w
2982+
j = self.j
2983+
2984+
if return_class == JavaClass('void'):
2985+
boxed_return = JavaClass('java.lang.Void')
2986+
else:
2987+
boxed_return = return_class
2988+
2989+
request_class = JavaClass('com.dropbox.core.DbxRequest', generics=(boxed_return,))
2990+
if method_name is None:
2991+
method_name = j.route_method(route) + 'Request'
2992+
if delegate is None:
2993+
delegate = '%s(%s)' % (j.route_method(route), arg_names)
2994+
2995+
w.out('')
2996+
dbx_request_ref = w.javadoc_ref(JavaClass('com.dropbox.core.DbxRequest'))
2997+
if return_class == JavaClass('void'):
2998+
returns_doc = "A %s that can be executed later." % dbx_request_ref
2999+
else:
3000+
result_type = w.resolved_class(boxed_return, generics=True)
3001+
returns_doc = "A %s that can be executed later to obtain the {@code %s}." % (
3002+
dbx_request_ref, result_type,
3003+
)
3004+
doc = "See %s." % w.javadoc_ref(route)
3005+
w.javadoc(doc, params=params, returns=returns_doc)
3006+
with w.block('public %s %s(%s)', request_class, method_name, args):
3007+
if return_class == JavaClass('void'):
3008+
w.out('return () -> { %s; return null; };', delegate)
3009+
else:
3010+
w.out('return () -> %s;', delegate)
3011+
29563012
def translate_error_wrapper(self, route, error_wrapper_var):
29573013
assert isinstance(route, ApiRoute), repr(route)
29583014
assert isinstance(error_wrapper_var, str), repr(error_wrapper_var)
@@ -3848,6 +3904,10 @@ def generate_route_builder(self, route):
38483904
else:
38493905
w.out('_client.%s(%s);', j.route_method(route), ', '.join(args))
38503906

3907+
if self.g.args.generate_request:
3908+
self._emit_request_method_body(route, '', '', return_class,
3909+
method_name='startRequest', delegate='start()')
3910+
38513911
def generate_field_assignment(self, field, lhs=None, rhs=None, allow_default=True):
38523912
assert isinstance(field, Field), repr(field)
38533913

0 commit comments

Comments
 (0)