Skip to content

Commit

Permalink
added polling
Browse files Browse the repository at this point in the history
  • Loading branch information
bZverok committed Jun 27, 2024
1 parent c70a1a3 commit d946950
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Apps.XTM/Apps.XTM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Product>XTM</Product>
<Version>3.1.5</Version>
<Version>3.2.0</Version>
<Description>Translation management system that centralizes localization assets and enables fast, accurate translation and deployment of content tailored to any audience in any territory</Description>
<AssemblyName>Apps.XTM</AssemblyName>
<LangVersion>12</LangVersion>
Expand All @@ -18,8 +18,7 @@
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.*"/>
<PackageReference Include="System.ServiceModel.Primitives" Version="8.0.0"/>
<PackageReference Include="System.ServiceModel.Security" Version="4.10.*"/>
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.6.0"/>
<PackageReference Include="Blackbird.Applications.Sdk.Utils" Version="1.0.17"/>
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.8.0"/>
</ItemGroup>
<ItemGroup>
<EmbeddedResource CopyToOutputDirectory="Always" Include="image\icon.png"/>
Expand Down
16 changes: 16 additions & 0 deletions Apps.XTM/DataSourceHandlers/EnumHandlers/ProjectStatusHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Blackbird.Applications.Sdk.Common.Dictionaries;

namespace Apps.XTM.DataSourceHandlers.EnumHandlers;

public class ProjectStatusHandler : IStaticDataSourceHandler
{
public Dictionary<string, string> GetData()
{
return new()
{
["NOT_STARTED"] = "Not started",
["STARTED"] = "Started",
["FINISHED"] = "Finished"
};
}
}
2 changes: 2 additions & 0 deletions Apps.XTM/Models/Response/Projects/FullProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public class FullProject
[Display("Target languages")] public IEnumerable<string> TargetLanguages { get; set; }

[Display("Template ID")] public string TemplateId { get; set; }

public string Status { get; set; }
}
6 changes: 6 additions & 0 deletions Apps.XTM/Polling/Models/Memory/DateMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Apps.XTM.Polling.Models.Memory;

public class DateMemory
{
public DateTime LastInteractionDate { get; set; }
}
6 changes: 6 additions & 0 deletions Apps.XTM/Polling/Models/Memory/ProjectStatusMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Apps.XTM.Polling.Models.Memory;

public class ProjectStatusMemory
{
public string Status { get; set; }
}
129 changes: 129 additions & 0 deletions Apps.XTM/Polling/PollingList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System.Globalization;
using Apps.XTM.Constants;
using Apps.XTM.DataSourceHandlers.EnumHandlers;
using Apps.XTM.Invocables;
using Apps.XTM.Models.Request.Projects;
using Apps.XTM.Models.Response.Projects;
using Apps.XTM.Polling.Models.Memory;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Dictionaries;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.Sdk.Common.Polling;
using RestSharp;

namespace Apps.XTM.Polling;

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

[PollingEvent("On projects created", "On any new projects created")]
public Task<PollingEventResponse<DateMemory, ListProjectsResponse>> OnProjectsCreated(
PollingEventRequest<DateMemory> request) => ProcessProjectsPolling(request,
$"createdDateFrom={request.Memory?.LastInteractionDate.ToString("o", CultureInfo.InvariantCulture)}");

[PollingEvent("On projects updated", "On any projects are updated")]
public Task<PollingEventResponse<DateMemory, ListProjectsResponse>> OnProjectsUpdated(
PollingEventRequest<DateMemory> request) => ProcessProjectsPolling(request,
$"modifiedDateFrom={request.Memory?.LastInteractionDate.ToString("o", CultureInfo.InvariantCulture)}");

[PollingEvent("On projects finished", "On any projects are finished")]
public Task<PollingEventResponse<DateMemory, ListProjectsResponse>> OnProjectsFinished(
PollingEventRequest<DateMemory> request) => ProcessProjectsPolling(request,
$"finishedDateFrom={request.Memory?.LastInteractionDate.ToString("o", CultureInfo.InvariantCulture)}");

[PollingEvent("On project status changed", "On status of the specific project changed")]
public async Task<PollingEventResponse<ProjectStatusMemory, SimpleProject>> OnProjectStatusChanged(
PollingEventRequest<ProjectStatusMemory> request,
[PollingEventParameter] ProjectRequest project,
[PollingEventParameter] [Display("Project status")] [StaticDataSource(typeof(ProjectStatusHandler))]
string? status)
{
var projectEntity = (await Client.ExecuteXtmWithJson<List<SimpleProject>>(
$"{ApiEndpoints.Projects}?ids={project.ProjectId}",
Method.Get,
null,
Creds)).FirstOrDefault() ?? throw new InvalidOperationException("Not project found with the provided ID");

if (request.Memory is null)
{
return new()
{
FlyBird = false,
Memory = new()
{
Status = projectEntity.Status
}
};
}

return new()
{
FlyBird = request.Memory.Status != projectEntity.Status &&
(status == null || projectEntity.Status == status),
Result = projectEntity,
Memory = new()
{
Status = projectEntity.Status
}
};
}

private async Task<PollingEventResponse<DateMemory, ListProjectsResponse>> ProcessProjectsPolling(
PollingEventRequest<DateMemory> request,
string query)
{
if (request.Memory is null)
{
return new()
{
FlyBird = false,
Memory = new()
{
LastInteractionDate = DateTime.UtcNow
}
};
}

var result = new List<SimpleProject>();
var page = 1;

List<SimpleProject> response;
do
{
response = await Client.ExecuteXtmWithJson<List<SimpleProject>>(
$"{ApiEndpoints.Projects}?{query}&page={page}",
Method.Get,
null,
Creds);
result.AddRange(response);

page++;
} while (response.Any());

if (!result.Any())
{
return new()
{
FlyBird = false,
Memory = new()
{
LastInteractionDate = DateTime.UtcNow
}
};
}

return new()
{
FlyBird = true,
Result = new(result),
Memory = new()
{
LastInteractionDate = DateTime.UtcNow
}
};
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Before you can connect you need to make sure that:
- **On project accepted**
- **On project finished**
- **On invoice status changed**
- **On projects created**
- **On project status changed**

## Missing features

Expand Down

0 comments on commit d946950

Please sign in to comment.