Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Willy/add integratons #8

Merged
merged 8 commits into from
Jul 10, 2024
Merged
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 DapperAutoData.Examples/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoFixture;
using FluentAssertions;

namespace DapperAutoData.Examples;

Expand All @@ -10,6 +11,8 @@ public class UnitTest1
public void Test1(TestClass a)
{
var b = a;

a.Name.Should().Be("John");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.1</Version>
</PropertyGroup>

</Project>
221 changes: 221 additions & 0 deletions DapperAutoData.SystemIntegrationUtilities/DapperServiceClientBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text.Json;
using System.Text;

namespace DapperAutoData.SystemIntegrationUtilities;

public class DapperServiceClientBase
{
protected readonly TokenProvider _tokenProvider;
protected readonly JsonSerializerOptions _jsonSerializerOptions;
public HttpClient _client;

public DapperServiceClientBase(HttpClient httpclient, JsonSerializerOptions jsonSerializerOptions, TokenProvider tokenProvider)
{
_client = httpclient;
_jsonSerializerOptions = jsonSerializerOptions;
_tokenProvider = tokenProvider;
}

#region Delete

public async Task<TResponse> Delete<TResponse>(string endpoint, Dictionary<string, string> headers, CancellationToken token)
{
SetHeaderRequest(headers);

var response = await _client.DeleteAsync(endpoint, token).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

public TResponse DeleteSync<TResponse>(string endpoint, Dictionary<string, string> headers, CancellationToken token)
{
SetHeaderRequest(headers);

var webRequest = new HttpRequestMessage(HttpMethod.Delete, endpoint);

var response = _client.Send(webRequest);

using var reader = new StreamReader(response.Content.ReadAsStream());
string responseContent = reader.ReadToEnd();
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

#endregion

#region Get

/// <summary>
/// Utility for making async Get requests with url and query parameters
/// </summary>
public async Task<TResponse> Get<TResponse>(string endpoint, Dictionary<string, string> headers, CancellationToken token)
{

SetHeaderRequest(headers);
HttpResponseMessage response = await _client.GetAsync(endpoint, token).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

public TResponse GetSync<TResponse>(string endpoint, Dictionary<string, string> headers)
{

SetHeaderRequest(headers);
var webRequest = new HttpRequestMessage(HttpMethod.Get, endpoint);

var response = _client.Send(webRequest);

using var reader = new StreamReader(response.Content.ReadAsStream());
string responseContent = reader.ReadToEnd();
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

#endregion

#region Post
public async Task<TResponse> Post<TRequest, TResponse>(string endpoint, TRequest request, Dictionary<string, string> headers, CancellationToken token)
{

var requestContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, MediaTypeNames.Application.Json);
SetHeaderRequest(headers);


var response = await _client.PostAsync(endpoint, requestContent, token).ConfigureAwait(false);

var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

/// <summary>
/// Utility for making sync Post requests with body, url, and query parameters
/// </summary>
public TResponse PostSync<TRequest, TResponse>(string endpoint, TRequest request, Dictionary<string, string> headers)
{
SetHeaderRequest(headers);

var webRequest = new HttpRequestMessage(HttpMethod.Post, endpoint)
{
Content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, MediaTypeNames.Application.Json)
};

var response = _client.Send(webRequest);

using var reader = new StreamReader(response.Content.ReadAsStream());
string responseContent = reader.ReadToEnd();
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

#endregion

#region Put

public async Task<TResponse> Put<TRequest, TResponse>(string endpoint, TRequest request, Dictionary<string, string> headers, CancellationToken token)
{

SetHeaderRequest(headers);

var requestContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, MediaTypeNames.Application.Json);

var response = await _client.PutAsync(endpoint, requestContent, token).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

/// <summary>
/// Utility for making sync Post requests with body, url, and query parameters
/// </summary>
public TResponse PutSync<TRequest, TResponse>(string endpoint, TRequest request, Dictionary<string, string> headers)
{
SetHeaderRequest(headers);

var webRequest = new HttpRequestMessage(HttpMethod.Put, endpoint)
{
Content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, MediaTypeNames.Application.Json)
};

var response = _client.Send(webRequest);

using var reader = new StreamReader(response.Content.ReadAsStream());
string responseContent = reader.ReadToEnd();
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}


#endregion

#region Patch

public async Task<TResponse> Patch<TRequest, TResponse>(string endpoint, TRequest request, Dictionary<string, string> headers, CancellationToken token)
{

SetHeaderRequest(headers);

var requestContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, MediaTypeNames.Application.Json);

var response = await _client.PatchAsync(endpoint, requestContent, token).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}

/// <summary>
/// Utility for making sync Post requests with body, url, and query parameters
/// </summary>
public TResponse PatchSync<TRequest, TResponse>(string endpoint, TRequest request, Dictionary<string, string> headers)
{
SetHeaderRequest(headers);

var webRequest = new HttpRequestMessage(HttpMethod.Patch, endpoint)
{
Content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, MediaTypeNames.Application.Json)
};

var response = _client.Send(webRequest);

using var reader = new StreamReader(response.Content.ReadAsStream());
string responseContent = reader.ReadToEnd();
var model = JsonSerializer.Deserialize<TResponse>(responseContent, _jsonSerializerOptions);

return model;
}


#endregion

public void SetHeaderRequest(Dictionary<string, string> headers)
{

_client.DefaultRequestHeaders.Clear();

if (_client.DefaultRequestHeaders.Authorization == null && _tokenProvider.AccessToken != null)
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _tokenProvider.AccessToken);

if (headers != null && headers.Any())
{
foreach (var item in headers)
{
_client.DefaultRequestHeaders.Add(item.Key, item.Value);
}
}

}
}
Loading