2

所以我正在编写一个需要能够从 Rally 读取历史数据的 C#.NET 应用程序。具体来说,我需要为发布的前 7 天中的每一天检索给定版本的总计划估计值。

我读过基本上有两种方法可以做到这一点;通过修订历史记录或通过 Lookback API 并使用 POST。我认为 LBAPI 会更干净、更健壮,所以我走上了这条路。不幸的是,我以前从未使用过 POST,所以我不完全确定我在做什么。

到目前为止,这是我想出的。我觉得我要么很接近并且错过了一些非常明显的东西,要么我完全离开了:

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);

当我在网络浏览器中使用编译后的 url 时,我得到了正确的数据:

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

当我使用上面的 C# 代码并调用时:

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

我收到一个 WebException 说明:“远程服务器返回错误:(405) Method Not Allowed。” 这几乎听起来我什至不能使用那个 post 方法开始。

任何指导将不胜感激。

4

1 回答 1

2

如果其他人想做类似的事情,这是我遵循马克指导的解决方案(可能会做得更干净,但这对我有用):

    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;
    }
于 2014-04-16T19:13:12.583 回答