Skip to content

Commit 9f2b530

Browse files
author
Stephen Cobbe
committed
Added userId support to global handlers.
1 parent 1fc5b87 commit 9f2b530

File tree

10 files changed

+140
-37
lines changed

10 files changed

+140
-37
lines changed

examples/global-callbacks/src/main/java/com/dropbox/core/examples/global-callbacks/DbxExampleGlobalCallbackFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class DbxExampleGlobalCallbackFactory implements DbxGlobalCallbackFactory {
1010
@Override
11-
public <T> DbxRouteErrorCallback<T> createRouteErrorCallback(T routeError) {
11+
public <T> DbxRouteErrorCallback<T> createRouteErrorCallback(String userId, T routeError) {
1212
if (routeError instanceof ListFolderError) {
1313
return new DbxExampleListFolderErrorCallback<T>();
1414
} else if (routeError instanceof LookupError) {
@@ -19,7 +19,7 @@ public <T> DbxRouteErrorCallback<T> createRouteErrorCallback(T routeError) {
1919
}
2020

2121
@Override
22-
public DbxExampleNetworkErrorCallback createNetworkErrorCallback() {
22+
public DbxExampleNetworkErrorCallback createNetworkErrorCallback(String userId) {
2323
return new DbxExampleNetworkErrorCallback();
2424
}
2525
}

generator/java.stoneg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,7 +2915,7 @@ def generate_route_upload_call(self, route, arg_var):
29152915
JavaClass('com.dropbox.core.http.HttpRequestor.Uploader')),
29162916
after=';',
29172917
)
2918-
w.out('return new %s(_uploader);', j.route_uploader_class(route))
2918+
w.out('return new %s(_uploader, this.client.getUserId());', j.route_uploader_class(route))
29192919

29202920
def generate_data_type(self, data_type):
29212921
"""Generate a class definition for a datatype (a struct or a union)."""
@@ -3566,8 +3566,8 @@ def generate_route_uploader(self, route):
35663566
params=(('httpUploader', 'Initiated HTTP upload request'),),
35673567
throws=(('NullPointerException', 'if {@code httpUploader} is {@code null}'),)
35683568
)
3569-
with w.block('public %s(HttpRequestor.Uploader httpUploader)', j.route_uploader_class(route)):
3570-
w.out('super(httpUploader, %s, %s);',
3569+
with w.block('public %s(HttpRequestor.Uploader httpUploader, String userId)', j.route_uploader_class(route)):
3570+
w.out('super(httpUploader, %s, %s, userId);',
35713571
w.java_serializer(route.result_data_type),
35723572
w.java_serializer(route.error_data_type))
35733573

src/main/java/com/dropbox/core/DbxRequestUtil.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public static List<HttpRequestor.Header> addAuthHeader(/*@Nullable*/List<HttpReq
104104
if (headers == null) headers = new ArrayList<HttpRequestor.Header>();
105105

106106
headers.add(new HttpRequestor.Header("Authorization", "Bearer " + accessToken));
107+
String pathRootMsg = "lalallala";
108+
headers.add(new HttpRequestor.Header("X-Dropbox-Path-Root", pathRootMsg));
107109
return headers;
108110
}
109111

@@ -280,7 +282,11 @@ public static String parseErrorBody(String requestId, int statusCode, byte[] bod
280282
}
281283
}
282284

283-
public static DbxException unexpectedStatus(HttpRequestor.Response response)
285+
public static DbxException unexpectedStatus(HttpRequestor.Response response) throws NetworkIOException, BadResponseException {
286+
return DbxRequestUtil.unexpectedStatus(response, null);
287+
}
288+
289+
public static DbxException unexpectedStatus(HttpRequestor.Response response, String userId)
284290
throws NetworkIOException, BadResponseException {
285291

286292
String requestId = getRequestId(response);
@@ -364,7 +370,7 @@ public static DbxException unexpectedStatus(HttpRequestor.Response response)
364370

365371
DbxGlobalCallbackFactory factory = DbxRequestUtil.sharedCallbackFactory;
366372
if (factory != null) {
367-
DbxNetworkErrorCallback callback = factory.createNetworkErrorCallback();
373+
DbxNetworkErrorCallback callback = factory.createNetworkErrorCallback(userId);
368374
callback.onNetworkError(networkError);
369375
}
370376

src/main/java/com/dropbox/core/DbxUploader.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ public abstract class DbxUploader<R, E, X extends DbxApiException> implements Cl
5050
private boolean closed;
5151
private boolean finished;
5252

53-
protected DbxUploader(HttpRequestor.Uploader httpUploader, StoneSerializer<R> responseSerializer, StoneSerializer<E> errorSerializer) {
53+
private final String userId;
54+
55+
protected DbxUploader(HttpRequestor.Uploader httpUploader, StoneSerializer<R> responseSerializer, StoneSerializer<E> errorSerializer, String userId) {
5456
this.httpUploader = httpUploader;
5557
this.responseSerializer = responseSerializer;
5658
this.errorSerializer = errorSerializer;
5759

5860
this.closed = false;
5961
this.finished = false;
62+
this.userId = userId;
6063
}
6164

6265
protected abstract X newException(DbxWrappedException error);
@@ -218,7 +221,7 @@ public R finish() throws X, DbxException {
218221
return responseSerializer.deserialize(response.getBody());
219222
}
220223
else if (response.getStatusCode() == 409) {
221-
DbxWrappedException wrapped = DbxWrappedException.fromResponse(errorSerializer, response);
224+
DbxWrappedException wrapped = DbxWrappedException.fromResponse(errorSerializer, response, this.userId);
222225
throw newException(wrapped);
223226
}
224227
else {

src/main/java/com/dropbox/core/DbxWrappedException.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public LocalizedText getUserMessage() {
3939
return userMessage;
4040
}
4141

42-
public static <T> DbxWrappedException fromResponse(StoneSerializer<T> errSerializer, HttpRequestor.Response response)
42+
public static <T> DbxWrappedException fromResponse(StoneSerializer<T> errSerializer, HttpRequestor.Response response, String userId)
4343
throws IOException, JsonParseException {
4444
String requestId = DbxRequestUtil.getRequestId(response);
4545

@@ -49,13 +49,13 @@ public static <T> DbxWrappedException fromResponse(StoneSerializer<T> errSeriali
4949
T routeError = apiResponse.getError();
5050

5151
DbxGlobalCallbackFactory factory = DbxRequestUtil.sharedCallbackFactory;
52-
DbxWrappedException.executeBlockForObject(factory, routeError);
53-
DbxWrappedException.executeOtherBlocks(factory, routeError);
52+
DbxWrappedException.executeBlockForObject(factory, userId, routeError);
53+
DbxWrappedException.executeOtherBlocks(factory, userId, routeError);
5454

5555
return new DbxWrappedException(routeError, requestId, apiResponse.getUserMessage());
5656
}
5757

58-
public static void executeOtherBlocks(DbxGlobalCallbackFactory factory, Object routeError) {
58+
public static void executeOtherBlocks(DbxGlobalCallbackFactory factory, String userId, Object routeError) {
5959
try {
6060
// Recursively looks at union errors and the union's current tag type. If there is a handler
6161
// for the current tag type, it is executed.
@@ -66,7 +66,7 @@ public static void executeOtherBlocks(DbxGlobalCallbackFactory factory, Object r
6666
if (f.getName().equalsIgnoreCase(fName) ) {
6767
f.setAccessible(true);
6868
Object fieldValue = f.get(routeError);
69-
DbxWrappedException.executeBlockForObject(factory, fieldValue);
69+
DbxWrappedException.executeBlockForObject(factory, userId, fieldValue);
7070
break;
7171
}
7272
}
@@ -75,9 +75,9 @@ public static void executeOtherBlocks(DbxGlobalCallbackFactory factory, Object r
7575
}
7676
}
7777

78-
public static <T> void executeBlockForObject(DbxGlobalCallbackFactory factory, T routeError) {
78+
public static <T> void executeBlockForObject(DbxGlobalCallbackFactory factory, String userId, T routeError) {
7979
if (factory != null) {
80-
DbxRouteErrorCallback<T> callback = factory.createRouteErrorCallback(routeError);
80+
DbxRouteErrorCallback<T> callback = factory.createRouteErrorCallback(userId, routeError);
8181
if (callback != null) {
8282
callback.setRouteError(routeError);
8383
callback.run();

src/main/java/com/dropbox/core/v2/DbxAppClientV2.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,23 @@ public DbxAppClientV2(DbxRequestConfig requestConfig, String key, String secret)
4343
* testing)
4444
*/
4545
public DbxAppClientV2(DbxRequestConfig requestConfig, String key, String secret, DbxHost host) {
46-
super(new DbxAppRawClientV2(requestConfig, key, secret, host));
46+
super(new DbxAppRawClientV2(requestConfig, key, secret, host, null));
47+
}
48+
49+
/**
50+
* Same as {@link #DbxAppClientV2(DbxRequestConfig, String, String)} except you can also set the
51+
* hostnames of the Dropbox API servers. This is used in testing. You don't normally need to
52+
* call this.
53+
*
54+
* @param requestConfig Default attributes to use for each request
55+
* @param key Dropbox app key (e.g. consumer key in OAuth)
56+
* @param secret Dropbox app secret (e.g. consumer secret in OAuth)
57+
* @param host Dropbox hosts to send requests to (used for mocking and
58+
* testing)
59+
* @param userId The user ID of the current Dropbox account. Used for multi-Dropbox account use-case.
60+
*/
61+
public DbxAppClientV2(DbxRequestConfig requestConfig, String key, String secret, DbxHost host, String userId) {
62+
super(new DbxAppRawClientV2(requestConfig, key, secret, host, userId));
4763
}
4864

4965
/**
@@ -53,8 +69,8 @@ private static final class DbxAppRawClientV2 extends DbxRawClientV2 {
5369
private final String key;
5470
private final String secret;
5571

56-
private DbxAppRawClientV2(DbxRequestConfig requestConfig, String key, String secret, DbxHost host) {
57-
super(requestConfig, host);
72+
private DbxAppRawClientV2(DbxRequestConfig requestConfig, String key, String secret, DbxHost host, String userId) {
73+
super(requestConfig, host, userId);
5874
this.key = key;
5975
this.secret = secret;
6076
}

src/main/java/com/dropbox/core/v2/DbxClientV2.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,21 @@ public class DbxClientV2 extends DbxClientV2Base {
2828
* acquired through {@link com.dropbox.core.DbxWebAuth}
2929
*/
3030
public DbxClientV2(DbxRequestConfig requestConfig, String accessToken) {
31-
this(requestConfig, accessToken, DbxHost.DEFAULT);
31+
this(requestConfig, accessToken, DbxHost.DEFAULT, null);
32+
}
33+
34+
/**
35+
* Creates a client that uses the given OAuth 2 access token as
36+
* authorization when performing requests against the default Dropbox hosts.
37+
*
38+
* @param requestConfig Default attributes to use for each request
39+
* @param accessToken OAuth 2 access token (that you got from Dropbox) that
40+
* gives your app the ability to make Dropbox API calls. Typically
41+
* acquired through {@link com.dropbox.core.DbxWebAuth}
42+
* @param userId The user ID of the current Dropbox account. Used for multi-Dropbox account use-case.
43+
*/
44+
public DbxClientV2(DbxRequestConfig requestConfig, String accessToken, String userId) {
45+
this(requestConfig, accessToken, DbxHost.DEFAULT, userId);
3246
}
3347

3448
/**
@@ -44,7 +58,24 @@ public DbxClientV2(DbxRequestConfig requestConfig, String accessToken) {
4458
* testing)
4559
*/
4660
public DbxClientV2(DbxRequestConfig requestConfig, String accessToken, DbxHost host) {
47-
super(new DbxUserRawClientV2(requestConfig, accessToken, host));
61+
super(new DbxUserRawClientV2(requestConfig, accessToken, host, null));
62+
}
63+
64+
/**
65+
* Same as {@link #DbxClientV2(DbxRequestConfig, String)} except you can
66+
* also set the hostnames of the Dropbox API servers. This is used in
67+
* testing. You don't normally need to call this.
68+
*
69+
* @param requestConfig Default attributes to use for each request
70+
* @param accessToken OAuth 2 access token (that you got from Dropbox) that
71+
* gives your app the ability to make Dropbox API calls. Typically
72+
* acquired through {@link com.dropbox.core.DbxWebAuth}
73+
* @param host Dropbox hosts to send requests to (used for mocking and
74+
* testing)
75+
* @param userId The user ID of the current Dropbox account. Used for multi-Dropbox account use-case.
76+
*/
77+
public DbxClientV2(DbxRequestConfig requestConfig, String accessToken, DbxHost host, String userId) {
78+
super(new DbxUserRawClientV2(requestConfig, accessToken, host, userId));
4879
}
4980

5081
/**
@@ -64,8 +95,8 @@ public DbxClientV2(DbxRequestConfig requestConfig, String accessToken, DbxHost h
6495
private static final class DbxUserRawClientV2 extends DbxRawClientV2 {
6596
private final String accessToken;
6697

67-
public DbxUserRawClientV2(DbxRequestConfig requestConfig, String accessToken, DbxHost host) {
68-
super(requestConfig, host);
98+
public DbxUserRawClientV2(DbxRequestConfig requestConfig, String accessToken, DbxHost host, String userId) {
99+
super(requestConfig, host, userId);
69100

70101
if (accessToken == null) throw new NullPointerException("accessToken");
71102

src/main/java/com/dropbox/core/v2/DbxRawClientV2.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,22 @@ public abstract class DbxRawClientV2 {
5353
private final DbxRequestConfig requestConfig;
5454
private final DbxHost host;
5555

56+
/* for multiple Dropbox account use-case */
57+
private final String userId;
58+
5659
/**
5760
* @param requestConfig Configuration controlling How requests should be issued to Dropbox
5861
* servers.
5962
* @param host Dropbox server hostnames (primarily for internal use)
63+
* @param userId The user ID of the current Dropbox account. Used for multi-Dropbox account use-case.
6064
*/
61-
protected DbxRawClientV2(DbxRequestConfig requestConfig, DbxHost host) {
65+
protected DbxRawClientV2(DbxRequestConfig requestConfig, DbxHost host, String userId) {
6266
if (requestConfig == null) throw new NullPointerException("requestConfig");
6367
if (host == null) throw new NullPointerException("host");
6468

6569
this.requestConfig = requestConfig;
6670
this.host = host;
71+
this.userId = userId;
6772
}
6873

6974
/**
@@ -95,6 +100,8 @@ public <ArgT,ResT,ErrT> ResT rpcStyle(final String host,
95100
headers.add(new HttpRequestor.Header("Content-Type", "application/json; charset=utf-8"));
96101

97102
return executeRetriable(requestConfig.getMaxRetries(), new RetriableExecution<ResT> () {
103+
private String userIdAnon;
104+
98105
@Override
99106
public ResT execute() throws DbxWrappedException, DbxException {
100107
HttpRequestor.Response response = DbxRequestUtil.startPostRaw(requestConfig, USER_AGENT_ID, host, path, body, headers);
@@ -103,9 +110,9 @@ public ResT execute() throws DbxWrappedException, DbxException {
103110
case 200:
104111
return responseSerializer.deserialize(response.getBody());
105112
case 409:
106-
throw DbxWrappedException.fromResponse(errorSerializer, response);
113+
throw DbxWrappedException.fromResponse(errorSerializer, response, userIdAnon);
107114
default:
108-
throw DbxRequestUtil.unexpectedStatus(response);
115+
throw DbxRequestUtil.unexpectedStatus(response, userIdAnon);
109116
}
110117
} catch (JsonProcessingException ex) {
111118
String requestId = DbxRequestUtil.getRequestId(response);
@@ -114,7 +121,12 @@ public ResT execute() throws DbxWrappedException, DbxException {
114121
throw new NetworkIOException(ex);
115122
}
116123
}
117-
});
124+
125+
private RetriableExecution<ResT> init(String userId){
126+
this.userIdAnon = userId;
127+
return this;
128+
}
129+
}.init(this.userId));
118130
}
119131

120132
public <ArgT,ResT,ErrT> DbxDownloader<ResT> downloadStyle(final String host,
@@ -138,6 +150,8 @@ public <ArgT,ResT,ErrT> DbxDownloader<ResT> downloadStyle(final String host,
138150
final byte[] body = new byte[0];
139151

140152
return executeRetriable(requestConfig.getMaxRetries(), new RetriableExecution<DbxDownloader<ResT>>() {
153+
private String userIdAnon;
154+
141155
@Override
142156
public DbxDownloader<ResT> execute() throws DbxWrappedException, DbxException {
143157
HttpRequestor.Response response = DbxRequestUtil.startPostRaw(requestConfig, USER_AGENT_ID, host, path, body, headers);
@@ -163,17 +177,22 @@ public DbxDownloader<ResT> execute() throws DbxWrappedException, DbxException {
163177
ResT result = responseSerializer.deserialize(resultHeader);
164178
return new DbxDownloader<ResT>(result, response.getBody());
165179
case 409:
166-
throw DbxWrappedException.fromResponse(errorSerializer, response);
180+
throw DbxWrappedException.fromResponse(errorSerializer, response, userIdAnon);
167181
default:
168-
throw DbxRequestUtil.unexpectedStatus(response);
182+
throw DbxRequestUtil.unexpectedStatus(response, userIdAnon);
169183
}
170184
} catch(JsonProcessingException ex) {
171185
throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
172186
} catch (IOException ex) {
173187
throw new NetworkIOException(ex);
174188
}
175189
}
176-
});
190+
191+
private RetriableExecution<DbxDownloader<ResT>> init(String userId){
192+
this.userIdAnon = userId;
193+
return this;
194+
}
195+
}.init(this.userId));
177196
}
178197

179198
private static <T> byte [] writeAsBytes(StoneSerializer<T> serializer, T arg) throws DbxException {
@@ -243,6 +262,15 @@ public DbxHost getHost() {
243262
return host;
244263
}
245264

265+
/**
266+
* Returns the {@code userId} that was passed in to the constructor.
267+
*
268+
* @return The user ID of the current Dropbox account.
269+
*/
270+
public String getUserId() {
271+
return userId;
272+
}
273+
246274
/**
247275
* Retries the execution at most a maximum number of times.
248276
*

0 commit comments

Comments
 (0)