Skip to content

Commit

Permalink
Added: get user profile endpoint for management API
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Feb 20, 2017
1 parent 62568ac commit a0df771
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ users
});
```

### Get User Profile

```java
users
.getProfile("user id")
.start(new BaseCallback<UserProfile, ManagementException>() {
@Override
public void onSuccess(UserProfile payload) {
//Profile
}

@Override
public void onFailure(ManagementException error) {
//Error!
}
});
```

### Update User Metadata

```java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void setUserAgent(String userAgent) {
* <pre>
* {@code
* client.link("{auth0 primary user id}", "{user secondary token}")
* .start(new BaseCallback<List<UserIdentity>>() {
* .start(new BaseCallback<List<UserIdentity>, ManagementException>() {
* {@literal}Override
* public void onSuccess(List<UserIdentity> payload) {}
*
Expand Down Expand Up @@ -179,7 +179,7 @@ public Request<List<UserIdentity>, ManagementException> link(String primaryUserI
* <pre>
* {@code
* client.unlink("{auth0 primary user id}", {auth0 secondary user id}, "{secondary provider}")
* .start(new BaseCallback<List<UserIdentity>>() {
* .start(new BaseCallback<List<UserIdentity>, ManagementException>() {
* {@literal}Override
* public void onSuccess(List<UserIdentity> payload) {}
*
Expand Down Expand Up @@ -212,12 +212,12 @@ public Request<List<UserIdentity>, ManagementException> unlink(String primaryUse
}

/**
* Update the user_metadata calling <a href="https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id">'/api/v2/users/:token'</a> endpoint
* Update the user_metadata calling <a href="https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id">'/api/v2/users/:userId'</a> endpoint
* Example usage:
* <pre>
* {@code
* client.updateMetadata("{user id}", "{user metadata}")
* .start(new BaseCallback<UserProfile>() {
* .start(new BaseCallback<UserProfile, ManagementException>() {
* {@literal}Override
* public void onSuccess(UserProfile payload) {}
*
Expand All @@ -244,4 +244,35 @@ public Request<UserProfile, ManagementException> updateMetadata(String userId, M
.addParameter(USER_METADATA_KEY, userMetadata);
}

/**
* Get the User Profile calling <a href="https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id">'/api/v2/users/:userId'</a> endpoint
* Example usage:
* <pre>
* {@code
* client.getProfile("{user id}")
* .start(new BaseCallback<UserProfile, ManagementException>() {
* {@literal}Override
* public void onSuccess(UserProfile payload) {}
*
* {@literal}Override
* public void onFailure(ManagementException error) {}
* });
* }
* </pre>
*
* @param userId identity of the user
* @return a request to start
*/
@SuppressWarnings("WeakerAccess")
public Request<UserProfile, ManagementException> getProfile(String userId) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(API_PATH)
.addPathSegment(V2_PATH)
.addPathSegment(USERS_PATH)
.addPathSegment(userId)
.build();

return factory.GET(url, client, gson, UserProfile.class, mgmtErrorBuilder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class UsersAPIClientTest {
private static final String METHOD_POST = "POST";
private static final String METHOD_DELETE = "DELETE";
private static final String METHOD_PATCH = "PATCH";
private static final String METHOD_GET = "GET";
private static final String KEY_LINK_WITH = "link_with";
private static final String KEY_USER_METADATA = "user_metadata";

Expand Down Expand Up @@ -360,6 +361,40 @@ public void shouldUpdateUserMetadataSync() throws Exception {
assertThat(result, isA(UserProfile.class));
}


@Test
public void shouldGetUserProfile() throws Exception {
mockAPI.willReturnUserProfile();

final MockManagementCallback<UserProfile> callback = new MockManagementCallback<>();
client.getProfile(USER_ID_PRIMARY)
.start(callback);

final RecordedRequest request = mockAPI.takeRequest();
assertThat(request.getPath(), equalTo("/api/v2/users/" + USER_ID_PRIMARY));

assertThat(request.getHeader(HEADER_AUTHORIZATION), equalTo(BEARER + TOKEN_PRIMARY));
assertThat(request.getMethod(), equalTo(METHOD_GET));

assertThat(callback, hasPayloadOfType(UserProfile.class));
}

@Test
public void shouldGetUserProfileSync() throws Exception {
mockAPI.willReturnUserProfile();

final UserProfile result = client.getProfile(USER_ID_PRIMARY)
.execute();

final RecordedRequest request = mockAPI.takeRequest();
assertThat(request.getPath(), equalTo("/api/v2/users/" + USER_ID_PRIMARY));

assertThat(request.getHeader(HEADER_AUTHORIZATION), equalTo(BEARER + TOKEN_PRIMARY));
assertThat(request.getMethod(), equalTo(METHOD_GET));

assertThat(result, isA(UserProfile.class));
}

private <T> Map<String, T> bodyFromRequest(RecordedRequest request) throws java.io.IOException {
final Type mapType = new TypeToken<Map<String, T>>() {
}.getType();
Expand Down

0 comments on commit a0df771

Please sign in to comment.