Skip to content

Commit

Permalink
Increased version of nuget
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed Jun 18, 2024
1 parent 7a076ec commit 0333eee
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 128 deletions.
53 changes: 19 additions & 34 deletions Apps.Crowdin/Actions/FileActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,28 @@ public async Task<FileEntity> 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<FileEntity> UpdateFile(
[ActionParameter] ProjectRequest project,
Expand Down
7 changes: 5 additions & 2 deletions Apps.Crowdin/Actions/ProjectActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public async Task<ListProjectsResponse> ListProjects([ActionParameter] ListProje
var client = new CrowdinClient(Creds);

var items = await Paginator.Paginate((lim, offset)
=> client.ProjectsGroups.ListProjects<ProjectBase>(userId, groupId, input.HasManagerAccess ?? false, lim, offset));

=> client.ProjectsGroups.ListProjects<ProjectBase>(userId, groupId, input.HasManagerAccess ?? false, ProjectType.FileBased, offset));
var stringBasedItems = await Paginator.Paginate((lim, offset)
=> client.ProjectsGroups.ListProjects<ProjectBase>(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);
Expand Down
64 changes: 0 additions & 64 deletions Apps.Crowdin/Api/CrowdinClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,68 +29,4 @@ private static CrowdinCredentials GetCrowdinCreds(
AccessToken = token.Value
};
}

public async Task<StorageResourceDto> 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<DataWrapper<StorageResourceDto>>(restRequest, baseUrl);
return response.Data;
}

public async Task<FileDto> 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<DataWrapper<FileDto>>(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<T> ExecuteRequestAsync<T>(RestRequest request, string baseUrl) where T : class
{
var restClient = new RestClient(baseUrl);
var response = await restClient.ExecuteAsync(request);

if (response.IsSuccessful)
{
return JsonConvert.DeserializeObject<T>(response.Content!)!;
}

throw new Exception($"Request failed: {response.Content}; Status code: {response.StatusCode}");
}
}
2 changes: 1 addition & 1 deletion Apps.Crowdin/Apps.Crowdin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.6.0" />
<PackageReference Include="Blackbird.Applications.SDK.Extensions.FileManagement" Version="1.0.1" />
<PackageReference Include="Blackbird.Applications.Sdk.Utils" Version="1.0.25" />
<PackageReference Include="Crowdin.Api" Version="2.22.1" />
<PackageReference Include="Crowdin.Api" Version="2.23.1" />
<PackageReference Include="MimeTypes" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
16 changes: 8 additions & 8 deletions Apps.Crowdin/DataSourceHandlers/ProjectDataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dictionary<string, string>> GetDataAsync(DataSourceContext context,
CancellationToken cancellationToken)
{
var client = new CrowdinClient(Creds);

var items = await Paginator.Paginate((lim, offset)
=> client.ProjectsGroups.ListProjects<ProjectBase>(null, null, false, lim, offset));

var fileBasedItems = await Paginator.Paginate((lim, offset)
=> client.ProjectsGroups.ListProjects<ProjectBase>(null, null, false, ProjectType.FileBased, offset));
var stringBasedItems = await Paginator.Paginate((lim, offset)
=> client.ProjectsGroups.ListProjects<ProjectBase>(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))
Expand Down
19 changes: 0 additions & 19 deletions Apps.Crowdin/Logger.cs

This file was deleted.

0 comments on commit 0333eee

Please sign in to comment.