Skip to content

Commit eb1510f

Browse files
authored
#448 Adding field filter option to get client by ID API (#449)
* Adding field filter option to get client by ID API * Adding field filter option to get client by ID API
1 parent 5fdf16a commit eb1510f

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/main/java/com/auth0/client/mgmt/ClientsEntity.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.auth0.client.mgmt;
22

33
import com.auth0.client.mgmt.filter.ClientFilter;
4+
import com.auth0.client.mgmt.filter.FieldsFilter;
45
import com.auth0.json.mgmt.client.Client;
56
import com.auth0.json.mgmt.client.ClientsPage;
67
import com.auth0.net.CustomRequest;
@@ -95,6 +96,33 @@ public Request<Client> get(String clientId) {
9596
return request;
9697
}
9798

99+
/**
100+
* 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.
101+
* See https://auth0.com/docs/api/management/v2#!/Clients/get_clients_by_id
102+
*
103+
* @param clientId the application's client id.
104+
* @param filter optional filter to restrict fields (to be included/excluded in response)
105+
* @return a Request to execute.
106+
*/
107+
public Request<Client> get(String clientId, FieldsFilter filter) {
108+
Asserts.assertNotNull(clientId, "client id");
109+
110+
HttpUrl.Builder builder = baseUrl
111+
.newBuilder()
112+
.addPathSegments("api/v2/clients")
113+
.addPathSegment(clientId);
114+
if (filter != null) {
115+
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
116+
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
117+
}
118+
}
119+
String url = builder.build().toString();
120+
CustomRequest<Client> request = new CustomRequest<>(client, url, "GET", new TypeReference<Client>() {
121+
});
122+
request.addHeader("Authorization", "Bearer " + apiToken);
123+
return request;
124+
}
125+
98126
/**
99127
* Create a new Application. A token with scope create:clients is needed.
100128
* See https://auth0.com/docs/api/management/v2#!/Clients/post_clients

src/test/java/com/auth0/client/mgmt/ClientsEntityTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.auth0.client.mgmt;
22

33
import com.auth0.client.mgmt.filter.ClientFilter;
4+
import com.auth0.client.mgmt.filter.FieldsFilter;
45
import com.auth0.json.mgmt.client.Client;
56
import com.auth0.json.mgmt.client.ClientsPage;
67
import com.auth0.net.Request;
@@ -175,6 +176,43 @@ public void shouldGetClient() throws Exception {
175176
assertThat(response, is(notNullValue()));
176177
}
177178

179+
@Test
180+
public void shouldGetClientWithFilter() throws Exception {
181+
FieldsFilter fieldsFilter = new FieldsFilter()
182+
.withFields("name,client_id,app_type,tenant", true);
183+
184+
Request<Client> request = api.clients().get("1", fieldsFilter);
185+
assertThat(request, is(notNullValue()));
186+
187+
server.jsonResponse(MGMT_CLIENT, 200);
188+
Client response = request.execute();
189+
RecordedRequest recordedRequest = server.takeRequest();
190+
191+
assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/clients/1"));
192+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
193+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
194+
assertThat(recordedRequest, hasQueryParameter("fields", "name,client_id,app_type,tenant"));
195+
assertThat(recordedRequest, hasQueryParameter("include_fields", "true"));
196+
197+
assertThat(response, is(notNullValue()));
198+
}
199+
200+
@Test
201+
public void shouldGetClientWithNullFilter() throws Exception {
202+
Request<Client> request = api.clients().get("1", null);
203+
assertThat(request, is(notNullValue()));
204+
205+
server.jsonResponse(MGMT_CLIENT, 200);
206+
Client response = request.execute();
207+
RecordedRequest recordedRequest = server.takeRequest();
208+
209+
assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/clients/1"));
210+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
211+
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
212+
213+
assertThat(response, is(notNullValue()));
214+
}
215+
178216
@Test
179217
public void shouldThrowOnCreateClientWithNullData() {
180218
exception.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)