Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pagination on the Clients entity #124

Merged
merged 3 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/com/auth0/client/mgmt/ClientsEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.auth0.client.mgmt;

import com.auth0.client.mgmt.filter.ClientFilter;
import com.auth0.json.mgmt.client.Client;
import com.auth0.json.mgmt.client.ClientsPage;
import com.auth0.net.CustomRequest;
import com.auth0.net.EmptyBodyRequest;
import com.auth0.net.Request;
Expand All @@ -11,6 +13,7 @@
import okhttp3.OkHttpClient;

import java.util.List;
import java.util.Map;

/**
* Class that provides an implementation of the Application methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Clients
Expand Down Expand Up @@ -40,6 +43,29 @@ public Request<List<Client>> list() {
return request;
}

/**
* Request all the Applications. A token with scope read:clients is needed. If you also need the client_secret and encryption_key attributes the token must have read:client_keys scope.
* See https://auth0.com/docs/api/management/v2#!/Clients/get_clients
*
* @param filter the filter to use. Can be null.
* @return a Request to execute.
*/
public Request<ClientsPage> list(ClientFilter filter) {
HttpUrl.Builder builder = baseUrl
.newBuilder()
.addPathSegments("api/v2/clients");
if (filter != null) {
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
}
}
String url = builder.build().toString();
CustomRequest<ClientsPage> request = new CustomRequest<>(client, url, "GET", new TypeReference<ClientsPage>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
return request;
}

