Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ Thumbs.db

# dotCover
*.dotCover

# Tests
*.test.json
60 changes: 57 additions & 3 deletions src/Masterloop.Plugin.Application/ExtendedHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Masterloop.Plugin.Application
{
public class ExtendedHttpClient
internal class ExtendedHttpClient
{
private const int DefaultTimeoutInSeconds = 30;
private const string OriginAddressHeader = "OriginAddress";
Expand All @@ -16,6 +17,9 @@ public class ExtendedHttpClient

private readonly HttpClient _httpClient;

private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions
{ PropertyNameCaseInsensitive = true };

public ExtendedHttpClient(string username, string password, bool useCompression, string originAddress, ApplicationMetadata applicationMetadata, HttpClient httpClient)
{
Username = username;
Expand Down Expand Up @@ -76,6 +80,30 @@ public void SetMetaData(ApplicationMetadata applicationMetadata)
_httpClient.DefaultRequestHeaders.Add(OriginReferenceHeader, applicationMetadata.Reference);
}

public async Task<HttpTypeResponse<TResult>> DownloadAsync<TResult>(string url, string accept)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));

using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
if (response == null)
return null;

StatusCode = response.StatusCode;
StatusDescription = response.ReasonPhrase;

var stream = await response.Content.ReadAsStreamAsync();

if (response.IsSuccessStatusCode)
{
return new HttpTypeResponse<TResult>(response.StatusCode, response.ReasonPhrase, await JsonSerializer.DeserializeAsync<TResult>(stream, JsonSerializerOptions));
}

return new HttpTypeResponse<TResult>(response.StatusCode, response.ReasonPhrase);
}
}

public async Task<HttpStringResponse> DownloadStringAsync(string url, string accept)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
Expand Down Expand Up @@ -108,8 +136,34 @@ public async Task<HttpByteResponse> DownloadBytesAsync(string url, string accept
response.IsSuccessStatusCode ? await response.Content?.ReadAsByteArrayAsync() : null);
}

public async Task<HttpStringResponse> UploadStringAsync(string url, string body, string accept,
string contentType)
public async Task<HttpTypeResponse<TResult>> UploadAsync<TResult>(string url, string body, string accept, string contentType)
{
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new StringContent(body, Encoding.UTF8, contentType)
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));

using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
if (response == null)
return null;

StatusCode = response.StatusCode;
StatusDescription = response.ReasonPhrase;

var stream = await response.Content.ReadAsStreamAsync();

if (response.IsSuccessStatusCode)
{
return new HttpTypeResponse<TResult>(response.StatusCode, response.ReasonPhrase, await JsonSerializer.DeserializeAsync<TResult>(stream, JsonSerializerOptions));
}

return new HttpTypeResponse<TResult>(response.StatusCode, response.ReasonPhrase);
}
}

public async Task<HttpStringResponse> UploadStringAsync(string url, string body, string accept, string contentType)
{
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Masterloop.Plugin.Application/HttpByteResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Masterloop.Plugin.Application
{
public class HttpByteResponse
internal class HttpByteResponse
{
public HttpByteResponse(HttpStatusCode statusCode, string statusDescription, byte[] content)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Masterloop.Plugin.Application/HttpStringResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Masterloop.Plugin.Application
{
public class HttpStringResponse
internal class HttpStringResponse
{
public HttpStringResponse(HttpStatusCode statusCode, string statusDescription, string content)
{
Expand Down
22 changes: 22 additions & 0 deletions src/Masterloop.Plugin.Application/HttpTypeResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Net;

namespace Masterloop.Plugin.Application
{
internal class HttpTypeResponse<T>
{
public HttpTypeResponse(HttpStatusCode statusCode, string statusDescription)
{
StatusCode = statusCode;
StatusDescription = statusDescription;
}

public HttpTypeResponse(HttpStatusCode statusCode, string statusDescription, T content): this(statusCode, statusDescription)
{
Content = content;
}

public HttpStatusCode StatusCode { get; }
public string StatusDescription { get; }
public T Content { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
<PackageReference Include="Masterloop.Core.Types" Version="6.4.0" />
<PackageReference Include="System.Text.Json" Version="6.0.3" />
</ItemGroup>
</Project>
Loading