开发人员(Cold Fusion)要求我公开REST API,以便他们可以从内部开发的应用程序以编程方式创建工作项。
这是我第一次涉足TFS中的REST,我不知道从哪里开始。我检查了Microsoft文档,但它当然偏向于.NET或客户端库,但据我所知,我不能用这些做任何事情,因为它是冷聚变环境进行“调用”?
我可以就如何实现这一目标获得一些建议吗?
Representational State Transfer(REST)API是支持HTTP操作集(方法)的服务端点,它提供对服务资源的创建,检索,更新或删除访问。
创建工作项的Api如下:
POST https://{accountName}.visualstudio.com/{project}/_apis/wit/workitems/${type}?api-version=4.1
[
{
"op": "add",
"path": "/fields/System.Title",
"from": null,
"value": "Sample task"
}
]
如果您只想测试其余的api,可以下载Postman,并使用它测试api。如果要在代码中使用其余的api,可以参考下面的示例。
以下是获取帐户项目列表的示例:
using System.Net.Http;
using System.Net.Http.Headers;
...
//encode your personal access token
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));
ListofProjectsResponse.Projects viewModel = null;
//use the httpclient
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://{accountname}.visualstudio.com"); //url of our account
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
//connect to the REST endpoint
HttpResponseMessage response = client.GetAsync("_apis/projects?stateFilter=All&api-version=1.0").Result;
//check to see if we have a succesfull respond
if (response.IsSuccessStatusCode)
{
//set the viewmodel from the content in the response
viewModel = response.Content.ReadAsAsync<ListofProjectsResponse.Projects>().Result;
//var value = response.Content.ReadAsStringAsync().Result;
}
}
有用的链接: