Search code examples
c#.netrallylookbackapi

Trying to POST via the .NET REST API


So I'm writing a C#.NET application that will need to be able to read historical data from Rally. Specifically I need to retrieve the total Plan Estimate for a given release for each of the first 7 days of the Release.

I've read that there are basically two ways to do this; via the revision history or via the Lookback API and using POST. I figured the LBAPI would be cleaner and more robust so I went that path. Unfortunately, I've never worked with POST before so I'm not entirely sure what I'm doing.

Here's what I've come up with so far. I feel like I'm either close and missing something blindingly obvious, or I'm totally off:

string uri = @"https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js";

DynamicJsonObject data = new DynamicJsonObject();
data["find"] = new DynamicJsonObject();
data["find"]["_ProjectHierarchy"] = 14457702297;
data["find"]["_TypeHierarchy"] = "HierarchicalRequirement";
data["find"]["Children"] = null;
data["find"]["__At"] = "2014-02-02T00Z%22";
data["fields"] = new string[] { "PlanEstimate", "ObjectID", "Name" };

DynamicJsonObject results = API.post(uri, data);

When I use the compiled url in a web browser, I get the right data back:

https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js?find={%22_ProjectHierarchy%22:14457702297,%22_TypeHierarchy%22:%22HierarchicalRequirement%22,%22Children%22:null,%22__At%22:%222014-02-02T00Z%22}&fields=[%22ObjectID%22,%22Name%22,%22PlanEstimate%22]&start=0

When I use the C# code above and call:

DynamicJsonObject results = API.post(uri, data);

I get a WebException stating: "The remote server returned an error: (405) Method Not Allowed." which almost sounds like I can't even use that post method to begin with.

Any guidance would be greatly appreciated.


Solution

  • In case anyone else is looking to do something similar, this was my solution following Mark's guidance (Could probably be done cleaner, but this works for me):

        private static DynamicJsonObject getBaseline(string day, long projectID, long ReleaseID)
        {
            int pageSize = 200;
            string uri = @"https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js?find={%22_ProjectHierarchy%22:" + 
                         projectID + @",%22_TypeHierarchy%22:%22HierarchicalRequirement%22,%22Release%22:{$in:[" + ReleaseID + @"]},%22Children%22:null,%22__At%22:%22" + 
                         day + @"T00Z%22}&fields=[%22PlanEstimate%22,%22ScheduleState%22]&hydrate=[%22ScheduleState%22]&start=0&pagesize=" + pageSize;
    
            DynamicJsonObject response = HttpGet(uri);
            DynamicJsonObject points = new DynamicJsonObject();
    
            foreach (var story in response["Results"])
            {
                // Do whatever
            }
            return points;
        }
    
        public static DynamicJsonObject HttpGet(string URI)
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
            req.Credentials = GetCredential();
            req.PreAuthenticate = true;
            req.ContentType = "application/json";
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
    
            DynamicJsonObject result = new DynamicJsonSerializer().Deserialize(sr.ReadToEnd());
            // Not sure if all of these are necessary
            sr.Close(); sr.Dispose();
            resp.Close();
            sr.Close(); sr.Dispose();
            return result;
        }
    
        private static CredentialCache GetCredential()
        {
            if (credentialCache == null)
            {
                string url = @"https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js?";
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                credentialCache = new CredentialCache();
                String user = ConfigurationManager.AppSettings["user"];
                String password = ConfigurationManager.AppSettings["password"];
                credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(user, password));
            }
            return credentialCache;
        }