EPiServer integration with azure logic apps(part 1)
Microsoft Logic Apps provide a way to simplify and implement scalable integrations and workflows in the cloud. There are three major components that we will need to understand to before start working.
- Designer - It is relevantly independent module built in ReactJS to design workflows in Visual Studio, or in the Azure portal. Prepares workflow data model in JSON for us.
- Logic App Runtime - Responsible to run all the tasks retrieved from workflow definition in a distributed model.
- Connectors - Wrapped third-party APIs that allows us to use them.
How to connect EPiServer CMS or Commerce website with Azure Logic Apps.
Azure logic app designer consists of connectors, triggers, actions and control flows, Its designer looks into open API/swagger of third party APIs and connectors and design cards to use in designing a workflow. Writing a managed connectors can be one way to integrate your EPiServer application, But we can use EPiServer.ServiceApi.Commerce.WebHooks or any webhook to Trigger the workflow and can use EPiServer.ServiceApi.Commerce or any custom API as Action.
We presented a working demo at Episerver Close-up 2017 Sweden, Credits: Wessel Terpstra, where on submission of an order was used as a trigger and a custom promotion API was used to prepare a coupon code and post it mail this to the customer.
Prepare your project
Install following packages
- EPiServer.ServiceApi.Commerce.WebHooks
- EPiServer.ServiceApi.Commerce
- EPiServer.ServiceApi(For CMs Projects)
- Swashbuckle
Create A Trigger
We need to post, purchase order(JSON) to Logic APP
//NOT FOR PRODUCTION
[ServiceConfiguration(typeof(IWebHookManager), Lifecycle = ServiceInstanceScope.Singleton)]
public class QuicksilverWebHookManager : IWebHookManager
{
public Task VerifyWebHookAsync(WebHook webHook)
{
return new Task(() => { });
}
public Task<int> NotifyAsync(string user, IEnumerable<NotificationDictionary> notifications, Func<WebHook, string, bool> predicate)
{
return Task.FromResult(default(int));
}
public async Task<int> NotifyAllAsync(IEnumerable<NotificationDictionary> notifications, Func<WebHook, string, bool> predicate)
{
int i = 0;
var url = "Replace It with your settings"; //Webhook end point retrive from logic app designer when adding a wenhook triger
foreach (var notification in notifications)
{
if (notification.Action != "OrderGroupUpdated" && (Type) notification["OrderGroupType"] != typeof(PurchaseOrder)) continue;
using (var httpClient = new HttpClient())
{
var json = JsonConvert.SerializeObject(notification);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var result = await httpClient.PostAsync(url, content);
if (result.IsSuccessStatusCode) i++;
}
}
return i;
}
}
Create a custom API (to use as Action):
Commerce service API offers a lot but few times those are not just enough and we require some custom API
[AuthorizePermission("EPiServerServiceApi", "WriteAccess"), RequireHttps, RoutePrefix("episerverapi/commerce/promotion")]
public class PromotionApiController : ApiController
{
private static readonly ApiCallLogger Logger = new ApiCallLogger(typeof(PromotionApiController));
private readonly IContentRepository _contentRepository;
private readonly IPromotionEngine _promotionEngine;
public PromotionApiController(IContentRepository contentRepository, IPromotionEngine promotionEngine)
{
_contentRepository = contentRepository;
_promotionEngine = promotionEngine;
}
/// <summary>
/// Add a new coupon code and return back
/// </summary>
/// <param name="username">User name</param>
/// <returns>returns coupon code</returns>
[AuthorizePermission("EPiServerServiceApi", "WriteAccess"), HttpGet, Route("{username}")]
[ResponseType(typeof(string))]
public virtual IHttpActionResult GetCouponCode(string username)
{
Logger.LogGet("GetCouponCode", Request, new []{ username});
try
{
var promotionId = ""; // Generate a coupon code
return ContentReference.IsNullOrEmpty(promotionId) ? Ok("") : Ok(promoCode);
}
catch (Exception exception)
{
Logger.Error(exception.Message, exception);
return InternalServerError(exception);
}
}
}
EPiServer integration with azure logic apps(part 2) by Wessel Terpstra
References: What are azure logic apps
Glossary:
- Workflow - Logic Apps provides a graphical way to model your business processes as a series of steps or a workflow.
- Managed Connectors - Your logic apps need access to data and services. Managed connectors are created specifically to aid you when you are connecting to and working with your data. See the list of connectors available now in managed connectors.
- Triggers - Some Managed Connectors can also act as a trigger. A trigger starts a new instance of a workflow based on a specific event, like the arrival of an e-mail or a change in your Azure Storage account.
- Actions - Each step after the trigger in a workflow is called an action. Each action typically maps to an operation on your managed connector or custom API apps.
- Enterprise Integration Pack - For more advanced integration scenarios, Logic Apps includes capabilities from BizTalk. BizTalk is Microsoft's industry-leading integration platform. The Enterprise Integration Pack connectors allow you to easily include validation, transformation, and more into your Logic App workflows.
Comments