Task <HttpResponseMessage> IRestApiService.InvokeAsync(Models.Connection connection, string path, string method, string body, string requestContentType, string responseContentType, Dictionary <string, string> additionalHeaders, Dictionary <string, string> queryParameters, string apiVersion, string serviceHostName) { var conn = connection.InnerConnection; path = path.TrimStart('/'); if (!string.IsNullOrEmpty(serviceHostName)) { if (!serviceHostName.Contains(".")) { Logger.Log($"Converting service prefix {serviceHostName} to {serviceHostName}.dev.azure.com"); serviceHostName += ".dev.azure.com"; } Logger.Log($"Using service host {serviceHostName}"); GenericHttpClient.UseHost(serviceHostName); } _client = conn.GetClient <GenericHttpClient>(); var task = _client.InvokeAsync(new HttpMethod(method), path, body, requestContentType, responseContentType, additionalHeaders, queryParameters, apiVersion); return(task); }
/// <summary> /// Performs execution of the command /// </summary> protected override void DoProcessRecord() { if (Path.Contains(" ")) { var tokens = Path.Split(' '); if (IsHttpMethod(tokens[0])) { Method = tokens[0]; Path = Path.Substring(tokens[0].Length + 1); } } var tpc = this.GetCollection(); Path = Path.Replace("https://{instance}/{collection}/", "http://tfs/"); if (Uri.TryCreate(Path, UriKind.Absolute, out var uri)) { var host = uri.Host; if (host.EndsWith(".dev.azure.com")) { UseHost = host; } Path = uri.AbsolutePath.Replace("%7Borganization%7D/", ""); if (uri.AbsoluteUri.StartsWith(tpc.Uri.AbsoluteUri)) { Path = Path.Substring(tpc.Uri.AbsoluteUri.Length); } var query = uri.ParseQueryString(); if (query["api-version"] != null) { ApiVersion = query["api-version"]; } } if (Path.Contains("%7Bproject%7D") || Path.Contains("%7BprojectId%7D")) { var(_, tp) = GetCollectionAndProject(); Path = Path .Replace("%7Bproject%7D", tp.Id.ToString()) .Replace("%7BprojectId%7D", tp.Id.ToString()); this.Log($"Replace token {{project[Id]}} in URL with [{tp.Id}]"); } if (Path.Contains("%7Bteam%7D") || Path.Contains("%7BteamId%7D")) { var(_, _, t) = GetCollectionProjectAndTeam(); Path = Path .Replace("%7Bteam%7D", t.Id.ToString()) .Replace("%7BteamId%7D", t.Id.ToString()); this.Log($"Replace token {{team}} in URL with [{t.Id}]"); } this.Log($"Path '{Path}', version '{ApiVersion}'"); if (tpc.IsHosted && !string.IsNullOrEmpty(UseHost)) { GenericHttpClient.UseHost(UseHost); } var client = this.GetService <IRestApiService>(); var task = client.InvokeAsync(tpc, Path, Method, Body, RequestContentType, ResponseContentType, AdditionalHeaders.ToDictionary <string, string>(), QueryParameters.ToDictionary <string, string>(), ApiVersion); this.Log($"{Method} {client.Uri.AbsoluteUri}"); if (AsTask) { WriteObject(task); return; } var result = task.GetResult("Unknown error when calling REST API"); var responseBody = result.Content.ReadAsStringAsync().GetAwaiter().GetResult(); var responseType = result.Content.Headers.ContentType.MediaType; WriteObject(!Raw && responseType.Equals("application/json") ? PSJsonConverter.Deserialize(responseBody) : responseBody); }