Skip to content

Commit 11cd768

Browse files
rylee-sollshalugin
authored andcommitted
Initial commit
0 parents  commit 11cd768

30 files changed

+1431
-0
lines changed

.gitignore

Lines changed: 454 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Callback API Sample
2+
3+
This is a sample of a Web Api server for working with the WorkflowServer Callback API.
4+
Callback API allows hosting your code of Actions, Conditions or Rules on third-party servers.
5+
Callback server should be connected in the admin panel on the Callback API page.
6+
More details on the [workflowserver.io](https://workflowserver.io/documentation/callback-api).
7+
8+
## What's inside
9+
10+
- **ApiController** – controller that implements all possible requests from Callback API of WFS 3.0.0.
11+
- **ActionProvider, ConditionProvider, IdentityProvider** – a sample of the implementation of API functions.
12+
- **VacationRequest** is a schema that implements this API.
13+
14+
## How to start
15+
16+
- Download, deploy and start WorkflowServer. More details on the [workflowserver.io](https://workflowserver.io/documentation/how-to-launch)
17+
- Deploy and start this Web Api Server.
18+
- Go to the WorkflowServer, in the sidebar go to "Api" -> "Callback API". Set up a connection properties to this API.
19+
More details on the [workflowserver.io](https://workflowserver.io/documentation/callback-api)
20+
- In the sidebar, go to "Workflow" -> "Scheme Management" -> "Create".
21+
Create a scheme, name it "VacationRequest" and upload the `Schemes/VacationRequest.xml` file from API project to the Scheme.
22+
- You can now test this sample with the WorkflowApi.
23+
Just create an instance, you can change the initial parameters if you like. Use the Workflow API to execute commands.
24+
Learn more about [workflowserver.io](https://workflowserver.io/documentation/workflow-api)

WorkflowServer.CallbackApi.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowServer.CallbackApi", "WorkflowServer.CallbackApi\WorkflowServer.CallbackApi.csproj", "{761E7062-16D0-434E-BE45-03E7AD8D002E}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{761E7062-16D0-434E-BE45-03E7AD8D002E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{761E7062-16D0-434E-BE45-03E7AD8D002E}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{761E7062-16D0-434E-BE45-03E7AD8D002E}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{761E7062-16D0-434E-BE45-03E7AD8D002E}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
using System.Xml;
2+
using Microsoft.AspNetCore.Mvc;
3+
using WorkflowServer.CallbackApi.Models;
4+
using WorkflowServer.CallbackApi.Workflow;
5+
6+
namespace WorkflowServer.CallbackApi.Controllers;
7+
8+
/// <summary>
9+
/// This controller implements all possible requests from Callback API of Workflow Server 3.0.0.
10+
/// Callback API allows hosting your code of Actions, Conditions or Rules on third-party servers.
11+
/// Callback server should be connected in the admin panel on the Callback API page.
12+
/// More details on the <see href="https://workflowserver.io/documentation/callback-api">workflowserver.io</see>
13+
/// </summary>
14+
[ApiController]
15+
[Route("[controller]/[action]")]
16+
public class ApiController : ControllerBase
17+
{
18+
public ApiController(ILogger<ApiController> logger)
19+
{
20+
_logger = logger;
21+
22+
_actionProvider = new ActionProvider();
23+
_conditionProvider = new ConditionProvider();
24+
_identityProvider = new IdentityProvider();
25+
26+
//The parameters will not be saved, this is an example. You should add the provider as a dependency injection.
27+
_parameterProvider = new ParameterProvider();
28+
}
29+
30+
#region Actions & Conditions Execution
31+
32+
/// <summary>
33+
/// Returns a list of actions that can be executed on the callback server.
34+
/// When the Workflow Server should execute an Action with a certain name,
35+
/// it calls the action execution method on the server that has returned this
36+
/// name in response to the request for the list of actions.
37+
/// </summary>
38+
[HttpGet]
39+
public Task<IActionResult> GetActions(string schemeCode, string? token = null)
40+
{
41+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok(_actionProvider.ActionNames)));
42+
}
43+
44+
/// <summary>
45+
/// Request to execute the action. Which of callback servers is called to execute this method, depends on the list of actions returned.
46+
/// </summary>
47+
[HttpPost]
48+
public async Task<IActionResult> ExecuteAction(InstanceRequest request)
49+
{
50+
await _actionProvider.ExecuteAction(request.Name, request.Parameter, request.ProcessInstance);
51+
52+
return Ok(ApiResponse.Ok());
53+
}
54+
55+
/// <summary>
56+
/// Returns a list of conditions that can be executed on the callback server.
57+
/// When the Workflow Server should execute a condition with a certain name,
58+
/// it calls the condition execution method on the server that has returned
59+
/// this name in response to the request for the list of conditions.
60+
/// </summary>
61+
[HttpGet]
62+
public Task<IActionResult> GetConditions(string schemeCode, string? token = null)
63+
{
64+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok(_conditionProvider.ConditionNames)));
65+
}
66+
67+
/// <summary>
68+
/// Request to execute the condition. Which of the callback servers
69+
/// is called to execute this method, depends on the list of conditions returned.
70+
/// </summary>
71+
[HttpPost]
72+
public async Task<IActionResult> ExecuteCondition(InstanceRequest request)
73+
{
74+
var result = await _conditionProvider.ExecuteCondition(request.Name, request.Parameter, request.ProcessInstance);
75+
76+
return Ok(ApiResponse.Ok(result));
77+
}
78+
79+
private readonly ActionProvider _actionProvider;
80+
private readonly ConditionProvider _conditionProvider;
81+
82+
#endregion
83+
84+
#region Authorization Rules Execution
85+
86+
/// <summary>
87+
/// Returns a list of Authorization Rules that can be executed on the callback server.
88+
/// When the Workflow Server should execute an Authorization Rule with a certain name,
89+
/// it calls the Authorization Rule execution method on the server that has returned
90+
/// this name in response to the request for the list of Authorization Rules.
91+
/// </summary>
92+
[HttpGet]
93+
public Task<IActionResult> GetRules(string schemeCode, string? token = null)
94+
{
95+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok(_identityProvider.RuleNames)));
96+
}
97+
98+
/// <summary>
99+
/// Check if the User has the access to the command.
100+
/// </summary>
101+
[HttpPost]
102+
public async Task<IActionResult> CheckRule(CheckRuleRequest request)
103+
{
104+
var result = await _identityProvider.RuleCheck(request.Name, request.IdentityId, request.Parameter, request.ProcessInstance);
105+
return Ok(ApiResponse.Ok(result));
106+
}
107+
108+
/// <summary>
109+
/// Returns the list of all Users who have the access.
110+
/// </summary>
111+
[HttpPost]
112+
public async Task<IActionResult> GetIdentities(InstanceRequest request)
113+
{
114+
var result = await _identityProvider.RuleGet(request.Name, request.Parameter, request.ProcessInstance);
115+
return Ok(ApiResponse.Ok(result));
116+
}
117+
118+
private readonly IdentityProvider _identityProvider;
119+
120+
#endregion
121+
122+
#region Remote Scheme Generation
123+
124+
/// <summary>
125+
/// Workflow Engine (is a part of WorkflowServer) supports scheme generation. If you include this method, then,
126+
/// to initialize a new scheme, the Workflow Server calls it on all the connected
127+
/// servers with the settings containing the address of this method.
128+
/// </summary>
129+
[HttpPost]
130+
public Task<IActionResult> Generate(GenerateRequest request)
131+
{
132+
XmlDocument xml = new XmlDocument();
133+
134+
//Your code
135+
136+
return Task.FromResult<IActionResult>(Ok(xml.OuterXml));
137+
}
138+
139+
#endregion
140+
141+
#region Event Handlers
142+
143+
/// <summary>
144+
/// The ProcessStatusChanged event is called only after switching to
145+
/// the statuses of Idled and Finalized. It is not called for subprocesses.
146+
/// </summary>
147+
[HttpPost]
148+
public Task<IActionResult> ProcessStatusChanged(StatusChangedRequest request)
149+
{
150+
_logger.LogInformation("Process status changed from {OldStatus} to {NewStatus}",
151+
request.OldStatus, request.NewStatus);
152+
153+
//Your event actions
154+
155+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok()));
156+
}
157+
158+
/// <summary>
159+
/// The ProcessActivityChanged event.
160+
/// </summary>
161+
[HttpPost]
162+
public Task<IActionResult> ProcessActivityChanged(ActivityChangedRequest request)
163+
{
164+
_logger.LogInformation("Process activity changed from {PreviousActivityName} to {CurrentActivityName}",
165+
request.PreviousActivityName, request.CurrentActivityName);
166+
167+
//Your event actions
168+
169+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok()));
170+
}
171+
172+
/// <summary>
173+
/// The processLogs event.
174+
/// </summary>
175+
[HttpPost]
176+
public Task<IActionResult> ProcessLog(LogRequest request)
177+
{
178+
//Your event actions
179+
180+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok()));
181+
}
182+
183+
#endregion
184+
185+
#region Parameters Providing
186+
187+
/// <summary>
188+
/// Used to get parameters list.
189+
/// </summary>
190+
[HttpGet]
191+
public Task<IActionResult> GetParameterNames(string schemeCode, string? token = null)
192+
{
193+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok(_parameterProvider.GetNames)));
194+
}
195+
196+
/// <summary>
197+
/// The method returns a JSON serialized parameter value.
198+
/// </summary>
199+
[HttpPost]
200+
public Task<IActionResult> GetParameter(ParameterRequest request)
201+
{
202+
var result = _parameterProvider.GetParameter(request.Name);
203+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok(result)));
204+
}
205+
206+
/// <summary>
207+
/// Used for set parameter value.
208+
/// </summary>
209+
[HttpPost]
210+
public Task<IActionResult> SetParameter(ParameterRequest request)
211+
{
212+
_parameterProvider.SetParameter(request.Name, request.Value);
213+
return Task.FromResult<IActionResult>(Ok(ApiResponse.Ok()));
214+
}
215+
216+
private readonly ParameterProvider _parameterProvider;
217+
218+
#endregion
219+
220+
private readonly ILogger<ApiController> _logger;
221+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace WorkflowServer.CallbackApi.Models;
2+
3+
public class ActivityChangedRequest : ProcessRequest
4+
{
5+
public ActivityChangedRequest(Guid processId, string schemeCode, ProcessInstance processInstance, string previousActivityName,
6+
string currentActivityName, string? token = null)
7+
: base(processId, schemeCode, processInstance, token)
8+
{
9+
PreviousActivityName = previousActivityName;
10+
CurrentActivityName = currentActivityName;
11+
}
12+
13+
public string PreviousActivityName { get; }
14+
public string CurrentActivityName { get; }
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace WorkflowServer.CallbackApi.Models;
2+
3+
public class ApiResponse
4+
{
5+
public static ApiResponse Ok(object? data = null) => new ApiResponse(true, data);
6+
public static ApiResponse Failure(string? error = null, string? message = null) => new ApiResponse(false, null, error, message);
7+
8+
public ApiResponse(bool success, object? data = null, string? error = null, string? message = null)
9+
{
10+
Success = success;
11+
Data = data;
12+
Error = error;
13+
}
14+
15+
public string? Error { get; set; }
16+
public string? Message { get; set; }
17+
public bool Success { get; set; }
18+
public object? Data { get; set; }
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace WorkflowServer.CallbackApi.Models;
2+
3+
public class CheckRuleRequest : InstanceRequest
4+
{
5+
public CheckRuleRequest(string name, string parameter, ProcessInstance processInstance, string? identityId = null, string? token = null) :
6+
base(name, parameter, processInstance, token)
7+
{
8+
IdentityId = identityId;
9+
}
10+
11+
public string? IdentityId { get; }
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace WorkflowServer.CallbackApi.Models;
2+
3+
public class GenerateRequest
4+
{
5+
public GenerateRequest(string schemeCode, Guid schemeId, object parameters, string scheme, string? token = null)
6+
{
7+
SchemeCode = schemeCode;
8+
SchemeId = schemeId;
9+
Parameters = parameters;
10+
Scheme = scheme;
11+
Token = token;
12+
}
13+
14+
public string SchemeCode { get; }
15+
public Guid SchemeId { get; }
16+
public object Parameters { get; }
17+
public string Scheme { get; }
18+
public string? Token { get; }
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace WorkflowServer.CallbackApi.Models;
2+
3+
public class InstanceRequest
4+
{
5+
public InstanceRequest(string name, string parameter, ProcessInstance processInstance, string? token = null)
6+
{
7+
Name = name;
8+
Parameter = parameter;
9+
Token = token;
10+
ProcessInstance = processInstance;
11+
}
12+
13+
public string Name { get; }
14+
public string Parameter { get; }
15+
public ProcessInstance ProcessInstance { get; }
16+
public string? Token { get; }
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace WorkflowServer.CallbackApi.Models;
2+
3+
public class LogRequest
4+
{
5+
public LogRequest(List<object> processLogs, string? token = null)
6+
{
7+
ProcessLogs = processLogs;
8+
Token = token;
9+
}
10+
11+
public List<object> ProcessLogs { get; }
12+
public string? Token { get; }
13+
}

0 commit comments

Comments
 (0)