Last active
October 31, 2024 04:08
-
-
Save justinyoo/cb04844306f9a44dcdcaaa70a6a55326 to your computer and use it in GitHub Desktop.
WebSub to EventGrid via CloudEvents and Beyond
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
[FunctionName("CallbackAsync")] | |
public async Task<IActionResult> CallbackAsync( | |
[HttpTrigger(AuthorizationLevel.Function, "GET", "POST", Route = "callback")] HttpRequest req, | |
ILogger log) | |
{ | |
if (HttpMethods.IsGet(req.Method)) | |
{ | |
string challenge = req.Query["hub.challenge"]; | |
var result = new ObjectResult(challenge) { StatusCode = 200 }; | |
return result; | |
} |
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
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns="http://www.w3.org/2005/Atom"> | |
<link rel="hub" href="https://pubsubhubbub.appspot.com"/> | |
<link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=[channel_id]"/> | |
<title>YouTube video feed</title> | |
<updated>2021-01-27T07:00:00.123456789+00:00</updated> | |
<entry> | |
<id>yt:video:[video_id]</id> | |
<yt:videoId>[video_id]</yt:videoId> | |
<yt:channelId>[channel_id]</yt:channelId> | |
<title>hello world</title> | |
<link rel="alternate" href="http://www.youtube.com/watch?v=<video_id>"/> | |
<author> | |
<name>My Channel</name> | |
<uri>http://www.youtube.com/channel/[channel_id]</uri> | |
</author> | |
<published>2021-01-27T07:00:00+00:00</published> | |
<updated>2021-01-27T07:00:00.123456789+00:00</updated> | |
</entry> | |
</feed> |
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
var payload = default(string); | |
using (var reader = new StreamReader(req.Body)) | |
{ | |
payload = await reader.ReadToEndAsync().ConfigureAwait(false); | |
} |
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
Link: <https://pubsubhubbub.appspot.com>; rel=hub, <https://www.youtube.com/xml/feeds/videos.xml?channel_id=[channel_id]>; rel=self |
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
var headers = req.Headers.ToDictionary(p => p.Key, p => string.Join("|", p.Value)); | |
var links = headers["Link"] | |
.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(p => p.Trim().Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) | |
.ToDictionary(p => p.Last().Trim(), p => p.First().Trim().Replace("<", string.Empty).Replace(">", string.Empty)); | |
var source = links["rel=self"]; |
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
var type = "com.youtube.video.published"; | |
var contentType = "application/cloudevents+json"; |
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
var @event = new CloudEvent(source, type, payload, contentType); | |
var events = new List<CloudEvent>() { @event }; | |
var topicEndpoint = new Uri("https://<eventgrid_name>.<location>-<random_number>.eventgrid.azure.net/api/events"); | |
var credential = new AzureKeyCredential("eventgrid_topic_access_key"); | |
var publisher = new EventGridPublisherClient(topicEndpoint, credential); | |
var response = await publisher.SendEventsAsync(events).ConfigureAwait(false); | |
return new StatusCodeResult(response.Status); | |
} |
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
{ | |
"id": "c2e9b2d1-802c-429d-b772-046230a9261e", | |
"source": "https://www.youtube.com/xml/feeds/videos.xml?channel_id=<channel_id>", | |
"data": "<websub_xml_data>", | |
"type": "com.youtube.video.published", | |
"time": "2021-01-27T07:00:00.123456Z", | |
"specversion": "1.0", | |
"datacontenttype": "application/cloudevents+json", | |
"traceparent": "00-37d33dfa0d909047b8215349776d7268-809f0432fbdfd94b-00" | |
} |
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
{ | |
"channelId": "<channel_id>", | |
"videoId": "<video_id>", | |
"title": "hello world", | |
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis malesuada.", | |
"link": "https://www.youtube.com/watch?v=<video_id>", | |
"thumbnailLink": "https://i.ytimg.com/vi/<video_id>/maxresdefault.jpg", | |
"datePublished": "2021-01-27T07:00:00+00:00", | |
"dateUpdated": "2021-01-27T07:00:00+00:00" | |
} |
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
{ | |
"id": "4cee6312-6584-462f-a8c0-c3d5d0cbfcb1", | |
"specversion": "1.0", | |
"source": "https://www.youtube.com/xml/feeds/videos.xml?channel_id=<channel_id>", | |
"type": "com.youtube.video.converted", | |
"time": "2021-01-16T05:21:23.9068402Z", | |
"datacontenttype": "application/cloudevents+json", | |
"data": { | |
"channelId": "<channel_id>", | |
"videoId": "<video_id>", | |
"title": "hello world", | |
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis malesuada.", | |
"link": "https://www.youtube.com/watch?v=<video_id>", | |
"thumbnailLink": "https://i.ytimg.com/vi/<video_id>/maxresdefault.jpg", | |
"datePublished": "2021-01-27T07:00:00+00:00", | |
"dateUpdated": "2021-01-27T07:00:00+00:00" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment