Skip to content

Commit

Permalink
use different http client for file
Browse files Browse the repository at this point in the history
  • Loading branch information
xxyziggy committed Apr 17, 2024
1 parent 1370a61 commit eb8c6bc
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions FunctionApp/UploadDocumentToLoxo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,20 @@ public async Task<IActionResult> Run(
return validationResult;
}

using var httpClient = CreateHttpClient(data.slug);
var personId = await GetPersonId(httpClient, data.slug, data.email, log);
using var loxoClient = CreateLoxoHttpClient(data.slug);
var personId = await GetPersonId(loxoClient, data.slug, data.email, log);
if (personId == null)
{
return new StatusCodeResult(StatusCodes.Status404NotFound);
}

var fileData = await DownloadFile(httpClient, data.fileUrl, log);
var fileData = await DownloadFile(new HttpClient(), data.fileUrl, log);
if (fileData == null)
{
return new StatusCodeResult(StatusCodes.Status404NotFound);
}

var postResult = await PostFile(httpClient, data.slug, personId.Value, fileData, log);
if (!postResult)
{
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}

log.LogInformation("File posted successfully.");
return new OkResult();
return await PostFile(loxoClient, data.slug, personId.Value, fileData, log);
}

private async Task<(string fileUrl, string slug, string email)> ParseRequestBody(HttpRequest req)
Expand Down Expand Up @@ -88,7 +81,7 @@ private static IActionResult ValidateData((string fileUrl, string slug, string e
return null;
}

private HttpClient CreateHttpClient(string slug)
private HttpClient CreateLoxoHttpClient(string slug)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand Down Expand Up @@ -125,7 +118,7 @@ private async Task<byte[]> DownloadFile(HttpClient httpClient, string fileUrl, I
return fileData;
}

private async Task<bool> PostFile(HttpClient httpClient, string slug, int personId, byte[] fileData,
private async Task<IActionResult> PostFile(HttpClient httpClient, string slug, int personId, byte[] fileData,
ILogger log)
{
var fileContent = new ByteArrayContent(fileData);
Expand All @@ -140,10 +133,10 @@ private async Task<bool> PostFile(HttpClient httpClient, string slug, int person
if (!postResponse.IsSuccessStatusCode)
{
log.LogError($"Failed to post file. HTTP status code: {postResponse.StatusCode}");
return false;
return new StatusCodeResult((int)postResponse.StatusCode);
}

return true;
return new OkResult();
}
}
}

0 comments on commit eb8c6bc

Please sign in to comment.