diff --git a/src/ReportPortal.Client/Resources/ServiceAsyncLaunchResource.cs b/src/ReportPortal.Client/Resources/ServiceAsyncLaunchResource.cs index 2623c09..441ee2d 100644 --- a/src/ReportPortal.Client/Resources/ServiceAsyncLaunchResource.cs +++ b/src/ReportPortal.Client/Resources/ServiceAsyncLaunchResource.cs @@ -16,7 +16,7 @@ public ServiceAsyncLaunchResource(HttpClient httpClient, string project) : base( public async Task StartAsync(StartLaunchRequest request, CancellationToken cancellationToken) { return await PostAsJsonAsync( - $"v2/{ProjectName}/launch", request, cancellationToken).ConfigureAwait(false); + $"v2/{ProjectName}/launch", request, "application/x.reportportal.launch.v2+json", cancellationToken).ConfigureAwait(false); } public async Task FinishAsync(string uuid, FinishLaunchRequest request, CancellationToken cancellationToken) @@ -28,7 +28,7 @@ public async Task FinishAsync(string uuid, FinishLaunchR public async Task MergeAsync(MergeLaunchesRequest request, CancellationToken cancellationToken) { return await PostAsJsonAsync( - $"v2/{ProjectName}/launch/merge", request, cancellationToken).ConfigureAwait(false); + $"v2/{ProjectName}/launch/merge", request, "application/x.reportportal.launch.v2+json", cancellationToken).ConfigureAwait(false); } } } diff --git a/src/ReportPortal.Client/Resources/ServiceAsyncLogItemResource.cs b/src/ReportPortal.Client/Resources/ServiceAsyncLogItemResource.cs index 23b4124..71980a3 100644 --- a/src/ReportPortal.Client/Resources/ServiceAsyncLogItemResource.cs +++ b/src/ReportPortal.Client/Resources/ServiceAsyncLogItemResource.cs @@ -57,7 +57,7 @@ public async Task CreateAsync(CreateLogItemRequest[] re } } - return await SendHttpRequestAsync(HttpMethod.Post, uri, multipartContent, cancellationToken).ConfigureAwait(false); + return await SendHttpRequestAsync(HttpMethod.Post, uri, multipartContent, cancellationToken: cancellationToken).ConfigureAwait(false); } } } diff --git a/src/ReportPortal.Client/Resources/ServiceBaseResource.cs b/src/ReportPortal.Client/Resources/ServiceBaseResource.cs index 34d6e70..4e07898 100644 --- a/src/ReportPortal.Client/Resources/ServiceBaseResource.cs +++ b/src/ReportPortal.Client/Resources/ServiceBaseResource.cs @@ -21,108 +21,110 @@ public ServiceBaseResource(HttpClient httpClient, string projectName) protected Task GetAsJsonAsync(string uri, CancellationToken cancellationToken) { - return SendAsJsonAsync(HttpMethod.Get, uri, null, cancellationToken); + return SendAsJsonAsync(HttpMethod.Get, uri, null, cancellationToken: cancellationToken); + } + + protected Task GetAsJsonAsync(string uri, string accept, CancellationToken cancellationToken) + { + return SendAsJsonAsync(HttpMethod.Get, uri, null, accept, cancellationToken); } protected Task PostAsJsonAsync( string uri, TRequest request, CancellationToken cancellationToken) { - return SendAsJsonAsync(HttpMethod.Post, uri, request, cancellationToken); + return SendAsJsonAsync(HttpMethod.Post, uri, request, cancellationToken: cancellationToken); + } + + protected Task PostAsJsonAsync( + string uri, TRequest request, string accept, CancellationToken cancellationToken) + { + return SendAsJsonAsync(HttpMethod.Post, uri, request, accept, cancellationToken); } protected Task PutAsJsonAsync( string uri, TRequest request, CancellationToken cancellationToken) { - return SendAsJsonAsync(HttpMethod.Put, uri, request, cancellationToken); + return SendAsJsonAsync(HttpMethod.Put, uri, request, cancellationToken: cancellationToken); } protected Task DeleteAsJsonAsync(string uri, CancellationToken cancellationToken) { - return SendAsJsonAsync(HttpMethod.Delete, uri, null, cancellationToken); + return SendAsJsonAsync(HttpMethod.Delete, uri, null, cancellationToken: cancellationToken); } private async Task SendAsJsonAsync( - HttpMethod httpMethod, string uri, TRequest request, CancellationToken cancellationToken) + HttpMethod httpMethod, string uri, TRequest request, string acccept = "application/json", CancellationToken cancellationToken = default) { HttpContent httpContent = null; if (request != null) { - using (var memoryStream = new MemoryStream()) - { - await ModelSerializer.SerializeAsync(request, memoryStream, cancellationToken).ConfigureAwait(false); - memoryStream.Seek(0, SeekOrigin.Begin); - httpContent = new StreamContent(memoryStream); - httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - - return await SendHttpRequestAsync(httpMethod, uri, httpContent, cancellationToken).ConfigureAwait(false); - } + using var memoryStream = new MemoryStream(); + + await ModelSerializer.SerializeAsync(request, memoryStream, cancellationToken).ConfigureAwait(false); + memoryStream.Seek(0, SeekOrigin.Begin); + httpContent = new StreamContent(memoryStream); + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + + return await SendHttpRequestAsync(httpMethod, uri, httpContent, acccept, cancellationToken).ConfigureAwait(false); } else { - return await SendHttpRequestAsync(httpMethod, uri, httpContent, cancellationToken).ConfigureAwait(false); + return await SendHttpRequestAsync(httpMethod, uri, httpContent, acccept, cancellationToken).ConfigureAwait(false); } } protected async Task SendHttpRequestAsync( - HttpMethod httpMethod, string uri, HttpContent httpContent, CancellationToken cancellationToken) + HttpMethod httpMethod, string uri, HttpContent httpContent, string accept = "application/json", CancellationToken cancellationToken = default) { - using (var httpRequest = new HttpRequestMessage(httpMethod, uri)) + using var httpRequest = new HttpRequestMessage(httpMethod, uri); + + using (httpContent) { - using (httpContent) - { - httpRequest.Content = httpContent; - - using (var response = await HttpClient - .SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false)) - { - using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) - { - CheckSuccessStatusCode(response, stream); - - return await ModelSerializer.DeserializeAsync(stream, cancellationToken).ConfigureAwait(false); - } - } - } + httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); + httpRequest.Content = httpContent; + + using var response = await HttpClient + .SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + + using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); + + CheckSuccessStatusCode(response, stream); + + return await ModelSerializer.DeserializeAsync(stream, cancellationToken).ConfigureAwait(false); } } protected async Task GetAsBytesAsync(string uri, CancellationToken cancellationToken) { - using (var httpRequest = new HttpRequestMessage(HttpMethod.Get, uri)) - { - using (var response = await HttpClient - .SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false)) - { - using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) - { - CheckSuccessStatusCode(response, stream); - - using (var memoryStream = new MemoryStream()) - { - stream.CopyTo(memoryStream); - return memoryStream.ToArray(); - } - } - } - } + using var httpRequest = new HttpRequestMessage(HttpMethod.Get, uri); + + using var response = await HttpClient + .SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + + using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); + + CheckSuccessStatusCode(response, stream); + + using var memoryStream = new MemoryStream(); + + stream.CopyTo(memoryStream); + return memoryStream.ToArray(); } private void CheckSuccessStatusCode(HttpResponseMessage response, Stream stream) { if (!response.IsSuccessStatusCode) { - using (var reader = new StreamReader(stream)) - { - string responseBody = reader.ReadToEnd(); - - throw new ServiceException( - "Response status code does not indicate success.", - response.StatusCode, - response.RequestMessage.RequestUri, - response.RequestMessage.Method, - responseBody); - } + using var reader = new StreamReader(stream); + string responseBody = reader.ReadToEnd(); + + throw new ServiceException( + "Response status code does not indicate success.", + response.StatusCode, + response.RequestMessage.RequestUri, + response.RequestMessage.Method, + responseBody); } } } diff --git a/src/ReportPortal.Client/Resources/ServiceLaunchResource.cs b/src/ReportPortal.Client/Resources/ServiceLaunchResource.cs index f573512..df97866 100644 --- a/src/ReportPortal.Client/Resources/ServiceLaunchResource.cs +++ b/src/ReportPortal.Client/Resources/ServiceLaunchResource.cs @@ -16,7 +16,7 @@ public ServiceLaunchResource(HttpClient httpClient, string project) : base(httpC public async Task GetAsync(string uuid, CancellationToken cancellationToken) { - return await GetAsJsonAsync($"v1/{ProjectName}/launch/uuid/{uuid}", cancellationToken).ConfigureAwait(false); + return await GetAsJsonAsync($"v1/{ProjectName}/launch/uuid/{uuid}", "application/x.reportportal.launch.v2+json", cancellationToken).ConfigureAwait(false); } public async Task GetAsync(long id, CancellationToken cancellationToken) @@ -50,7 +50,7 @@ public async Task DeleteAsync(long id, CancellationToken cancel public async Task MergeAsync(MergeLaunchesRequest request, CancellationToken cancellationToken) { return await PostAsJsonAsync( - $"v1/{ProjectName}/launch/merge", request, cancellationToken).ConfigureAwait(false); + $"v1/{ProjectName}/launch/merge", request, "application/x.reportportal.launch.v2+json", cancellationToken).ConfigureAwait(false); } public async Task UpdateAsync(long id, UpdateLaunchRequest request, CancellationToken cancellationToken) diff --git a/src/ReportPortal.Client/Resources/ServiceLogItemResource.cs b/src/ReportPortal.Client/Resources/ServiceLogItemResource.cs index 528da3f..84027c6 100644 --- a/src/ReportPortal.Client/Resources/ServiceLogItemResource.cs +++ b/src/ReportPortal.Client/Resources/ServiceLogItemResource.cs @@ -90,7 +90,7 @@ public async Task CreateAsync(CreateLogItemRequest[] re } } - return await SendHttpRequestAsync(HttpMethod.Post, uri, multipartContent, cancellationToken).ConfigureAwait(false); + return await SendHttpRequestAsync(HttpMethod.Post, uri, multipartContent, cancellationToken: cancellationToken).ConfigureAwait(false); } } diff --git a/src/ReportPortal.Client/Resources/ServiceTestItemResource.cs b/src/ReportPortal.Client/Resources/ServiceTestItemResource.cs index c042da1..4e1dc5a 100644 --- a/src/ReportPortal.Client/Resources/ServiceTestItemResource.cs +++ b/src/ReportPortal.Client/Resources/ServiceTestItemResource.cs @@ -38,7 +38,7 @@ public async Task GetAsync(long id, CancellationToken cancella public async Task GetAsync(string uuid, CancellationToken cancellationToken) { - return await GetAsJsonAsync($"v1/{ProjectName}/item/uuid/{uuid}", cancellationToken).ConfigureAwait(false); + return await GetAsJsonAsync($"v1/{ProjectName}/item/uuid/{uuid}", "application/x.reportportal.test.v2+json", cancellationToken).ConfigureAwait(false); } public async Task StartAsync(StartTestItemRequest request, CancellationToken cancellationToken)