Skip to content

Commit

Permalink
Add back getUser API call
Browse files Browse the repository at this point in the history
* Fix typos and remove TODOs
  • Loading branch information
spoonman01 committed Sep 26, 2024
1 parent 1f362b2 commit 1596758
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -27,8 +28,8 @@ This API is targeted towards clients or anything that logs in as a User. For a s
</dependencies>
```

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

Expand Down
50 changes: 40 additions & 10 deletions src/main/java/com/wire/helium/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public Conversation createConversation(String name, UUID teamId, List<QualifiedI

if (response.getStatus() >= 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());
}

Expand Down Expand Up @@ -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());
}
}

Expand All @@ -504,13 +506,21 @@ public void uploadPreKeys(ArrayList<PreKey> preKeys) {

@Override
public ArrayList<Integer> 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<>() {});
}

/**
Expand All @@ -523,12 +533,25 @@ public Collection<User> getUsers(Collection<QualifiedId> 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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/wire/helium/LoginClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -175,7 +175,7 @@ public String registerClient(String token, String password, ArrayList<PreKey> 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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 1596758

Please sign in to comment.