diff --git a/README.md b/README.md index 64cab1c..820d201 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ making HTTP Rest calls to the Wire backend. Current backend API targeted version is v6, set host and version with environmental variable "WIRE_API_HOST". -Not all the APIs available are implemented, but Helium should help with the most common task of an SDK to send/receive messages and manage the users' data. +Not all the APIs available are implemented, but Helium should help with the most common task of an SDK to send/receive messages and manage the user's +data. This API is targeted towards clients or anything that logs in as a User. For a service/bot/server approach take a look at Lithium (same API contract, but using other endpoints designed for services). @@ -27,8 +28,8 @@ This API is targeted towards clients or anything that logs in as a User. For a s ``` -Create a `LoginClient`, passing a Java WS RS http client previously created, then obtain the users' token -with the login call. Then, create a `API` instance with the same http-client and the users' token. +Create a `LoginClient`, passing a JAX-RS (e.g. Jersey Client) http client previously created, then obtain the user's token +with the login call. Then, create a `API` instance with the same http-client and the user's token. ## How to build the project diff --git a/src/main/java/com/wire/helium/API.java b/src/main/java/com/wire/helium/API.java index 89ec6a1..b0ef0d0 100644 --- a/src/main/java/com/wire/helium/API.java +++ b/src/main/java/com/wire/helium/API.java @@ -432,7 +432,7 @@ public Conversation createConversation(String name, UUID teamId, List= 400) { String msgError = response.readEntity(String.class); - Logger.error("AddService http error: %s, status: %d", msgError, response.getStatus()); + Logger.error("CreateConversation http error: %s, status: %d", msgError, response.getStatus()); throw new HttpException(msgError, response.getStatus()); } @@ -489,7 +489,9 @@ public void leaveConversation(QualifiedId user) throws HttpException { .delete(); if (response.getStatus() >= 400) { - throw new HttpException(response.readEntity(String.class), response.getStatus()); + String msgError = response.readEntity(String.class); + Logger.error("LeaveConversation http error: %s, status: %d", msgError, response.getStatus()); + throw new HttpException(msgError, response.getStatus()); } } @@ -504,13 +506,21 @@ public void uploadPreKeys(ArrayList preKeys) { @Override public ArrayList getAvailablePrekeys(String clientId) { - return clientsPath. + Response response = clientsPath. path(clientId). path("prekeys"). request(). header(HttpHeaders.AUTHORIZATION, bearer(token)). accept(MediaType.APPLICATION_JSON). - get(new GenericType<>() {}); + get(); + + if (response.getStatus() >= 400) { + String msgError = response.readEntity(String.class); + Logger.error("GetAvailablePrekeys http error: %s, status: %d", msgError, response.getStatus()); + throw new RuntimeException(msgError); + } + + return response.readEntity(new GenericType<>() {}); } /** @@ -523,12 +533,25 @@ public Collection getUsers(Collection ids) { } /** - * Unused in base api, only needed for bot specific purposes which already extend this API. - * @param userId user id + * Get the metadata of a specific user based on its id. + * @param userId qualified user id */ @Override public User getUser(QualifiedId userId) throws HttpException { - throw new UnsupportedOperationException("Bot specific feature, use a more specific API implementation"); + Response response = usersPath + .path(userId.domain) + .path(userId.id.toString()) + .request() + .header(HttpHeaders.AUTHORIZATION, bearer(token)) + .accept(MediaType.APPLICATION_JSON) + .get(); + + if (response.getStatus() >= 400) { + String msgError = response.readEntity(String.class); + Logger.error("GetUser http error: %s, status: %d", msgError, response.getStatus()); + throw new RuntimeException(msgError); + } + return response.readEntity(User.class); } public NotificationList retrieveNotifications(String client, UUID since, int size) throws HttpException { @@ -556,7 +579,7 @@ public NotificationList retrieveNotifications(String client, UUID since, int siz emptyNotifications.hasMore = false; emptyNotifications.notifications = new ArrayList<>(); return emptyNotifications; - } else if (status == 401) { //todo nginx returns text/html for 401. Cannot deserialize as json + } else if (status == 401) { // Nginx returns text/html for 401. Cannot deserialize as json response.readEntity(String.class); throw new AuthException(status); } else if (status == 403) { @@ -582,10 +605,17 @@ public boolean hasDevice(QualifiedId userId, String clientId) { @Override public User getSelf() { - return selfPath. + Response response = selfPath. request(MediaType.APPLICATION_JSON). header(HttpHeaders.AUTHORIZATION, bearer(token)). - get(User.class); + get(); + + if (response.getStatus() >= 400) { + String msgError = response.readEntity(String.class); + Logger.error("GetSelf http error: %s, status: %d", msgError, response.getStatus()); + throw new RuntimeException(msgError); + } + return response.readEntity(User.class); } @JsonIgnoreProperties(ignoreUnknown = true) diff --git a/src/main/java/com/wire/helium/LoginClient.java b/src/main/java/com/wire/helium/LoginClient.java index 47b074c..5efce9f 100644 --- a/src/main/java/com/wire/helium/LoginClient.java +++ b/src/main/java/com/wire/helium/LoginClient.java @@ -105,7 +105,7 @@ public Access login(String email, String password, boolean persisted) throws Htt int status = response.getStatus(); - if (status == 401) { //todo nginx returns text/html for 401. Cannot deserialize as json + if (status == 401) { // Nginx returns text/html for 401. Cannot deserialize as json response.readEntity(String.class); throw new AuthException(status); } @@ -175,7 +175,7 @@ public String registerClient(String token, String password, ArrayList pr int status = response.getStatus(); - if (status == 401) { //todo nginx returns text/html for 401. Cannot deserialize as json + if (status == 401) { // Nginx returns text/html for 401. Cannot deserialize as json response.readEntity(String.class); throw new AuthException(status); } else if (status >= 400) { @@ -195,7 +195,7 @@ public Access renewAccessToken(Cookie cookie) throws HttpException { int status = response.getStatus(); - if (status == 401) { //todo nginx returns text/html for 401. Cannot deserialize as json + if (status == 401) { // Nginx returns text/html for 401. Cannot deserialize as json response.readEntity(String.class); throw new AuthException(status); } else if (status == 403) { @@ -225,7 +225,7 @@ public void logout(Cookie cookie, String token) throws HttpException { .post(Entity.entity(null, MediaType.APPLICATION_JSON)); int status = response.getStatus(); - if (status == 401) { //todo nginx returns text/html for 401. Cannot deserialize as json + if (status == 401) { // Nginx returns text/html for 401. Cannot deserialize as json response.readEntity(String.class); throw new AuthException(status); } else if (status == 403) { @@ -248,7 +248,7 @@ public void removeCookies(String token, String password) throws HttpException { int status = response.getStatus(); - if (status == 401) { //todo nginx returns text/html for 401. Cannot deserialize as json + if (status == 401) { // Nginx returns text/html for 401. Cannot deserialize as json response.readEntity(String.class); throw new AuthException(status); } else if (status >= 400) {