Skip to content

Commit

Permalink
added polling events
Browse files Browse the repository at this point in the history
  • Loading branch information
bZverok committed Jun 11, 2024
1 parent e334183 commit 4de8042
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Apps.Wordpress/Apps.Wordpress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AssemblyName>Apps.Wordpress</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.6.0" />
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.8.0" />
<PackageReference Include="Blackbird.Applications.SDK.Extensions.FileManagement" Version="1.0.1" />
<PackageReference Include="Blackbird.Applications.Sdk.Utils" Version="1.0.16" />
<PackageReference Include="Blackbird.Applications.Sdk.Utils.Html" Version="1.0.1" />
Expand Down
8 changes: 8 additions & 0 deletions Apps.Wordpress/Events/Polling/Models/ContentPollingResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Apps.Wordpress.Models.Entities;

namespace Apps.Wordpress.Events.Polling.Models;

public class ContentPollingResult
{
public IEnumerable<WordPressItem> Items { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Apps.Wordpress.Events.Polling.Models.Memory;

public class ContentCreatedPollingMemory
{
public DateTime LastCreationDate { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Apps.Wordpress.Events.Polling.Models.Memory;

public class ContentUpdatedPollingMemory
{
public DateTime LastModificationTime { get; set; }
}
164 changes: 164 additions & 0 deletions Apps.Wordpress/Events/Polling/PollingList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using Apps.Wordpress.Api.RestSharp;
using Apps.Wordpress.Constants;
using Apps.Wordpress.DataSourceHandlers;
using Apps.Wordpress.Events.Polling.Models;
using Apps.Wordpress.Events.Polling.Models.Memory;
using Apps.Wordpress.Models.Dtos;
using Apps.Wordpress.Models.Entities;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Dynamic;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.Sdk.Common.Polling;
using Blackbird.Applications.Sdk.Utils.Extensions.String;
using RestSharp;

namespace Apps.Wordpress.Events.Polling;

[PollingEventList]
public class PollingList : BaseInvocable
{
public PollingList(InvocationContext invocationContext) : base(invocationContext)
{
}

[PollingEvent("On post created", "On a new post created")]
public Task<PollingEventResponse<ContentCreatedPollingMemory, ContentPollingResult>> OnPostCreated(
PollingEventRequest<ContentCreatedPollingMemory> request)
=> PollContentCreation(request, new()
{
["after"] = request.Memory?.LastCreationDate.ToString(Formats.ISO8601) ?? string.Empty
}, "posts", () => new()
{
LastCreationDate = DateTime.UtcNow
});


[PollingEvent("On post updated", "On a specific post updated")]
public Task<PollingEventResponse<ContentUpdatedPollingMemory, ContentPollingResult>> OnPostUpdated(
PollingEventRequest<ContentUpdatedPollingMemory> request,
[PollingEventParameter] [Display("Post ID")] [DataSource(typeof(PostDataHandler))]
string? postId)
=> PollContentChanges(request, postId, new()
{
["modified_after"] = request.Memory?.LastModificationTime.ToString(Formats.ISO8601) ?? string.Empty
}, "posts", () => new()
{
LastModificationTime = DateTime.UtcNow
});

[PollingEvent("On page created", "On a new page created")]
public Task<PollingEventResponse<ContentCreatedPollingMemory, ContentPollingResult>> OnPageCreated(
PollingEventRequest<ContentCreatedPollingMemory> request)
=> PollContentCreation(request, new()
{
["after"] = request.Memory?.LastCreationDate.ToString(Formats.ISO8601) ?? string.Empty
}, "pages", () => new()
{
LastCreationDate = DateTime.UtcNow
});


[PollingEvent("On page updated", "On a specific page updated")]
public Task<PollingEventResponse<ContentUpdatedPollingMemory, ContentPollingResult>> OnPageUpdated(
PollingEventRequest<ContentUpdatedPollingMemory> request,
[PollingEventParameter] [Display("Page ID")] [DataSource(typeof(PageDataHandler))]
string? pageId)
=> PollContentChanges(request, pageId, new()
{
["modified_after"] = request.Memory?.LastModificationTime.ToString(Formats.ISO8601) ?? string.Empty
}, "pages", () => new()
{
LastModificationTime = DateTime.UtcNow
});

private async Task<PollingEventResponse<T, ContentPollingResult>> PollContentCreation<T>(
PollingEventRequest<T> request, Dictionary<string, string> query, string endpoint, Func<T> createMemory)
{
if (request.Memory is null)
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

var items = await ListContentItems(endpoint.WithQuery(query));

if (!items.Any())
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

return new()
{
FlyBird = true,
Result = new()
{
Items = items.Select(x => new WordPressItem(x)).ToList()
},
Memory = createMemory()
};
}

private async Task<PollingEventResponse<T, ContentPollingResult>> PollContentChanges<T>(
PollingEventRequest<T> request, string? resourceId, Dictionary<string, string> query, string endpoint,
Func<T> createMemory) where T : ContentUpdatedPollingMemory
{
if (request.Memory is null)
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

var items = string.IsNullOrWhiteSpace(resourceId)
? await ListContentItems(endpoint.WithQuery(query))
: await GetContentItem($"{endpoint}/{resourceId}", request.Memory);

if (!items.Any())
{
return new()
{
FlyBird = false,
Memory = createMemory()
};
}

return new()
{
FlyBird = true,
Result = new()
{
Items = items.Select(x => new WordPressItem(x)).ToList()
},
Memory = createMemory()
};
}

private Task<List<BaseDto>> ListContentItems(string endpoint)
{
var client = new WordpressRestClient(InvocationContext.AuthenticationCredentialsProviders);
var listRequest =
new WordpressRestRequest(endpoint, Method.Get, InvocationContext.AuthenticationCredentialsProviders);

return client.Paginate<BaseDto>(listRequest);
}

private async Task<List<BaseDto>> GetContentItem(string endpoint, ContentUpdatedPollingMemory memory)
{
var client = new WordpressRestClient(InvocationContext.AuthenticationCredentialsProviders);
var listRequest =
new WordpressRestRequest(endpoint, Method.Get, InvocationContext.AuthenticationCredentialsProviders);

var item = await client.ExecuteWithHandling<BaseDto>(listRequest);

return item.ModifiedGmt > memory.LastModificationTime ? [item] : [];
}
}
2 changes: 1 addition & 1 deletion Apps.Wordpress/Models/Requests/Post/PostRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Apps.Wordpress.Models.Requests.Post
{
public class PostRequest
{
[Display("Post")]
[Display("Post ID")]
[DataSource(typeof(PostDataHandler))]
public string Id { get; set; }
}
Expand Down

0 comments on commit 4de8042

Please sign in to comment.