use of com.auth0.net.Request in project auth0-java by auth0.
the class UsersEntity method removePermissions.
/**
* Remove permissions from a user.
* A token with update:users is needed.
* See https://auth0.com/docs/api/management/v2#!/Users/delete_permissions
*
* @param userId the user id
* @param permissions a list of permission objects to remove from the user
* @return a Request to execute
*/
public Request<Void> removePermissions(String userId, List<Permission> permissions) {
Asserts.assertNotNull(userId, "user id");
Asserts.assertNotEmpty(permissions, "permissions");
Map<String, List<Permission>> body = new HashMap<>();
body.put("permissions", permissions);
final String url = baseUrl.newBuilder().addPathSegments("api/v2/users").addPathSegments(userId).addPathSegments("permissions").build().toString();
VoidRequest request = new VoidRequest(this.client, url, "DELETE");
request.setBody(body);
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}
use of com.auth0.net.Request in project auth0-java by auth0.
the class UsersEntity method delete.
/**
* Delete an existing User.
* A token with scope delete:users is needed.
* See https://auth0.com/docs/api/management/v2#!/Users/delete_users_by_id
*
* @param userId the user id
* @return a Request to execute.
*/
public Request<Void> delete(String userId) {
Asserts.assertNotNull(userId, "user id");
String url = baseUrl.newBuilder().addPathSegments("api/v2/users").addPathSegment(userId).build().toString();
VoidRequest request = new VoidRequest(client, url, "DELETE");
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}
use of com.auth0.net.Request in project auth0-java by auth0.
the class BaseRequest method executeAsync.
@Override
public CompletableFuture<T> executeAsync() {
final CompletableFuture<T> future = new CompletableFuture<T>();
okhttp3.Request request;
try {
request = createRequest();
} catch (Auth0Exception e) {
future.completeExceptionally(e);
return future;
}
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new Auth0Exception("Failed to execute request", e));
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try {
T parsedResponse = parseResponse(response);
future.complete(parsedResponse);
} catch (Auth0Exception e) {
future.completeExceptionally(e);
}
}
});
return future;
}
use of com.auth0.net.Request in project auth0-java by auth0.
the class BaseRequestTest method alwaysCloseResponseOnRateLimitException.
@Test
public void alwaysCloseResponseOnRateLimitException() {
Exception exception = null;
try {
new MockBaseRequest<String>(client) {
@Override
protected String parseResponse(Response response) throws Auth0Exception {
throw new RateLimitException(-1, -1, -1);
}
}.execute();
} catch (Exception e) {
exception = e;
}
assertThat(exception, is(notNullValue()));
assertThat(exception, is(instanceOf(RateLimitException.class)));
assertThat(exception.getMessage(), is("Request failed with status code 429: Rate limit reached"));
verify(response, times(1)).close();
}
use of com.auth0.net.Request in project auth0-java by auth0.
the class BaseRequestTest method asyncCompletesWithExceptionWhenResponseParsingFails.
@Test
public void asyncCompletesWithExceptionWhenResponseParsingFails() throws Exception {
doReturn(call).when(client).newCall(any());
doAnswer(invocation -> {
((Callback) invocation.getArgument(0)).onResponse(call, response);
return null;
}).when(call).enqueue(any());
CompletableFuture<?> request = new MockBaseRequest<String>(client) {
@Override
protected String parseResponse(Response response) throws Auth0Exception {
throw new Auth0Exception("Response Parsing Error");
}
}.executeAsync();
Exception exception = null;
Object result = null;
try {
result = request.get();
} catch (Exception e) {
exception = e;
}
assertThat(result, is(nullValue()));
assertThat(exception, is(notNullValue()));
assertThat(exception.getCause(), is(instanceOf(Auth0Exception.class)));
assertThat(exception.getCause().getMessage(), is("Response Parsing Error"));
}
Aggregations