|
4 | 4 | import com.fasterxml.jackson.core.JsonProcessingException;
|
5 | 5 | import com.fasterxml.jackson.databind.DeserializationFeature;
|
6 | 6 | import com.fasterxml.jackson.databind.JsonNode;
|
7 |
| -import com.fasterxml.jackson.databind.MappingIterator; |
8 | 7 | import com.fasterxml.jackson.databind.ObjectMapper;
|
9 | 8 | import com.fasterxml.jackson.databind.SerializationFeature;
|
10 | 9 | import com.ning.http.client.AsyncCompletionHandler;
|
|
25 | 24 | import java.io.Closeable;
|
26 | 25 | import java.io.IOException;
|
27 | 26 | import java.util.ArrayList;
|
| 27 | +import java.util.Arrays; |
28 | 28 | import java.util.Collections;
|
| 29 | +import java.util.HashMap; |
29 | 30 | import java.util.Iterator;
|
30 | 31 | import java.util.List;
|
| 32 | +import java.util.Map; |
31 | 33 | import java.util.NoSuchElementException;
|
32 | 34 | import java.util.concurrent.ExecutionException;
|
33 | 35 |
|
@@ -252,8 +254,9 @@ public Attachment.Upload createUpload(String fileName, String contentType, byte[
|
252 | 254 |
|
253 | 255 | public Attachment.Upload createUpload(String token, String fileName, String contentType, byte[] content) {
|
254 | 256 | TemplateUri uri = tmpl("/uploads.json{?filename}{?token}").set("filename", fileName);
|
255 |
| - if (token != null) |
| 257 | + if (token != null) { |
256 | 258 | uri.set("token", token);
|
| 259 | + } |
257 | 260 | return complete(
|
258 | 261 | submit(req("POST", uri, contentType,
|
259 | 262 | content), handle(Attachment.Upload.class, "upload")));
|
@@ -296,19 +299,74 @@ public Iterable<User> getGroupUsers(int id) {
|
296 | 299 | }
|
297 | 300 |
|
298 | 301 | public Iterable<User> getOrganizationUsers(int id) {
|
299 |
| - return new PagedIterable<User>(tmpl("/organization/{id}/users.json").set("id", id), handleList(User.class, "users")); |
| 302 | + return new PagedIterable<User>(tmpl("/organization/{id}/users.json").set("id", id), |
| 303 | + handleList(User.class, "users")); |
300 | 304 | }
|
301 | 305 |
|
302 | 306 | public User getUser(int id) {
|
303 | 307 | return complete(submit(req("GET", tmpl("/users/{id}.json").set("id", id)), handle(User.class, "user")));
|
304 | 308 | }
|
305 | 309 |
|
| 310 | + public User createUser(User user) { |
| 311 | + return complete(submit(req("POST", cnst("/users.json"), JSON, json( |
| 312 | + Collections.singletonMap("user", user))), handle(User.class, "user"))); |
| 313 | + } |
| 314 | + |
| 315 | + public List<User> createUsers(User... users) { |
| 316 | + return createUsers(Arrays.asList(users)); |
| 317 | + } |
| 318 | + |
| 319 | + public List<User> createUsers(List<User> users) { |
| 320 | + return complete(submit(req("POST", cnst("/users/create_many.json"), JSON, json( |
| 321 | + Collections.singletonMap("users", users))), handleList(User.class, "results"))); |
| 322 | + } |
| 323 | + |
| 324 | + public User updateUser(User user) { |
| 325 | + checkHasId(user); |
| 326 | + return complete(submit(req("PUT", tmpl("/users/{id}.json").set("id", user.getId()), JSON, json( |
| 327 | + Collections.singletonMap("user", user))), handle(User.class, "user"))); |
| 328 | + } |
| 329 | + |
| 330 | + public void deleteUser(User user) { |
| 331 | + checkHasId(user); |
| 332 | + deleteUser(user.getId()); |
| 333 | + } |
| 334 | + |
| 335 | + public void deleteUser(int id) { |
| 336 | + complete(submit(req("DELETE", tmpl("/users/{id}.json").set("id", id)), handleStatus())); |
| 337 | + } |
| 338 | + |
306 | 339 | public Iterable<User> lookupUserByEmail(String email) {
|
307 |
| - return new PagedIterable<User>(tmpl("/users/search.json{?query}").set("query", email), handleList(User.class, "users")); |
| 340 | + return new PagedIterable<User>(tmpl("/users/search.json{?query}").set("query", email), |
| 341 | + handleList(User.class, "users")); |
308 | 342 | }
|
309 | 343 |
|
310 | 344 | public Iterable<User> lookupUserByExternalId(String externalId) {
|
311 |
| - return new PagedIterable<User>(tmpl("/users/search.json{?external_id}").set("external_id", externalId), handleList(User.class, "users")); |
| 345 | + return new PagedIterable<User>(tmpl("/users/search.json{?external_id}").set("external_id", externalId), |
| 346 | + handleList(User.class, "users")); |
| 347 | + } |
| 348 | + |
| 349 | + public User getCurrentUser() { |
| 350 | + return complete(submit(req("GET", cnst("/users/me.json")), handle(User.class, "user"))); |
| 351 | + } |
| 352 | + |
| 353 | + public void resetUserPassword(User user, String password) { |
| 354 | + checkHasId(user); |
| 355 | + resetUserPassword(user.getId(), password); |
| 356 | + } |
| 357 | + |
| 358 | + public void resetUserPassword(int id, String password) { |
| 359 | + complete(submit(req("POST", tmpl("/users/{id}/password.json").set("id", id), JSON, |
| 360 | + json(Collections.singletonMap("password", password))), handleStatus())); |
| 361 | + } |
| 362 | + |
| 363 | + public void changeUserPassword(User user, String oldPassword, String newPassword) { |
| 364 | + checkHasId(user); |
| 365 | + Map<String,String> req = new HashMap<String,String>(); |
| 366 | + req.put("previous_password", oldPassword); |
| 367 | + req.put("password", newPassword); |
| 368 | + complete(submit(req("PUT", tmpl("/users/{id}/password.json").set("id", user.getId()), JSON, |
| 369 | + json(req)), handleStatus())); |
312 | 370 | }
|
313 | 371 |
|
314 | 372 | //////////////////////////////////////////////////////////////////////
|
@@ -502,6 +560,12 @@ private static void checkHasId(Attachment attachment) {
|
502 | 560 | }
|
503 | 561 | }
|
504 | 562 |
|
| 563 | + private static void checkHasId(User user) { |
| 564 | + if (user.getId() == null) { |
| 565 | + throw new IllegalArgumentException("User requires id"); |
| 566 | + } |
| 567 | + } |
| 568 | + |
505 | 569 | private static void checkHasToken(Attachment.Upload upload) {
|
506 | 570 | if (upload.getToken() == null) {
|
507 | 571 | throw new IllegalArgumentException("Upload requires token");
|
|
0 commit comments