Skip to content

Commit

Permalink
Added custom calls to endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed Jun 18, 2024
1 parent 730f37d commit 047c056
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 8 deletions.
15 changes: 10 additions & 5 deletions Apps.Crowdin/Actions/FileActions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Apps.Crowdin.Api;
using System.Reflection;
using Apps.Crowdin.Api;
using Apps.Crowdin.Models.Dtos;
using Apps.Crowdin.Models.Entities;
using Apps.Crowdin.Models.Request.File;
using Apps.Crowdin.Models.Request.Project;
Expand All @@ -10,7 +12,9 @@
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;
using Blackbird.Applications.Sdk.Utils.Parsers;
using Crowdin.Api;
using Crowdin.Api.SourceFiles;
using Crowdin.Api.Storage;
using RestSharp;

namespace Apps.Crowdin.Actions;
Expand Down Expand Up @@ -69,8 +73,9 @@ public async Task<FileEntity> AddFile(
var client = new CrowdinClient(Creds);

var fileStream = await _fileManagementClient.DownloadAsync(input.File);
var storage = await client.Storage.AddStorage(fileStream, input.File.Name);
var request = new AddFileRequest
var storage = await client.AddStorageAsync(input.File.Name, fileStream);

var request = new AddFileRequestDto
{
StorageId = storage.Id,
Name = input.File.Name,
Expand All @@ -80,8 +85,8 @@ public async Task<FileEntity> AddFile(
ExcludedTargetLanguages = input.ExcludedTargetLanguages?.ToList(),
AttachLabelIds = input.AttachLabelIds?.ToList()
};
var file = await client.SourceFiles.AddFile(intProjectId!.Value, request);


var file = await client.AddFileAsync(intProjectId!.Value, request);
return new(file);
}

Expand Down
65 changes: 64 additions & 1 deletion Apps.Crowdin/Api/CrowdinClient.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
using Apps.Crowdin.Constants;
using Apps.Crowdin.Models.Dtos;
using Blackbird.Applications.Sdk.Common.Authentication;
using Blackbird.Applications.Sdk.Utils.Extensions.Files;
using Blackbird.Applications.Sdk.Utils.Extensions.Http;
using Crowdin.Api;
using Newtonsoft.Json;
using RestSharp;

namespace Apps.Crowdin.Api;

public class CrowdinClient : CrowdinApiClient
{
public CrowdinClient(IEnumerable<AuthenticationCredentialsProvider> creds)
private readonly AuthenticationCredentialsProvider[] _creds;

public CrowdinClient(IEnumerable<AuthenticationCredentialsProvider> creds)
: base(GetCrowdinCreds(creds))
{
this._creds = creds.ToArray();
}

private static CrowdinCredentials GetCrowdinCreds(
Expand All @@ -21,4 +29,59 @@ private static CrowdinCredentials GetCrowdinCreds(
AccessToken = token.Value
};
}

public async Task<StorageResourceDto> AddStorageAsync(string fileName, Stream fileStream)
{
var credentials = GetCrowdinCreds(_creds);
var baseUrl = GetBaseUrl(credentials);

using var memoryStream = new MemoryStream();
await fileStream.CopyToAsync(memoryStream);
var bytes = memoryStream.ToArray();

var restClient = new RestClient(baseUrl);
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);

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 @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<Product>Crowdin</Product>
<Description>Cloud-based solution that streamlines localization management</Description>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<AssemblyName>Apps.Crowdin</AssemblyName>
</PropertyGroup>
<ItemGroup>
Expand Down
40 changes: 40 additions & 0 deletions Apps.Crowdin/Models/Dtos/AddFileRequestDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Crowdin.Api.SourceFiles;
using Newtonsoft.Json;

namespace Apps.Crowdin.Models.Dtos;

public class AddFileRequestDto
{
[JsonProperty("storageId")]
public long StorageId { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("branchId")]
public int? BranchId { get; set; }

[JsonProperty("directoryId")]
public int? DirectoryId { get; set; }

[JsonProperty("title")]
public string? Title { get; set; }

[JsonProperty("context")]
public string? Context { get; set; }

[JsonProperty("type")]
public ProjectFileType? Type { get; set; }

[JsonProperty("importOptions")]
public FileImportOptions? ImportOptions { get; set; }

[JsonProperty("exportOptions")]
public FileExportOptions? ExportOptions { get; set; }

[JsonProperty("excludedTargetLanguages")]
public List<string>? ExcludedTargetLanguages { get; set; }

[JsonProperty("attachLabelIds")]
public ICollection<int>? AttachLabelIds { get; set; }
}
9 changes: 9 additions & 0 deletions Apps.Crowdin/Models/Dtos/DataWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Newtonsoft.Json;

namespace Apps.Crowdin.Models.Dtos;

public class DataWrapper<T>
{
[JsonProperty("data")]
public T Data { get; set; }
}
59 changes: 59 additions & 0 deletions Apps.Crowdin/Models/Dtos/FileDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Crowdin.Api;
using Crowdin.Api.SourceFiles;
using Newtonsoft.Json;

namespace Apps.Crowdin.Models.Dtos;

public class FileDto
{
[JsonProperty("id")]
public long Id { get; set; }

[JsonProperty("projectId")]
public int ProjectId { get; set; }

[JsonProperty("branchId")]
public int? BranchId { get; set; }

[JsonProperty("directoryId")]
public int? DirectoryId { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("title")]
public string? Title { get; set; }

[JsonProperty("context")]
public string? Context { get; set; }

[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("path")]
public string Path { get; set; }

[JsonProperty("status")]
public FileStatus Status { get; set; }

[JsonProperty("revisionId")]
public int RevisionId { get; set; }

[JsonProperty("priority")]
public Priority Priority { get; set; }

[JsonProperty("importOptions")]
public FileImportOptions? ImportOptions { get; set; }

[JsonProperty("exportOptions")]
public FileExportOptions? ExportOptions { get; set; }

[JsonProperty("excludedTargetLanguages")]
public string[]? ExcludedTargetLanguages { get; set; }

[JsonProperty("createdAt")]
public DateTimeOffset CreatedAt { get; set; }

[JsonProperty("updatedAt")]
public DateTimeOffset? UpdatedAt { get; set; }
}
12 changes: 12 additions & 0 deletions Apps.Crowdin/Models/Dtos/StorageResourceDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Apps.Crowdin.Models.Dtos;

public class StorageResourceDto
{
[JsonProperty("id")]
public long Id { get; set; }

[JsonProperty("fileName")]
public string FileName { get; set; } = string.Empty;
}
18 changes: 17 additions & 1 deletion Apps.Crowdin/Models/Entities/FileEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Apps.Crowdin.Webhooks.Models.Payload.File;
using Apps.Crowdin.Models.Dtos;
using Apps.Crowdin.Webhooks.Models.Payload.File;
using Blackbird.Applications.Sdk.Common;
using Crowdin.Api.SourceFiles;
using File = Crowdin.Api.SourceFiles.File;
Expand Down Expand Up @@ -67,6 +68,21 @@ public FileEntity(FileResource file)
IsModified = false;
}

public FileEntity(FileDto file)
{
Id = file.Id.ToString();
ProjectId = file.ProjectId.ToString();
BranchId = file.BranchId.ToString();
DirectoryId = file.DirectoryId.ToString();
Name = file.Name;
Title = file.Title;
Type = file.Type;
Path = file.Path;
Status = file.Status.ToString();
CreatedAt = file.CreatedAt.DateTime;
IsModified = false;
}

public FileEntity(FilePayloadWithProject file)
{
Id = file.Id.ToString();
Expand Down

0 comments on commit 047c056

Please sign in to comment.