Skip to content

Commit

Permalink
Resumable upload content type fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed May 12, 2023
1 parent ee20c08 commit 781ca12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
68 changes: 45 additions & 23 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class WhatsAppBusinessClient : IWhatsAppBusinessClient
/// Initialize WhatsAppBusinessClient with httpclient factory
/// </summary>
/// <param name="whatsAppConfig">WhatsAppBusiness configuration</param>
/// <param name="isLatestGraphApiVersion">Set True if you want use v14, false if you want to use v13</param>
public WhatsAppBusinessClient(WhatsAppBusinessCloudApiConfig whatsAppConfig)
{
var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError()
Expand Down Expand Up @@ -2068,14 +2067,16 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
/// <returns>Response object</returns>
/// <exception cref="WhatsappBusinessCloudAPIException"></exception>
private async Task<T> WhatsAppBusinessPostAsync<T>(string whatsAppBusinessEndpoint, string filePath, string fileContentType, CancellationToken cancellationToken = default, bool isMediaUpload = false) where T : new()
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _whatsAppConfig.AccessToken);

{
if (!isMediaUpload) // Resumable upload
{
_httpClient.DefaultRequestHeaders.Add("Content-Type", fileContentType);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _whatsAppConfig.AccessToken);
_httpClient.DefaultRequestHeaders.Add("file_offset", "0");
}
else
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _whatsAppConfig.AccessToken);
}

T result = new();
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -2086,7 +2087,9 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
string boundary = $"----------{Guid.NewGuid():N}";
var content = new MultipartFormDataContent(boundary);

if (isMediaUpload)
HttpResponseMessage? response;

if (isMediaUpload)
{
ByteArrayContent mediaFileContent = new ByteArrayContent(uploaded_file);
mediaFileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
Expand All @@ -2103,21 +2106,24 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>

content.Add(mediaFileContent);
content.Add(new StringContent(fileData.messaging_product), "messaging_product");
}
else

response = await _httpClient.PostAsync(whatsAppBusinessEndpoint, content, cancellationToken).ConfigureAwait(false);
}
else // Resumable upload
{
ByteArrayContent mediaFileContent = new ByteArrayContent(uploaded_file);
mediaFileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "file",
FileName = file.FullName,
};
content.Add(mediaFileContent);
}
ByteArrayContent mediaFileContent = new ByteArrayContent(uploaded_file);

var response = await _httpClient.PostAsync(whatsAppBusinessEndpoint, content, cancellationToken).ConfigureAwait(false);
HttpRequestMessage requestMessage = new HttpRequestMessage();
requestMessage.Method = HttpMethod.Post;
requestMessage.Content = mediaFileContent;
requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

if (response.IsSuccessStatusCode)
requestMessage.RequestUri = new Uri($"{_httpClient.BaseAddress}{whatsAppBusinessEndpoint}");

response = await _httpClient.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
}

if (response.IsSuccessStatusCode)
{
#if NET5_0_OR_GREATER
await response.Content.ReadAsStreamAsync(cancellationToken).ContinueWith((Task<Stream> stream) =>
Expand Down Expand Up @@ -2173,22 +2179,38 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
/// <exception cref="WhatsappBusinessCloudAPIException"></exception>
private async Task<T> WhatsAppBusinessGetAsync<T>(string whatsAppBusinessEndpoint, CancellationToken cancellationToken = default, bool isCacheControlActive = false, bool isHeaderAccessTokenProvided = true) where T : new()
{
if (isHeaderAccessTokenProvided)
if (isHeaderAccessTokenProvided && !isCacheControlActive)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _whatsAppConfig.AccessToken);
}

if (isCacheControlActive)
if (isCacheControlActive) // Resumable upload
{
_httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _whatsAppConfig.AccessToken);
_httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
};
}
}

T result = new();
cancellationToken.ThrowIfCancellationRequested();
var response = await _httpClient.GetAsync(whatsAppBusinessEndpoint, cancellationToken).ConfigureAwait(false);

HttpResponseMessage? response;

if (!isCacheControlActive)
{
response = await _httpClient.GetAsync(whatsAppBusinessEndpoint, cancellationToken).ConfigureAwait(false);
}
else
{
HttpRequestMessage requestMessage = new HttpRequestMessage();
requestMessage.Method = HttpMethod.Get;
requestMessage.RequestUri = new Uri($"{_httpClient.BaseAddress}{whatsAppBusinessEndpoint}");

response = await _httpClient.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
}


if (response.IsSuccessStatusCode)
{
Expand Down
6 changes: 3 additions & 3 deletions WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
<RepositoryUrl>https://github.com/gabrieldwight/Whatsapp-Business-Cloud-Api-Net</RepositoryUrl>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Version>1.0.16</Version>
<Version>1.0.17</Version>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.15" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.16" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 781ca12

Please sign in to comment.