Hello,
I have a project in C# .Net to be able to perform Transifex API calls. I have an issue while downloading the resource file content as I see a different endpoint where it downloads and we need to make callbacks to the API to check status while it is processing or succeeded. Once succeeded only then we will have the actual content of the resource file to be written on my local file. Can anyone share an example of how this can be handled especially making the POST callback in C# with the location received and doing it while the status is not succeeded.
My request object (TransifexRequestResource.DownloadResourceTranslation):
{
"data": {
"type": "resource_translations_async_downloads",
"attributes": {
"content_encoding": "text",
"file_type": "default",
"mode": "default",
"pseudo": false
},
"relationships": {
"resource": {
"data": {
"type": "resources",
"id": "o:organization_name:p:{projectSlug}:r:{resourceSlug}"
}
},
"language": {
"data": {
"type": "languages",
"id": "l:{langCode}"
}
}
}
}
}
My DownloadFunction looks like below:
public async Task<bool> DownloadResourceFile(string filePath)
{
try
{
var httpDownloadResourceRequestMessage = new HttpRequestMessage(HttpMethod.Post, $"{_httpClient.BaseAddress}resource_translations_async_downloads");
var downloadResourceRequest = TransifexRequestResource.DownloadResourceTranslations.Replace("{projectSlug}", $"{_projectName}Slug");
downloadResourceRequest = downloadResourceRequest.Replace("{resourceSlug}", _resourceSlug);
downloadResourceRequest = downloadResourceRequest.Replace("{langCode}", _languageCode);
httpDownloadResourceRequestMessage.Content = new StringContent(downloadResourceRequest, Encoding.UTF8, "application/vnd.api+json"); //Content-type header
httpDownloadResourceRequestMessage.Content.Headers.ContentType.CharSet = null;
var response = _httpClient.SendAsync(httpDownloadResourceRequestMessage).Result;
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
var error = JObject.Parse(responseBody);
//Console.WriteLine(error["errors"]);
Console.WriteLine(error);
return false;
}
else
{
var responseJson = JObject.Parse(responseBody);
var downloadLinkSelf = (string)responseJson["data"]["links"]["self"];
GetFileDownloadContent(_httpClient, downloadLinkSelf, filePath);
Console.WriteLine(responseJson);
return true;
}
}
catch (Exception ex)
{
_consoleMessageHelper.PrintResultsToConsole($"Writing to file failed for resource {Path.GetFileName(filePath)}\nReason : {_exceptionHandler.GetExceptionDetails(ex, true)}");
return false;
}
}
private static async void GetFileDownloadContent(HttpClient httpClient, string downloadLinkSelf,
string filePath)
{
var httpDownloadResourceRequestMessage = new HttpRequestMessage(HttpMethod.Get, downloadLinkSelf);
var response = httpClient.SendAsync(httpDownloadResourceRequestMessage).Result;
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
var error = JObject.Parse(responseBody);
Console.WriteLine(error);
}
else
{
File.WriteAllText(filePath, responseBody);
Console.WriteLine($"File downloaded content {responseBody}");
Console.WriteLine(response);
}
}