Skip to content

Commit

Permalink
updated simplified samples
Browse files Browse the repository at this point in the history
  • Loading branch information
danhellem committed Aug 11, 2016
1 parent 653c446 commit 29b1dd4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 33 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Team Services REST API Sample Code
Team Services REST API and .Net Client Library Sample Code
===================

This repo will eventually be chuck of code samples when using the REST API's in Team Services. Code is specifically optimized to be simple to read and understands.
This repo contains code samples for using the REST API's and .Net Client Libraries in Team Services. Code is optimized to be simple and easy to understand.

This is an active repo and we will be adding new code samples weekly. Feel free to contribute and/or provide feedback.

Let us know if you have any questions.
Feel free to contribute and/or provide feedback.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="GettingStarted\AuthenticationTest.cs" />
<Compile Include="InitHelper.cs" />
<Compile Include="ProjectsAndTeams\ProcessesTest.cs" />
<Compile Include="WorkItemTracking\SamplesTest.cs" />
<Compile Include="WorkItemTracking\WIQLTest.cs" />
<Compile Include="WorkItemTracking\QueriesTest.cs" />
<Compile Include="WorkItemTracking\FieldsTest.cs" />
Expand Down
39 changes: 39 additions & 0 deletions VSTSRestApiSamples.UnitTests/WorkItemTracking/SamplesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VstsRestApiSamples.WorkItemTracking;
using System.Net;

namespace VstsRestApiSamples.Tests.WorkItemTracking
{
[TestClass]
public class SamplesTest
{
private IConfiguration _configuration = new Configuration();

[TestInitialize]
public void TestInitialize()
{
InitHelper.GetConfiguration(_configuration);
}

[TestCleanup]
public void TestCleanup()
{
_configuration = null;
}

[TestMethod, TestCategory("REST API")]
public void WorkItemTracking_Samples_CreateBug_Success()
{
//arrange
Samples samples = new Samples(_configuration);

//act
var response = samples.CreateNewBug();

//assert
Assert.AreEqual("success", response);

samples = null;
}
}
}
58 changes: 30 additions & 28 deletions VSTSRestApiSamples/WorkItemTracking/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,43 @@ public Samples(IConfiguration configuration)
_credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _configuration.PersonalAccessToken)));
}

public void CreateNewBug()
{
var projectName = _configuration.Project;
public string CreateNewBug()
{
var projectName = _configuration.Project;

Object[] patchDocument = new Object[4];

WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
WorkItemPatch.Field[] fields = new WorkItemPatch.Field[4];
patchDocument[0] = new { op = "add", path = "/fields/System.Title", value = "Authorization Errors" };
patchDocument[1] = new { op = "add", path = "/fields/Microsoft.VSTS.TCM.ReproSteps", value = "Our authorization logic needs to allow for users with Microsoft accounts (formerly Live Ids) - http://msdn.microsoft.com/en-us/library/live/hh826547.aspx" };
patchDocument[2] = new { op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = "1" };
patchDocument[3] = new { op = "add", path = "/fields/Microsoft.VSTS.Common.Severity", value = "2 - High" };

//set some field values like title and description
fields[0] = new WorkItemPatch.Field() { op = "add", path = "/fields/System.Title", value = "Authorization Errors" };
fields[1] = new WorkItemPatch.Field() { op = "add", path = "/fields/Microsoft.VSTS.TCM.ReproSteps", value = "Our authorization logic needs to allow for users with Microsoft accounts (formerly Live Ids) - http://msdn.microsoft.com/en-us/library/live/hh826547.aspx" };
fields[2] = new WorkItemPatch.Field() { op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = "1" };
fields[3] = new WorkItemPatch.Field() { op = "add", path = "/fields/Microsoft.VSTS.Common.Severity", value = "2 - High" };

using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

//serialize the fields array into a json string
var patchValue = new StringContent(JsonConvert.SerializeObject(fields), Encoding.UTF8, "application/json-patch+json"); //mediaType needs to be application/json-patch+json for a patch call
//serialize the fields array into a json string
var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json"); //mediaType needs to be application/json-patch+json for a patch call

//set the httpmethod to Patch
var method = new HttpMethod("PATCH");
//set the httpmethod to Patch
var method = new HttpMethod("PATCH");

//send the request
var request = new HttpRequestMessage(method, _configuration.UriString + projectName + "/_apis/wit/workitems/$Bug?api-version=1.0") { Content = patchValue };
var response = client.SendAsync(request).Result;
//send the request
var request = new HttpRequestMessage(method, _configuration.UriString + projectName + "/_apis/wit/workitems/$Bug?api-version=1.0") { Content = patchValue };
var response = client.SendAsync(request).Result;

if (response.IsSuccessStatusCode)
{
viewModel = response.Content.ReadAsAsync<WorkItemPatchResponse.WorkItem>().Result;
}
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}

patchDocument = null;

return "success";
}
}
}

public void UdateExistingWorkItem()
{
Expand Down

0 comments on commit 29b1dd4

Please sign in to comment.