/**
* Request an Application. A token with scope read:clients is needed. If you also need the client_secret and encryption_key attributes the token must have read:client_keys scope.
* See https://auth0.com/docs/api/management/v2#!/Clients/get_clients_by_id
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/auth0/client/mgmt/filter/ClientFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.auth0.client.mgmt.filter;

/**
* Class used to filter the results received when calling the Clients endpoint. Related to the {@link com.auth0.client.mgmt.ClientsEntity} entity.
*/
public class ClientFilter extends FieldsFilter {

/**
* Include the query summary
*
* @param includeTotals whether to include or not the query summary.
* @return this filter instance
*/
public ClientFilter withTotals(boolean includeTotals) {
parameters.put("include_totals", includeTotals);
return this;
}

/**
* Filter by page
*
* @param pageNumber the page number to retrieve.
* @param amountPerPage the amount of items per page to retrieve.
* @return this filter instance
*/
public ClientFilter withPage(int pageNumber, int amountPerPage) {
parameters.put("page", pageNumber);
parameters.put("per_page", amountPerPage);
return this;
}

/**
* Filter by global clients
*
* @param isGlobal whether the client should or not be global
* @return this filter instance
*/
public ClientFilter withIsGlobal(boolean isGlobal) {
parameters.put("is_global", isGlobal);
return this;
}

/**
* Filter by first party clients
*
* @param isFirstParty whether the client should or not be first party
* @return this filter instance
*/
public ClientFilter withIsFirstParty(boolean isFirstParty) {
parameters.put("is_first_party", isFirstParty);
return this;
}

/**
* Filter by application type
*
* @param appType A comma separated list of application types used to filter the returned clients (native, spa, regular_web, non_interactive)
* @return this filter instance
*/
public ClientFilter withAppType(String appType) {
parameters.put("app_type", appType);
return this;
}

@Override
public ClientFilter withFields(String fields, boolean includeFields) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of this logical? If you pick fields surely you would want to include them? Otherwise why include them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cocojoe a Client has a huge number of properties. The endpoint accepts a fields value so you can filter them. When you pass a list of fields, you can also say whether you want those fields included (whitelist) or excluded (blacklist) from the response.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, that makes more sense, I miss interpreted it 👍

super.withFields(fields, includeFields);
return this;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/auth0/json/mgmt/PageDeserializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.auth0.json.mgmt;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.IOException;
import java.util.List;

public abstract class PageDeserializer<T, U> extends StdDeserializer<T> {

private String itemsPropertyName;
private Class<U> uClazz;

protected PageDeserializer(Class<U> clazz, String arrayName) {
super(Object.class);
this.uClazz = clazz;
this.itemsPropertyName = arrayName;
}

@Override
public T deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
JsonNode node = p.getCodec().readTree(p);
ObjectMapper mapper = new ObjectMapper();
if (node.isArray()) {
return createPage(getArrayElements((ArrayNode) node, mapper));
}

Integer start = getIntegerValue(node.get("start"));
Integer length = getIntegerValue(node.get("length"));
Integer total = getIntegerValue(node.get("total"));
Integer limit = getIntegerValue(node.get("limit"));
ArrayNode array = (ArrayNode) node.get(itemsPropertyName);

return createPage(start, length, total, limit, getArrayElements(array, mapper));
}

protected abstract T createPage(List<U> items);

protected abstract T createPage(Integer start, Integer length, Integer total, Integer limit, List<U> items);

private Integer getIntegerValue(JsonNode node) {
if (node == null || node.isNull()) {
return null;
} else {
return node.intValue();
}
}

private List<U> getArrayElements(ArrayNode array, ObjectMapper mapper) throws IOException {
CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, uClazz);
return mapper.readerFor(type).readValue(array);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/auth0/json/mgmt/client/ClientsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.auth0.json.mgmt.client;

import com.auth0.json.mgmt.Page;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.util.List;

/**
* Class that represents a given page of Clients. Related to the {@link com.auth0.client.mgmt.ClientsEntity} entity.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonDeserialize(using = ClientsPageDeserializer.class)
public class ClientsPage extends Page<Client> {

public ClientsPage(List<Client> items) {
super(items);
}

public ClientsPage(Integer start, Integer length, Integer total, Integer limit, List<Client> items) {
super(start, length, total, limit, items);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.auth0.json.mgmt.client;

import com.auth0.json.mgmt.PageDeserializer;

import java.util.List;

@SuppressWarnings({"unused", "WeakerAccess"})
class ClientsPageDeserializer extends PageDeserializer<ClientsPage, Client> {

ClientsPageDeserializer() {
super(Client.class, "clients");
}

@Override
protected ClientsPage createPage(List<Client> items) {
return new ClientsPage(items);
}

@Override
protected ClientsPage createPage(Integer start, Integer length, Integer total, Integer limit, List<Client> items) {
return new ClientsPage(start, length, total, limit, items);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

/**
* Class that represents a Page of Auth0 Events objects. Related to the {@link com.auth0.client.mgmt.LogEventsEntity} entity.
* Class that represents a given page of Log Events. Related to the {@link com.auth0.client.mgmt.LogEventsEntity} entity.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,23 @@
package com.auth0.json.mgmt.logevents;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.auth0.json.mgmt.PageDeserializer;

import java.io.IOException;
import java.util.List;

@SuppressWarnings({"unused", "WeakerAccess"})
class LogEventsPageDeserializer extends StdDeserializer<LogEventsPage> {
LogEventsPageDeserializer(JavaType valueType) {
super(valueType);
}
class LogEventsPageDeserializer extends PageDeserializer<LogEventsPage, LogEvent> {

LogEventsPageDeserializer() {
this(null);
protected LogEventsPageDeserializer() {
super(LogEvent.class, "logs");
}

@Override
public LogEventsPage deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
JsonNode node = p.getCodec().readTree(p);
ObjectMapper mapper = new ObjectMapper();
if (node.isArray()) {
return new LogEventsPage(getArrayElements((ArrayNode) node, mapper));
}

Integer start = getIntegerValue(node.get("start"));
Integer length = getIntegerValue(node.get("length"));
Integer total = getIntegerValue(node.get("total"));
Integer limit = getIntegerValue(node.get("limit"));
ArrayNode array = (ArrayNode) node.get("logs");

return new LogEventsPage(start, length, total, limit, getArrayElements(array, mapper));
protected LogEventsPage createPage(List<LogEvent> items) {
return new LogEventsPage(items);
}

private Integer getIntegerValue(JsonNode node) {
if (node == null || node.isNull()) {
return null;
} else {
return node.intValue();
}
}

private List<LogEvent> getArrayElements(ArrayNode array, ObjectMapper mapper) throws IOException {
CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, LogEvent.class);
return mapper.readerFor(type).readValue(array);
@Override
protected LogEventsPage createPage(Integer start, Integer length, Integer total, Integer limit, List<LogEvent> items) {
return new LogEventsPage(start, length, total, limit, items);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/auth0/json/mgmt/users/UsersPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

/**
* Class that represents an Auth0 User object. Related to the {@link com.auth0.client.mgmt.UsersEntity} entity.
* Class that represents a given Ppge of Users. Related to the {@link com.auth0.client.mgmt.UsersEntity} entity.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,24 @@
package com.auth0.json.mgmt.users;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.auth0.json.mgmt.PageDeserializer;

import java.io.IOException;
import java.util.List;

@SuppressWarnings({"unused", "WeakerAccess"})
class UsersPageDeserializer extends StdDeserializer<UsersPage> {
UsersPageDeserializer(JavaType valueType) {
super(valueType);
}
class UsersPageDeserializer extends PageDeserializer<UsersPage, User> {

UsersPageDeserializer() {
this(null);
super(User.class, "users");
}

@Override
public UsersPage deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
JsonNode node = p.getCodec().readTree(p);
ObjectMapper mapper = new ObjectMapper();
if (node.isArray()) {
return new UsersPage(getArrayElements((ArrayNode) node, mapper));
}

Integer start = getIntegerValue(node.get("start"));
Integer length = getIntegerValue(node.get("length"));
Integer total = getIntegerValue(node.get("total"));
Integer limit = getIntegerValue(node.get("limit"));
ArrayNode array = (ArrayNode) node.get("users");

return new UsersPage(start, length, total, limit, getArrayElements(array, mapper));
protected UsersPage createPage(List<User> items) {
return new UsersPage(items);
}

private Integer getIntegerValue(JsonNode node) {
if (node == null || node.isNull()) {
return null;
} else {
return node.intValue();
}
@Override
protected UsersPage createPage(Integer start, Integer length, Integer total, Integer limit, List<User> items) {
return new UsersPage(start, length, total, limit, items);
}

private List<User> getArrayElements(ArrayNode array, ObjectMapper mapper) throws IOException {
CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, User.class);
return mapper.readerFor(type).readValue(array);
}
}
1 change: 1 addition & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class MockServer {
public static final String MGMT_CLIENT_GRANTS_LIST = "src/test/resources/mgmt/client_grants_list.json";
public static final String MGMT_CLIENT_GRANT = "src/test/resources/mgmt/client_grant.json";
public static final String MGMT_CLIENTS_LIST = "src/test/resources/mgmt/clients_list.json";
public static final String MGMT_CLIENTS_PAGED_LIST = "src/test/resources/mgmt/clients_paged_list.json";
public static final String MGMT_CLIENT = "src/test/resources/mgmt/client.json";
public static final String MGMT_CONNECTIONS_LIST = "src/test/resources/mgmt/connections_list.json";
public static final String MGMT_CONNECTION = "src/test/resources/mgmt/connection.json";
Expand Down
Loading