-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement conductor client with Http client
- Loading branch information
1 parent
b6a243b
commit 8df5f7f
Showing
8 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,10 @@ protected override void Load(ContainerBuilder builder) | |
|
||
private static void RegisterTasks(ContainerBuilder builder) | ||
{ | ||
builder.RegisterWorkerTask<PrepareEmailHandler>(); | ||
builder.RegisterWorkerTask<PrepareEmailHandler>(options => | ||
{ | ||
options.OwnerEmail = "[email protected]"; | ||
}); | ||
} | ||
|
||
private static void RegisterWorkflows(ContainerBuilder builder) { } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,10 @@ protected override void Load(ContainerBuilder builder) | |
|
||
private static void RegisterTasks(ContainerBuilder builder) | ||
{ | ||
builder.RegisterWorkerTask<GetCustomerHandler>(); | ||
builder.RegisterWorkerTask<GetCustomerHandler>(options => | ||
{ | ||
options.OwnerEmail = "[email protected]"; | ||
}); | ||
} | ||
|
||
private static void RegisterWorkflows(ContainerBuilder builder) { } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/ConductorSharp.Client/Service/ConductorHttpClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using ConductorSharp.Client.Exceptions; | ||
using ConductorSharp.Client.Model.Response; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using JsonSerializer = Newtonsoft.Json.JsonSerializer; | ||
|
||
namespace ConductorSharp.Client.Service | ||
{ | ||
public class ConductorHttpClient : IConductorClient | ||
{ | ||
private readonly ILogger<ConductorClient> _logger; | ||
private readonly HttpClient _httpClient; | ||
private readonly RestConfig _restConfig; | ||
|
||
public ConductorHttpClient(ILogger<ConductorClient> logger, HttpClient httpClient, RestConfig restConfig) | ||
{ | ||
_logger = logger; | ||
_httpClient = httpClient; | ||
_restConfig = restConfig; | ||
} | ||
|
||
private async void CheckResponse(HttpResponseMessage response) | ||
{ | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var request = await response.RequestMessage.Content.ReadAsStringAsync(); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
ConductorErrorResponse error = default; | ||
|
||
_logger.LogDebug("Received {@response}", response.Content); | ||
|
||
error = JsonConvert.DeserializeObject<ConductorErrorResponse>(responseContent); | ||
|
||
if (error == null || string.IsNullOrEmpty(error.Message)) | ||
throw new Exception("Unable to deserialize error"); | ||
|
||
_logger.LogError("{@conductorError}", error); | ||
|
||
if (!_restConfig.IgnoreValidationErrors && error?.Message?.Contains("Validation failed") == true) | ||
throw new Exception(responseContent); | ||
|
||
if (response.StatusCode == HttpStatusCode.InternalServerError) | ||
throw new Exception(responseContent); | ||
|
||
if (response.StatusCode == HttpStatusCode.NotFound) | ||
throw new NotFoundException(error.Message); | ||
} | ||
} | ||
|
||
private HttpRequestMessage CreateRequest(Uri relativeUrl, HttpMethod method, object body = null) | ||
{ | ||
Uri uri; | ||
|
||
if (relativeUrl.IsAbsoluteUri) | ||
uri = relativeUrl; | ||
else | ||
uri = new Uri($"{_restConfig.BaseUrl}/{_restConfig.ApiPath}/{relativeUrl.OriginalString}"); | ||
|
||
var httpRequestMessage = new HttpRequestMessage(method, uri); | ||
|
||
if (body != null) | ||
httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); | ||
|
||
return httpRequestMessage; | ||
} | ||
|
||
public async Task<T> ExecuteRequestAsync<T>(Uri relativeUrl, HttpMethod method, object resource) | ||
{ | ||
var request = CreateRequest(relativeUrl, method, resource); | ||
var response = await _httpClient.SendAsync(request); | ||
|
||
CheckResponse(response); | ||
|
||
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
public async Task<T> ExecuteRequestAsync<T>(Uri relativeUrl, HttpMethod method) | ||
{ | ||
var request = CreateRequest(relativeUrl, method); | ||
|
||
var response = await _httpClient.SendAsync(request); | ||
|
||
CheckResponse(response); | ||
|
||
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
public async Task ExecuteRequestAsync(Uri relativeUrl, HttpMethod method, object resource) | ||
{ | ||
var request = CreateRequest(relativeUrl, method, resource); | ||
var response = await _httpClient.SendAsync(request); | ||
|
||
CheckResponse(response); | ||
} | ||
|
||
public async Task ExecuteRequestAsync(Uri relativeUrl, HttpMethod method) | ||
{ | ||
var request = CreateRequest(relativeUrl, method); | ||
var response = await _httpClient.SendAsync(request); | ||
|
||
CheckResponse(response); | ||
} | ||
|
||
public async Task<T> ExecuteRequestAsync<T>(Uri relativeUrl, HttpMethod method, object resource, Dictionary<string, string> headers) | ||
{ | ||
var request = CreateRequest(relativeUrl, method, resource); | ||
|
||
foreach (var header in headers) | ||
request.Headers.Add(header.Key, header.Value); | ||
|
||
var response = await _httpClient.SendAsync(request); | ||
|
||
CheckResponse(response); | ||
|
||
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
public async Task<string> ExecuteRequestAsync(Uri relativeUrl, HttpMethod method, object resource, Dictionary<string, string> headers) | ||
{ | ||
var request = CreateRequest(relativeUrl, method, resource); | ||
|
||
foreach (var header in headers) | ||
request.Headers.Add(header.Key, header.Value); | ||
|
||
var response = await _httpClient.SendAsync(request); | ||
|
||
CheckResponse(response); | ||
|
||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,10 @@ public static IExecutionManagerBuilder AddCSharpLambdaTasks( | |
string csharpLambdaTaskNamePrefix = null | ||
) | ||
{ | ||
executionManagerBuilder.Builder.RegisterWorkerTask<CSharpLambdaTask>(); | ||
executionManagerBuilder.Builder.RegisterWorkerTask<CSharpLambdaTask>(options => | ||
{ | ||
options.OwnerEmail = "[email protected]"; | ||
}); | ||
executionManagerBuilder.Builder.RegisterMediatR(typeof(CSharpLambdaTask).Assembly); | ||
executionManagerBuilder.Builder.RegisterInstance( | ||
new ConfigurationProperty(CSharpLambdaTask.LambdaTaskNameConfigurationProperty, csharpLambdaTaskNamePrefix) | ||
|