Skip to content

Commit a877b0c

Browse files
committed
Add missing user API calls
1 parent f0a38b7 commit a877b0c

File tree

2 files changed

+157
-7
lines changed

2 files changed

+157
-7
lines changed

src/main/java/org/zendesk/client/v2/ZenDesk.java

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.databind.DeserializationFeature;
66
import com.fasterxml.jackson.databind.JsonNode;
7-
import com.fasterxml.jackson.databind.MappingIterator;
87
import com.fasterxml.jackson.databind.ObjectMapper;
98
import com.fasterxml.jackson.databind.SerializationFeature;
109
import com.ning.http.client.AsyncCompletionHandler;
@@ -25,9 +24,12 @@
2524
import java.io.Closeable;
2625
import java.io.IOException;
2726
import java.util.ArrayList;
27+
import java.util.Arrays;
2828
import java.util.Collections;
29+
import java.util.HashMap;
2930
import java.util.Iterator;
3031
import java.util.List;
32+
import java.util.Map;
3133
import java.util.NoSuchElementException;
3234
import java.util.concurrent.ExecutionException;
3335

@@ -252,8 +254,9 @@ public Attachment.Upload createUpload(String fileName, String contentType, byte[
252254

253255
public Attachment.Upload createUpload(String token, String fileName, String contentType, byte[] content) {
254256
TemplateUri uri = tmpl("/uploads.json{?filename}{?token}").set("filename", fileName);
255-
if (token != null)
257+
if (token != null) {
256258
uri.set("token", token);
259+
}
257260
return complete(
258261
submit(req("POST", uri, contentType,
259262
content), handle(Attachment.Upload.class, "upload")));
@@ -296,19 +299,74 @@ public Iterable<User> getGroupUsers(int id) {
296299
}
297300

298301
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"));
300304
}
301305

302306
public User getUser(int id) {
303307
return complete(submit(req("GET", tmpl("/users/{id}.json").set("id", id)), handle(User.class, "user")));
304308
}
305309

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+
306339
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"));
308342
}
309343

310344
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()));
312370
}
313371

314372
//////////////////////////////////////////////////////////////////////
@@ -502,6 +560,12 @@ private static void checkHasId(Attachment attachment) {
502560
}
503561
}
504562

563+
private static void checkHasId(User user) {
564+
if (user.getId() == null) {
565+
throw new IllegalArgumentException("User requires id");
566+
}
567+
}
568+
505569
private static void checkHasToken(Attachment.Upload upload) {
506570
if (upload.getToken() == null) {
507571
throw new IllegalArgumentException("Upload requires token");

src/main/java/org/zendesk/client/v2/model/User.java

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

33
import com.fasterxml.jackson.annotation.JsonProperty;
44

5+
import java.util.ArrayList;
6+
import java.util.Arrays;
57
import java.util.Date;
68
import java.util.List;
79

@@ -37,6 +39,52 @@ public class User {
3739
private List<String> tags;
3840
private Boolean suspended;
3941
private Attachment photo;
42+
private List<Identity> identities;
43+
private String remotePhotoUrl;
44+
45+
public User() {
46+
}
47+
48+
public User(Boolean verified, String name, String email) {
49+
this.name = name;
50+
this.email = email;
51+
this.verified = verified;
52+
}
53+
54+
public User(Boolean verified, String name, List<Identity> identities) {
55+
this.verified = verified;
56+
this.name = name;
57+
this.identities = identities;
58+
}
59+
60+
public User(Boolean verified, String name, Identity... identities) {
61+
this.verified = verified;
62+
this.name = name;
63+
this.identities = new ArrayList<Identity>(Arrays.asList(identities));
64+
}
65+
66+
public User(String name, String email) {
67+
this.name = name;
68+
this.email = email;
69+
}
70+
71+
public User(String name, List<Identity> identities) {
72+
this.name = name;
73+
this.identities = identities;
74+
}
75+
76+
public User(String name, Identity... identities) {
77+
this.name = name;
78+
this.identities = new ArrayList<Identity>(Arrays.asList(identities));
79+
}
80+
81+
public List<Identity> getIdentities() {
82+
return identities;
83+
}
84+
85+
public void setIdentities(List<Identity> identities) {
86+
this.identities = identities;
87+
}
4088

4189
public Boolean getActive() {
4290
return active;
@@ -264,7 +312,16 @@ public void setVerified(Boolean verified) {
264312
this.verified = verified;
265313
}
266314

267-
private enum Role {
315+
@JsonProperty("remote_photo_url")
316+
public String getRemotePhotoUrl() {
317+
return remotePhotoUrl;
318+
}
319+
320+
public void setRemotePhotoUrl(String remotePhotoUrl) {
321+
this.remotePhotoUrl = remotePhotoUrl;
322+
}
323+
324+
public static enum Role {
268325
END_USER("end-user"),
269326
AGENT("agent"),
270327
ADMIN("admin");
@@ -281,7 +338,7 @@ public String toString() {
281338
}
282339
}
283340

284-
private enum TicketRestriction {
341+
public static enum TicketRestriction {
285342
ORGANIZATION,
286343
GROUPS,
287344
ASSIGNED,
@@ -292,4 +349,33 @@ public String toString() {
292349
return name().toLowerCase();
293350
}
294351
}
352+
353+
public static class Identity {
354+
private String type;
355+
private String value;
356+
357+
public Identity() {
358+
}
359+
360+
public Identity(String type, String value) {
361+
this.type = type;
362+
this.value = value;
363+
}
364+
365+
public String getType() {
366+
return type;
367+
}
368+
369+
public void setType(String type) {
370+
this.type = type;
371+
}
372+
373+
public String getValue() {
374+
return value;
375+
}
376+
377+
public void setValue(String value) {
378+
this.value = value;
379+
}
380+
}
295381
}

0 commit comments

Comments
 (0)