/// <summary> /// Create teams /// </summary> /// <param name="json"></param> /// <param name="project"></param> /// <returns></returns> public GetTeamResponse.Team CreateNewTeam(string json, string project) { GetTeamResponse.Team viewModel = new GetTeamResponse.Team(); 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("Bearer", _credentials); // serialize the fields array into a json string //var patchValue = new StringContent(JsonConvert.SerializeObject(team), Encoding.UTF8, "application/json"); var jsonContent = new StringContent(json, Encoding.UTF8, "application/json"); var method = new HttpMethod("POST"); var request = new HttpRequestMessage(method, _configuration.UriString + "/_apis/projects/" + project + "/teams?api-version=4.1") { Content = jsonContent }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetTeamResponse.Team>().Result; } else { var errorMessage = response.Content.ReadAsStringAsync(); string error = Utility.GeterroMessage(errorMessage.Result.ToString()); this.lastFailureMessage = error; } } return(viewModel); }
public GetTeamResponse.Team CreateTeam(string project, string newTeam) { GetTeamResponse.Team viewModel = new GetTeamResponse.Team(); TeamPost team = new TeamPost() { name = newTeam }; 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(team), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call var method = new HttpMethod("POST"); var request = new HttpRequestMessage(method, _configuration.UriString + "/_apis/projects/" + project + "/teams?api-version=2.2") { Content = patchValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetTeamResponse.Team>().Result; } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } }
public void ProjectsAndTeams_Teams_CreateTeam_Success() { // arrange Teams request = new Teams(_configuration); // act GetTeamResponse.Team response = request.CreateTeam(_configuration.Project, "My Awesome Team"); // assert if (response.HttpStatusCode == HttpStatusCode.NotFound) { Assert.Inconclusive("team '" + _configuration.Team + "' not found"); } else { Assert.AreEqual(HttpStatusCode.Created, response.HttpStatusCode); } request = null; }
public GetTeamResponse.Team GetTeam(string project, string team) { GetTeamResponse.Team viewModel = new GetTeamResponse.Team(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(_configuration.UriString); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); HttpResponseMessage response = client.GetAsync("_apis/projects/" + project + "/teams/" + team + "?api-version=2.2").Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetTeamResponse.Team>().Result; } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } }
/// <summary> /// Create teams /// </summary> /// <param name="json"></param> /// <param name="project"></param> /// <returns></returns> public GetTeamResponse.Team CreateNewTeam(string json, string project) { try { using (HttpClient client = GetHttpClient()) { // serialize the fields array into a json string //var patchValue = new StringContent(JsonConvert.SerializeObject(team), Encoding.UTF8, "application/json"); var jsonContent = new StringContent(json, Encoding.UTF8, "application/json"); var method = new HttpMethod("POST"); var request = new HttpRequestMessage(method, client.BaseAddress + "/_apis/projects/" + project + "/teams?api-version=" + _configuration.VersionNumber) { Content = jsonContent }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { GetTeamResponse.Team viewModel = new GetTeamResponse.Team(); viewModel = response.Content.ReadAsAsync <GetTeamResponse.Team>().Result; return(viewModel); } else { logger.Debug(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t CreateNewTeam \t" + response.Content.ReadAsStringAsync().Result); var errorMessage = response.Content.ReadAsStringAsync(); string error = Utility.GeterroMessage(errorMessage.Result.ToString()); LastFailureMessage = error; } } } catch (Exception ex) { logger.Debug(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + "CreateNewTeam" + "\t" + ex.Message + "\t" + "\n" + ex.StackTrace + "\n"); } return(new GetTeamResponse.Team()); }