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

Expand the Rest API examples to take into account Users in Multiple groups #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions v6/adobesign/api/rest/sample/GetStatusOfAgreements.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private void run() throws Exception {
System.out.println("AgreementName: " + agreement.get("name"));
System.out.println("AgreementID: " + agreement.get("id"));
System.out.println("AgreementStatus: " + agreement.get("status"));
System.out.println("GroupID: " + agreement.get("groupId"));
System.out.println();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import adobesign.api.rest.sample.util.RestApiAgreements;
import adobesign.api.rest.sample.util.RestApiLibraryDocuments;
import adobesign.api.rest.sample.util.RestApiOAuthTokens;
import adobesign.api.rest.sample.util.RestApiUserGroups;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

Expand Down Expand Up @@ -58,14 +59,37 @@ private void run() throws Exception {
// Fetch oauth access token to make further API calls.
String accessToken = RestApiOAuthTokens.getOauthAccessToken(authRequestJSONFileName);

// get all active groups of the user
JSONObject userGroups = RestApiUserGroups.getUserGroups(accessToken);
JSONArray userGroupsList = (JSONArray) userGroups.get("groupInfoList");

// select the first group the user can send from
String groupId = null;
for (Object eachUserGroup : userGroupsList) {
JSONObject userGroup = (JSONObject) eachUserGroup;
String groupStatus = (String) userGroup.get("status");
JSONObject groupSettings = (JSONObject) userGroup.get("settings");
JSONObject userCanSend = (JSONObject) groupSettings.get("userCanSend");
Boolean userCanSendValue = (Boolean) userCanSend.get("value");
if (groupStatus.equals("ACTIVE") && userCanSendValue) {
groupId = userGroup.get("id").toString();
break;
}
}

// no group with send permissions was found
if (groupId == null){
System.err.println("No Group the user can send from");
return;
}

// Fetch library documents of the user using access token from above.
JSONObject libraryDocumentsResponse = RestApiLibraryDocuments.getLibraryDocuments(accessToken);
JSONObject libraryDocumentsResponse = RestApiLibraryDocuments.getLibraryDocuments(accessToken, groupId);

// Retrieve library documents list for the user and fetch the ID of first library document.
JSONArray libraryDocumentList = (JSONArray) libraryDocumentsResponse.get("libraryDocumentList");

String libraryDocumentId = null;

// Fetch the first personal or shared library document of the user.
for (Object eachLibraryDocument : libraryDocumentList) {
JSONObject libraryDocument = (JSONObject) eachLibraryDocument;
Expand All @@ -77,7 +101,7 @@ private void run() throws Exception {

if (libraryDocumentId != null && !libraryDocumentId.isEmpty()) {
// Send agreement using this library document ID retrieved from above.
JSONObject sendAgreementResponse = RestApiAgreements.sendAgreement(accessToken, sendAgreementJSONFileName, libraryDocumentId,
JSONObject sendAgreementResponse = RestApiAgreements.sendAgreement(accessToken, sendAgreementJSONFileName, libraryDocumentId, groupId,
RestApiAgreements.DocumentIdentifierName.LIBRARY_DOCUMENT_ID);

// Parse and read response.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package adobesign.api.rest.sample;

import adobesign.api.rest.sample.util.RestApiUserGroups;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import adobesign.api.rest.sample.util.RestApiAgreements;
Expand Down Expand Up @@ -79,13 +81,37 @@ private void run() throws Exception {
// Fetch oauth access token to make further API calls.
String accessToken = RestApiOAuthTokens.getOauthAccessToken(authRequestJSONFileName);

// get all active groups of the user
JSONObject userGroups = RestApiUserGroups.getUserGroups(accessToken);
JSONArray userGroupsList = (JSONArray) userGroups.get("groupInfoList");

// select the first group the user can send from
String groupId = null;
for (Object eachUserGroup : userGroupsList) {
JSONObject userGroup = (JSONObject) eachUserGroup;
String groupStatus = (String) userGroup.get("status");
JSONObject groupSettings = (JSONObject) userGroup.get("settings");
JSONObject userCanSend = (JSONObject) groupSettings.get("userCanSend");
Boolean userCanSendValue = (Boolean) userCanSend.get("value");
if (groupStatus.equals("ACTIVE") && userCanSendValue) {
groupId = userGroup.get("id").toString();
break;
}
}

// no group with send permissions was found
if (groupId == null){
System.err.println("No Group the user can send from");
return;
}

// Upload a transient document and retrieve transient document ID from the response.
JSONObject uploadDocumentResponse = RestApiAgreements.postTransientDocument(accessToken, mimeType, fileToBeUploaded, uploadedFileName);
String transientDocumentId = (String) uploadDocumentResponse.get("transientDocumentId");

// Send an agreement using the transient document ID derived from above.
DocumentIdentifierName idName = DocumentIdentifierName.TRANSIENT_DOCUMENT_ID;
JSONObject sendAgreementResponse = RestApiAgreements.sendAgreement(accessToken, sendAgreementJSONFileName, transientDocumentId, idName);
JSONObject sendAgreementResponse = RestApiAgreements.sendAgreement(accessToken, sendAgreementJSONFileName, transientDocumentId, groupId, idName);

// Parse and read response.
System.out.println("Agreement Sent. Agreement ID = " + sendAgreementResponse.get("id"));
Expand Down
10 changes: 8 additions & 2 deletions v6/adobesign/api/rest/sample/util/RestApiAgreements.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RestApiAgreements {
private static final String TRANSIENT_DOCUMENTS_ENDPOINT = "/transientDocuments";

private static final String FILEINFOS = "fileInfos";

private static final String GROUPID = "groupId";
/**
* Represents the various ways that a set of documents can be identified, depending on the context.
*/
Expand Down Expand Up @@ -100,6 +100,7 @@ public static JSONObject postTransientDocument(String accessToken, String mimeTy
* @param requestJsonFile Name of the file containing the JSON structure used as the input for this API call.
* @param documentId Document ID of the document to be associated with the agreement. It can refer to a transient document or a library
* document.
* @param groupId The group used to send the agreement.
* @param idName Name by which to refer to a list containing document IDs when adding it to the input JSON structure. Must be one of
* <ul>
* <li>DocumentIdentifierName.TRANSIENT_DOCUMENT_IDS</li>
Expand All @@ -110,7 +111,7 @@ public static JSONObject postTransientDocument(String accessToken, String mimeTy
* @see DocumentIdentifierName
*/
@SuppressWarnings("unchecked")
public static JSONObject sendAgreement(String accessToken, String requestJsonFile, String documentId, DocumentIdentifierName idName) throws Exception {
public static JSONObject sendAgreement(String accessToken, String requestJsonFile, String documentId, String groupId, DocumentIdentifierName idName) throws Exception {
// URL to invoke the agreements end point.
String url = RestApiUtils.getBaseURIForAPI(accessToken) + AGREEMENTS_ENDPOINT;

Expand All @@ -135,6 +136,11 @@ public static JSONObject sendAgreement(String accessToken, String requestJsonFil
fileInfos.add(fileInfo);
requestJson.put(FILEINFOS, fileInfos);
}
if (groupId != null)
{
requestJson.put(GROUPID, groupId);
}

responseJson = (JSONObject) RestApiUtils.makeApiCall(url, RestApiUtils.HttpRequestMethod.POST, headers, requestJson.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public class RestApiLibraryDocuments {
* Fetches the list of all library documents.
*
* @param accessToken access token of the user whose library documents are to be fetched.
* @param accessToken the group id to filter on
* @return JSON response containing the list of all the library documents for the user.
* @throws IOException
*/
public static JSONObject getLibraryDocuments(String accessToken) throws Exception {
public static JSONObject getLibraryDocuments(String accessToken, String groupId) throws Exception {
// URL for library documents end point.
String url = RestApiUtils.getBaseURIForAPI(accessToken) + LIBRARY_DOCUMENTS_ENDPOINT;
if (groupId != null)
url+="?groupId" + groupId;

// Create header list.
HashMap<String, String> headers = new HashMap<String, String>();
Expand Down
48 changes: 48 additions & 0 deletions v6/adobesign/api/rest/sample/util/RestApiUserGroups.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*************************************************************************
* ADOBE SYSTEMS INCORPORATED
* Copyright 2020 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the
* terms of the Adobe license agreement accompanying it. If you have received this file from a
* source other than Adobe, then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
**************************************************************************/

package adobesign.api.rest.sample.util;

import org.json.simple.JSONObject;

import java.io.IOException;
import java.util.HashMap;

/**
* Encapsulates calls to REST end points related to users groups.
*/
public class RestApiUserGroups {
private static final String userGroups = "/users/me/groups";

/**
* Fetches the list of all active groups memberships of a user.
*
* @param accessToken access token of the user whose groups are to be fetched.
* @return JSON response containing the list of all the groups the user is a member of.
* @throws IOException
*/
public static JSONObject getUserGroups(String accessToken) throws Exception {
// URL for library documents end point.
String url = RestApiUtils.getBaseURIForAPI(accessToken) + userGroups;

// Create header list.
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(RestApiUtils.HttpHeaderField.CONTENT_TYPE.toString(), RestApiUtils.MimeType.JSON.toString());
headers.put(RestApiUtils.HttpHeaderField.AUTHORIZATION.toString(), accessToken);

// Invoke API and get JSON response.
JSONObject responseJSON = null;
responseJSON = (JSONObject) RestApiUtils.makeApiCall(url, RestApiUtils.HttpRequestMethod.GET, headers);

return responseJSON;
}

}