with Rally api v2.0, i am unable to access list of workspaces when i query subscription object

567 Views Asked by At

here is the code i am using to query for 'subscription' and then return a reference of the workspace i want to access:

public static String query_for_workspace(String ws, RallyRestApi r) throws IOException{

    String Wspace_ref=null;
    //Read Subscription
    QueryRequest subscriptionRequest = new QueryRequest("Subscriptions");
    subscriptionRequest.setFetch(new Fetch("Name", "Workspaces"));

    QueryResponse subscriptionQueryResponse = r.query(subscriptionRequest);
    String subName = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Name").toString();
    System.out.println("Read Subscription: " + subName);

    // Grab Workspaces Collection
    JsonArray myWorkspaces = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Workspaces").getAsJsonArray();

    // iterates through all workspaces and fetch 'ref' (string attribute) of the useful workspace
    for (int i=0; i<myWorkspaces.size(); i++) {
        JsonObject workspaceObject = myWorkspaces.get(i).getAsJsonObject();
        String workspaceRef = workspaceObject.get("_ref").getAsString();                
        GetRequest workspaceRequest = new GetRequest(workspaceRef);
        workspaceRequest.setFetch(new Fetch("Name"));
        GetResponse workspaceResponse = r.get(workspaceRequest);
        JsonObject workspaceObj = workspaceResponse.getObject();
        String workspaceName = workspaceObj.get("Name").getAsString();
        if(workspaceName.equals(ws)){
            System.out.printf("Workspace found ==> %s\n", workspaceName);
            Wspace_ref=workspaceRef;
        }
    }
    return Wspace_ref;
}

The error comes from this line:

JsonArray myWorkspaces=subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Workspaces").getAsJsonArray();

This code is working with 1.x API. How do I access the list of workspaces in 2.x?

Thank you!

1

There are 1 best solutions below

0
On BEST ANSWER

In Rally api v2.0 the ability to return child collections in the same response was removed for performance reasons. In v2.0 fetching a collection will return an object with the count and the url from which to get the collection data.

In the older versions of WS API certain fetch lists created a lot recursive calls, and all the collections included in the fetch make the call quite expensive. In WS API v2.0 this will not happen, since a separate call will have to be made in order to get objects of the collections.

So, I believe, in this situation if you update your code to:

public static String query_for_workspace(String ws, RallyRestApi r) throws IOException{

    String Wspace_ref=null;
    //Read Subscription
    QueryRequest subscriptionRequest = new QueryRequest("Subscriptions");

    QueryResponse subscriptionQueryResponse = r.query(subscriptionRequest);
    String subName = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Name").toString();
    System.out.println("Read Subscription: " + subName);

    //Grab Workspaces Collection
    QueryRequest workspaceRequest = new QueryRequest(subscriptionQueryResponse.getResults().get(0).getAsJsonObject().getAsJsonObject("Workspaces"));
    workspaceRequest.setFetch(new Fetch("Name", "_ref”));

    JsonArray myWorkspaces = r.query(workspaceRequest).getResults();

    //Iterate through the Workspaces to find the correct one
    String workspaceName = "";

    for (int i=0; i<myWorkspaces.size(); i++){
        workspaceName = myWorkspaces.get(i).getAsJsonObject().get("Name").getAsString();
        if(workspaceName.equals(ws)){
            System.out.printf("Workspace found ==> %s\n", workspaceName);
            Wspace_ref = myWorkspaces.get(i).getAsJsonObject().get("_ref").getAsString();
        }
    }
    return Wspace_ref;
}

That should provide the Ref you need.