private async Task <HttpClient> CreateHttpClientAsync() { var client = new HttpClient { Timeout = Timeout.InfiniteTimeSpan }; if (!string.IsNullOrEmpty(this.username)) { this.logger?.LogDebug("Setting Authorization request header..."); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(InedoLib.UTF8Encoding.GetBytes(this.username + ":" + AH.Unprotect(this.password)))); } if (this.csrfProtectionEnabled) { this.logger?.LogDebug("Checking for CSRF protection..."); using (var response = await client.GetAsync(IJenkinsConnectionInfoExtensions.GetApiUrl(this.serverUrl) + "/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)").ConfigureAwait(false)) { // Assume if the request failed that Jenkins is not set up to use CSRF protection. if (response.IsSuccessStatusCode) { var csrfHeader = (await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Split(new[] { ':' }, 2); if (csrfHeader.Length == 2) { client.DefaultRequestHeaders.Add(csrfHeader[0], csrfHeader[1]); } } } } return(client); }
private async Task <OpenArtifact> OpenAsync(string url) { if (string.IsNullOrEmpty(this.serverUrl)) { throw new InvalidOperationException("Jenkins ServerUrl has not been set."); } var client = await this.CreateHttpClientAsync().ConfigureAwait(false); var downloadUrl = IJenkinsConnectionInfoExtensions.GetApiUrl(this.serverUrl) + '/' + url.TrimStart('/'); this.logger?.LogDebug($"Downloading file from {downloadUrl}..."); var response = await client.GetAsync(downloadUrl, this.cancellationToken).ConfigureAwait(false); response.EnsureSuccessStatusCode(); return(new OpenArtifact(client, response, await response.Content.ReadAsStreamAsync().ConfigureAwait(false))); }
private async Task DownloadAsync(string url, string toFileName) { if (string.IsNullOrEmpty(serverUrl)) { throw new InvalidOperationException("Jenkins ServerUrl has not been set."); } using (var client = await this.CreateHttpClientAsync().ConfigureAwait(false)) { var downloadUrl = IJenkinsConnectionInfoExtensions.GetApiUrl(this.serverUrl) + '/' + url.TrimStart('/'); this.logger?.LogDebug($"Downloading file from {downloadUrl}..."); using (var response = await client.GetAsync(downloadUrl, this.cancellationToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); using (var file = new FileStream(toFileName, FileMode.Create, FileAccess.Write)) { await response.Content.CopyToAsync(file).ConfigureAwait(false); } } } }
private async Task <string> GetAsync(string url, NotFoundAction action = NotFoundAction.ThrowException) { if (string.IsNullOrEmpty(serverUrl)) { return(null); } using (var client = await this.CreateHttpClientAsync().ConfigureAwait(false)) { var downloadUrl = IJenkinsConnectionInfoExtensions.GetApiUrl(this.serverUrl) + '/' + url.TrimStart('/'); this.logger?.LogDebug($"Downloading string from {downloadUrl}..."); using (var response = await client.GetAsync(downloadUrl, this.cancellationToken).ConfigureAwait(false)) { if (response.StatusCode == HttpStatusCode.NotFound && action == NotFoundAction.ReturnNull) { return(null); } response.EnsureSuccessStatusCode(); return(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); } } }
private async Task <string> PostAsync(string url) { if (string.IsNullOrEmpty(serverUrl)) { throw new InvalidOperationException("Jenkins ServerUrl has not been set."); } using (var client = await this.CreateHttpClientAsync().ConfigureAwait(false)) { var uploafUrl = IJenkinsConnectionInfoExtensions.GetApiUrl(this.serverUrl) + '/' + url.TrimStart('/'); this.logger?.LogDebug($"Posting to {uploafUrl}..."); using (var response = await client.PostAsync(uploafUrl, new StringContent(string.Empty), this.cancellationToken).ConfigureAwait(false)) { if (!response.IsSuccessStatusCode) { string message = await response.Content.ReadAsStringAsync().ConfigureAwait(false); throw new WebException("Invalid Jenkins API call, response body was: " + message); } return(response.Headers.Location?.OriginalString); } } }
public async Task <JenkinsQueueItem> GetQueuedBuildInfoAsync(int queueItem) { using (var client = await this.CreateHttpClientAsync().ConfigureAwait(false)) using (var response = await client.GetAsync(IJenkinsConnectionInfoExtensions.GetApiUrl(this.serverUrl) + "/queue/item/" + queueItem + "/api/xml?tree=executable[number],why", this.cancellationToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); var item = XDocument.Load(await response.Content.ReadAsStreamAsync().ConfigureAwait(false)).Root; var buildNumber = item.Element("executable")?.Element("number")?.Value; if (!string.IsNullOrEmpty(buildNumber)) { return(new JenkinsQueueItem { BuildNumber = buildNumber }); } return(new JenkinsQueueItem { WaitReason = item.Element("why")?.Value }); } }