Skip to content

Commit

Permalink
Added pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed Sep 5, 2024
1 parent 0433760 commit 5268a86
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
6 changes: 1 addition & 5 deletions Apps.XTM/Actions/LqaActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
namespace Apps.XTM.Actions
{
[ActionList]
public class LqaActions : XtmInvocable
public class LqaActions(InvocationContext invocationContext) : XtmInvocable(invocationContext)
{
public LqaActions(InvocationContext invocationContext) : base(invocationContext)
{
}

[Action("Search LQA reports", Description = "Define criteria to search LQA reports")]
public async Task<List<LqaResponse>> SearchLqa([ActionParameter] LQARequest input)
{
Expand Down
48 changes: 33 additions & 15 deletions Apps.XTM/Actions/ProjectActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,43 @@
namespace Apps.XTM.Actions;

[ActionList]
public class ProjectActions : XtmInvocable
public class ProjectActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: XtmInvocable(invocationContext)
{
private readonly IFileManagementClient _fileManagementClient;

public ProjectActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: base(invocationContext)
{
_fileManagementClient = fileManagementClient;
}

private const int PageSize = 100;

[Action("List projects", Description = "List all projects")]
public async Task<ListProjectsResponse> ListProjects()
{
var response = await Client.ExecuteXtmWithJson<List<SimpleProject>>(ApiEndpoints.Projects,
Method.Get,
null,
Creds);
var page = 1;
var hasMorePages = true;
var allProjects = new List<SimpleProject>();

while (hasMorePages)
{
var queryParams = new Dictionary<string, string>
{
{ "page", page.ToString() },
{ "pageSize", PageSize.ToString() }
};

var response = await Client.ExecuteXtmWithJson<List<SimpleProject>?>(ApiEndpoints.Projects,
Method.Get,
queryParams,
Creds);

if (response != null && response.Any())
{
allProjects.AddRange(response);
page++;
}
else
{
hasMorePages = false;
}
}

return new(response);
return new ListProjectsResponse(allProjects);
}

[Action("Get project", Description = "Get project")]
Expand Down Expand Up @@ -209,7 +227,7 @@ public async Task<FileResponse> DownloadMetrics(
var response = await Client.ExecuteXtmWithJson(endpoint, Method.Get, null, Creds);

using var stream = new MemoryStream(response.RawBytes);
var file = await _fileManagementClient.UploadAsync(stream,
var file = await fileManagementClient.UploadAsync(stream,
response.ContentType ?? MediaTypeNames.Application.Octet, $"{project.ProjectId}.xlsx");
return new(file);
}
Expand Down
2 changes: 1 addition & 1 deletion 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.2.3</Version>
<Version>3.2.4</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 Down
19 changes: 6 additions & 13 deletions Apps.XTM/DataSourceHandlers/ProjectDataHandler.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
using Apps.XTM.Constants;
using Apps.XTM.Actions;
using Apps.XTM.Invocables;
using Apps.XTM.Models.Response.Projects;
using Blackbird.Applications.Sdk.Common.Dynamic;
using Blackbird.Applications.Sdk.Common.Invocation;
using RestSharp;

namespace Apps.XTM.DataSourceHandlers;

public class ProjectDataHandler : XtmInvocable, IAsyncDataSourceHandler
public class ProjectDataHandler(InvocationContext invocationContext)
: XtmInvocable(invocationContext), IAsyncDataSourceHandler
{
public ProjectDataHandler(InvocationContext invocationContext) : base(invocationContext)
{
}

public async Task<Dictionary<string, string>> GetDataAsync(DataSourceContext context, CancellationToken cancellationToken)
{
var response = await Client.ExecuteXtmWithJson<List<SimpleProject>>(ApiEndpoints.Projects,
Method.Get,
null,
Creds);
var actions = new ProjectActions(InvocationContext, null!);
var response = await actions.ListProjects();

return response
return response.Projects
.Where(x => context.SearchString == null || x.Name.Contains(context.SearchString, StringComparison.OrdinalIgnoreCase))
.Take(20)
.ToDictionary(x => x.Id, x => x.Name);
Expand Down

0 comments on commit 5268a86

Please sign in to comment.