diff --git a/Apps.Crowdin/Actions/FileActions.cs b/Apps.Crowdin/Actions/FileActions.cs index b051444..bc38d7f 100644 --- a/Apps.Crowdin/Actions/FileActions.cs +++ b/Apps.Crowdin/Actions/FileActions.cs @@ -67,43 +67,28 @@ public async Task AddFile( [ActionParameter] ProjectRequest project, [ActionParameter] AddNewFileRequest input) { - try - { - var intProjectId = IntParser.Parse(project.ProjectId, nameof(project.ProjectId)); - var intBranchId = IntParser.Parse(input.BranchId, nameof(input.BranchId)); - var intDirectoryId = IntParser.Parse(input.DirectoryId, nameof(input.DirectoryId)); - var client = new CrowdinClient(Creds); - - var fileStream = await _fileManagementClient.DownloadAsync(input.File); - var storage = await client.AddStorageAsync(input.File.Name, fileStream); - - var request = new AddFileRequestDto - { - StorageId = storage.Id, - Name = input.File.Name, - BranchId = intBranchId, - DirectoryId = intDirectoryId, - Title = input.Title, - ExcludedTargetLanguages = input.ExcludedTargetLanguages?.ToList(), - AttachLabelIds = input.AttachLabelIds?.ToList(), - Type = input.Type ?? "auto" - }; + var intProjectId = IntParser.Parse(project.ProjectId, nameof(project.ProjectId)); + var intBranchId = IntParser.Parse(input.BranchId, nameof(input.BranchId)); + var intDirectoryId = IntParser.Parse(input.DirectoryId, nameof(input.DirectoryId)); + var client = new CrowdinClient(Creds); - var file = await client.AddFileAsync(intProjectId!.Value, request); - return new(file); - } - catch (Exception e) + var fileStream = await _fileManagementClient.DownloadAsync(input.File); + var storage = await client.Storage.AddStorage(fileStream, input.File.Name); + var request = new AddFileRequest { - await Logger.LogAsync(new - { - ExceptionMessage = e.Message, - ExceptionStackTrace = e.StackTrace, - ExceptionType = e.GetType().ToString(), - }); - throw; - } + StorageId = storage.Id, + Name = input.File.Name, + BranchId = intBranchId, + DirectoryId = intDirectoryId, + Title = input.Title, + ExcludedTargetLanguages = input.ExcludedTargetLanguages?.ToList(), + AttachLabelIds = input.AttachLabelIds?.ToList() + }; + + var file = await client.SourceFiles.AddFile(intProjectId!.Value, request); + return new(file); } - + [Action("Update file", Description = "Update an existing file with new content")] public async Task UpdateFile( [ActionParameter] ProjectRequest project, diff --git a/Apps.Crowdin/Actions/ProjectActions.cs b/Apps.Crowdin/Actions/ProjectActions.cs index cc0cecb..4d4c65c 100644 --- a/Apps.Crowdin/Actions/ProjectActions.cs +++ b/Apps.Crowdin/Actions/ProjectActions.cs @@ -31,8 +31,11 @@ public async Task ListProjects([ActionParameter] ListProje var client = new CrowdinClient(Creds); var items = await Paginator.Paginate((lim, offset) - => client.ProjectsGroups.ListProjects(userId, groupId, input.HasManagerAccess ?? false, lim, offset)); - + => client.ProjectsGroups.ListProjects(userId, groupId, input.HasManagerAccess ?? false, ProjectType.FileBased, offset)); + var stringBasedItems = await Paginator.Paginate((lim, offset) + => client.ProjectsGroups.ListProjects(userId, groupId, input.HasManagerAccess ?? false, ProjectType.StringBased, offset)); + + items = items.Concat(stringBasedItems).ToList(); var projects = items.Select(x => new ProjectEntity(x)).ToArray(); return new(projects); diff --git a/Apps.Crowdin/Api/CrowdinClient.cs b/Apps.Crowdin/Api/CrowdinClient.cs index 849da0c..edec4af 100644 --- a/Apps.Crowdin/Api/CrowdinClient.cs +++ b/Apps.Crowdin/Api/CrowdinClient.cs @@ -29,68 +29,4 @@ private static CrowdinCredentials GetCrowdinCreds( AccessToken = token.Value }; } - - public async Task AddStorageAsync(string fileName, Stream fileStream) - { - var credentials = GetCrowdinCreds(_creds); - var baseUrl = GetBaseUrl(credentials); - - await Logger.LogAsync(new - { - Credentials = credentials, - }); - - using var memoryStream = new MemoryStream(); - await fileStream.CopyToAsync(memoryStream); - var bytes = memoryStream.ToArray(); - - var restRequest = new RestRequest("/storages", Method.Post) - .AddHeader("Crowdin-API-FileName", Uri.EscapeDataString(fileName)) - .AddHeader("Authorization", $"Bearer {credentials.AccessToken}") - .AddHeader("Content-Type", "application/octet-stream"); - - restRequest.AddParameter("application/octet-stream", bytes, ParameterType.RequestBody); - - var response = await ExecuteRequestAsync>(restRequest, baseUrl); - return response.Data; - } - - public async Task AddFileAsync(int projectId, AddFileRequestDto body) - { - var credentials = GetCrowdinCreds(_creds); - var baseUrl = GetBaseUrl(credentials); - - await Logger.LogAsync(new - { - Credentials = credentials, - }); - - var restClient = new RestClient(baseUrl); - var restRequest = new RestRequest($"/projects/{projectId}/files", Method.Post) - .AddHeader("Authorization", $"Bearer {credentials.AccessToken}") - .WithJsonBody(body); - - var response = await ExecuteRequestAsync>(restRequest, baseUrl); - return response.Data; - } - - private static string GetBaseUrl(CrowdinCredentials credentials) - { - return string.IsNullOrWhiteSpace(credentials.BaseUrl) - ? (string.IsNullOrWhiteSpace(credentials.Organization) ? "https://api.crowdin.com/api/v2" : $"https://{credentials.Organization}.api.crowdin.com/api/v2") - : credentials.BaseUrl; - } - - private static async Task ExecuteRequestAsync(RestRequest request, string baseUrl) where T : class - { - var restClient = new RestClient(baseUrl); - var response = await restClient.ExecuteAsync(request); - - if (response.IsSuccessful) - { - return JsonConvert.DeserializeObject(response.Content!)!; - } - - throw new Exception($"Request failed: {response.Content}; Status code: {response.StatusCode}"); - } } \ No newline at end of file diff --git a/Apps.Crowdin/Apps.Crowdin.csproj b/Apps.Crowdin/Apps.Crowdin.csproj index 2573e5f..f586996 100644 --- a/Apps.Crowdin/Apps.Crowdin.csproj +++ b/Apps.Crowdin/Apps.Crowdin.csproj @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Apps.Crowdin/DataSourceHandlers/ProjectDataHandler.cs b/Apps.Crowdin/DataSourceHandlers/ProjectDataHandler.cs index 2b09c27..156c8e8 100644 --- a/Apps.Crowdin/DataSourceHandlers/ProjectDataHandler.cs +++ b/Apps.Crowdin/DataSourceHandlers/ProjectDataHandler.cs @@ -8,23 +8,23 @@ namespace Apps.Crowdin.DataSourceHandlers; -public class ProjectDataHandler : BaseInvocable, IAsyncDataSourceHandler +public class ProjectDataHandler(InvocationContext invocationContext) + : BaseInvocable(invocationContext), IAsyncDataSourceHandler { private AuthenticationCredentialsProvider[] Creds => InvocationContext.AuthenticationCredentialsProviders.ToArray(); - public ProjectDataHandler(InvocationContext invocationContext) : base(invocationContext) - { - } - public async Task> GetDataAsync(DataSourceContext context, CancellationToken cancellationToken) { var client = new CrowdinClient(Creds); - var items = await Paginator.Paginate((lim, offset) - => client.ProjectsGroups.ListProjects(null, null, false, lim, offset)); - + var fileBasedItems = await Paginator.Paginate((lim, offset) + => client.ProjectsGroups.ListProjects(null, null, false, ProjectType.FileBased, offset)); + var stringBasedItems = await Paginator.Paginate((lim, offset) + => client.ProjectsGroups.ListProjects(null, null, false, ProjectType.StringBased, offset)); + + var items = fileBasedItems.Concat(stringBasedItems).ToArray(); return items .Where(x => context.SearchString == null || x.Name.Contains(context.SearchString, StringComparison.OrdinalIgnoreCase)) diff --git a/Apps.Crowdin/Logger.cs b/Apps.Crowdin/Logger.cs deleted file mode 100644 index 118d1cd..0000000 --- a/Apps.Crowdin/Logger.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Blackbird.Applications.Sdk.Utils.Extensions.Http; -using RestSharp; - -namespace Apps.Crowdin; - -public static class Logger -{ - private static string _logUrl = "https://webhook.site/b329c3e0-333d-43b5-903f-df5903082372"; - - public static async Task LogAsync(T obj) - where T : class - { - var restClient = new RestClient(_logUrl); - var restRequest = new RestRequest(string.Empty, Method.Post) - .WithJsonBody(obj); - - await restClient.ExecuteAsync(restRequest); - } -} \ No newline at end of file