Created
May 3, 2022 21:47
-
-
Save deanebarker/c6565f591bdcdaa27d9ffe4f0cc10156 to your computer and use it in GitHub Desktop.
The simplest webhook system for Optimizely Content Cloud
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
public void Configuration(IAppBuilder app) | |
{ | |
// This is the URL template to call | |
// The placeholders are optional -- they will be replaced with data specific to the content that is published | |
var webhookUrl = "https://example.com?id={id}&type={type}&version={version}"; | |
// Here is an example URL that would be called for this example: | |
// https://example.com?id=17&type=ArticlePage&version=691 | |
var events = ServiceLocator.Current.GetInstance<IContentEvents>(); | |
events.PublishedContent += (s, e) => | |
{ | |
// Get the type name | |
var typeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>(); | |
var typeName = typeRepository.Load(e.Content.ContentTypeID); | |
// Form the specific URL for *this* webhook call | |
var url = webhookUrl | |
.Replace("{id}", e.ContentLink.ID.ToString()) | |
.Replace("{type}", typeName.Name) | |
.Replace("{version}", e.ContentLink.WorkID.ToString()); | |
// Call it in a new thread | |
// Note: errors will be swallowed; implement logging as you see fit | |
Task.Run(() => | |
{ | |
var webClient = new WebClient(); | |
webClient.DownloadStringAsync(new Uri(url)); | |
}); | |
}; | |
// More startup code here... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment