diff --git a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs index 8ef5ad2..50076d1 100644 --- a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs @@ -40,7 +40,6 @@ public class WhatsAppBusinessClient : IWhatsAppBusinessClient /// Initialize WhatsAppBusinessClient with httpclient factory /// /// WhatsAppBusiness configuration - /// Set True if you want use v14, false if you want to use v13 public WhatsAppBusinessClient(WhatsAppBusinessCloudApiConfig whatsAppConfig) { var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError() @@ -2068,14 +2067,16 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task stream) => /// Response object /// private async Task WhatsAppBusinessPostAsync(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(); @@ -2086,7 +2087,9 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task 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") @@ -2103,21 +2106,24 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task 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) => @@ -2173,22 +2179,38 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task stream) => /// private async Task WhatsAppBusinessGetAsync(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) { diff --git a/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj b/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj index 168c113..1ee11c2 100644 --- a/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj +++ b/WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj @@ -12,19 +12,19 @@ https://github.com/gabrieldwight/Whatsapp-Business-Cloud-Api-Net enable latest - 1.0.16 + 1.0.17 - + - +