Tuesday, January 28, 2014

How to Login in to Rally using Java Rest API client

To access to Rally you need to login authentication
public RallyRestApi authenticate() throws URISyntaxException, Exception {

            //Provide Rally url,username and password
            RallyRestApi restApi = new RallyRestApi(new URI(https://us1.rallydev.com),
"username, "password");

          //If you are behind a proxy server provide the following
            restApi.setProxy("proxyserver", "userName", "password");
try {
if (restApi == null) {
LOGGER.debug("Authenticating Rally with provided credentials");
System.out.println("Initializing Rally Connector with user : "
+ userName);
LOGGER.debug("Trying to connect and login to rally : ");
restApi = new RallyRestApi(new URI(rallyUrl), userName,
password);
QueryRequest userRequest = new QueryRequest("User");
// userRequest.setFetch(new Fetch("UserName", "Subscription",
// "DisplayName"));
userRequest.setQueryFilter(new QueryFilter("UserName", "=",
ReadProperties.getInstance().getProperty("username")));
QueryResponse userQueryResponse;

userQueryResponse = restApi.query(userRequest);
JsonArray userQueryResults = userQueryResponse.getResults();
JsonElement userQueryElement = userQueryResults.get(0);
JsonObject userQueryObject = userQueryElement.getAsJsonObject();
String userRef = userQueryObject.get("_ref").toString();
LOGGER.info("Rally Authentication Successful!");
}
} catch (Exception e) {
LOGGER.error("Failed to Authenticate User [" + userName + "]"
+ e.getMessage());
e.printStackTrace();
}
return restApi;
}

1 comment:

  1. any code to update test results and testcases to rally using Rest API ?

    ReplyDelete