Add sample with SendHttpRequest

This commit is contained in:
Sipke Schoorstra 2023-01-23 15:59:06 +01:00
parent 0caa4c3c49
commit 0584dc0bdf
4 changed files with 87 additions and 0 deletions

View file

@ -160,6 +160,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.AzureServiceBu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.ElasticsearchStorage", "src\samples\aspnet\Elsa.Samples.ElasticsearchStorage\Elsa.Samples.ElasticsearchStorage.csproj", "{4A54379F-4775-41B6-9752-FF300288916A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.OutboundHttpRequests", "src\samples\console\Elsa.Samples.OutboundHttpRequests\Elsa.Samples.OutboundHttpRequests.csproj", "{39AB433B-F7A6-4DD3-873A-9527946B7BD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -410,6 +412,10 @@ Global
{4A54379F-4775-41B6-9752-FF300288916A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A54379F-4775-41B6-9752-FF300288916A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A54379F-4775-41B6-9752-FF300288916A}.Release|Any CPU.Build.0 = Release|Any CPU
{39AB433B-F7A6-4DD3-873A-9527946B7BD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39AB433B-F7A6-4DD3-873A-9527946B7BD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39AB433B-F7A6-4DD3-873A-9527946B7BD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39AB433B-F7A6-4DD3-873A-9527946B7BD8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{155227F0-A33B-40AA-A4B4-06F813EB921B} = {61017E64-6D00-49CB-9E81-5002DC8F7D5F}
@ -480,5 +486,6 @@ Global
{9F79BBEC-02CE-4F76-AED4-BC191DA3054B} = {5BA4A8FA-F7F4-45B3-AEC8-8886D35AAC79}
{C25CFDCF-8E06-4DD8-A83E-FF7EF9FDA02E} = {56C2FFB8-EA54-45B5-A095-4A78142EB4B5}
{4A54379F-4775-41B6-9752-FF300288916A} = {56C2FFB8-EA54-45B5-A095-4A78142EB4B5}
{39AB433B-F7A6-4DD3-873A-9527946B7BD8} = {873BFC3E-63C2-4495-A503-5EC05DCD84E4}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\bundles\Elsa\Elsa.csproj" />
<ProjectReference Include="..\..\..\modules\Elsa.Http\Elsa.Http.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,19 @@
using Elsa.Extensions;
using Elsa.Samples.OutboundHttpRequests.Workflows;
using Elsa.Workflows.Core.Services;
using Microsoft.Extensions.DependencyInjection;
// Setup service container.
var services = new ServiceCollection();
// Add Elsa services.
services.AddElsa(elsa => elsa.UseHttp());
// Build service container.
var serviceProvider = services.BuildServiceProvider();
// Resolve a workflow runner to run the workflow.
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
// Run the workflow.
await workflowRunner.RunAsync<GetUsersWorkflow>();

View file

@ -0,0 +1,45 @@
using System.Dynamic;
using Elsa.Http;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Services;
using Microsoft.AspNetCore.Http;
namespace Elsa.Samples.OutboundHttpRequests.Workflows;
/// <summary>
/// A workflow that retrieves a list of users from an API endpoint and prints out the names to the console output.
/// </summary>
public class GetUsersWorkflow : WorkflowBase
{
protected override void Build(IWorkflowBuilder builder)
{
var responseVariable = builder.WithVariable<ExpandoObject>();
var currentUserVariable = builder.WithVariable<ExpandoObject>();
builder.Root = new Sequence
{
Activities =
{
new SendHttpRequest
{
Url = new(new Uri("https://reqres.in/api/users")),
Method = new(HttpMethods.Get),
ParsedContent = new(responseVariable)
},
new ForEach<ExpandoObject>(context =>
{
var response = (dynamic)responseVariable.Get(context)!;
return (ICollection<ExpandoObject>)response.data;
})
{
CurrentValue = new(currentUserVariable),
Body = new WriteLine(context =>
{
var currentUser = (dynamic)currentUserVariable.Get(context)!;
return $"{currentUser.first_name} {currentUser.last_name}";
})
}
}
};
}
}