Skip to content

Commit

Permalink
Added additional parameters for media download async method
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed May 1, 2024
1 parent 4ef42a5 commit fbb2dc5
Show file tree
Hide file tree
Showing 7 changed files with 1,232 additions and 1,180 deletions.
2,351 changes: 1,197 additions & 1,154 deletions Samples/WhatsAppBusinessCloudAPI.Web/Controllers/HomeController.cs

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions Samples/WhatsAppBusinessCloudAPI.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
// Add services to the container.
builder.Services.AddControllersWithViews().AddNewtonsoftJson(options =>
{
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});

builder.Services.Configure<WhatsAppBusinessCloudApiConfig>(options =>
{
builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration").Bind(options);
builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration").Bind(options);
});

WhatsAppBusinessCloudApiConfig whatsAppConfig = new WhatsAppBusinessCloudApiConfig();
whatsAppConfig.WhatsAppBusinessPhoneNumberId = builder.Configuration.GetSection("WhatsApp")["WhatsAppBusinessPhoneNumberId"];
whatsAppConfig.WhatsAppBusinessAccountId = builder.Configuration.GetSection("WhatsApp")["WhatsAppBusinessAccountId"];
whatsAppConfig.WhatsAppBusinessId = builder.Configuration.GetSection("WhatsApp")["WhatsAppBusinessId"];
whatsAppConfig.AccessToken = builder.Configuration.GetSection("WhatsApp")["AccessToken"];
whatsAppConfig.AppName = builder.Configuration.GetSection("AppInfo")["AppName"];
whatsAppConfig.Version = builder.Configuration.GetSection("AppInfo")["Version"];
whatsAppConfig.WhatsAppBusinessPhoneNumberId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessPhoneNumberId"];
whatsAppConfig.WhatsAppBusinessAccountId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessAccountId"];
whatsAppConfig.WhatsAppBusinessId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessId"];
whatsAppConfig.AccessToken = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["AccessToken"];
whatsAppConfig.AppName = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["AppName"];
whatsAppConfig.Version = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["Version"];

builder.Services.AddWhatsAppBusinessCloudApiService(whatsAppConfig);

Expand All @@ -31,9 +31,9 @@
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
Expand All @@ -44,7 +44,7 @@
app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// </summary>
public class SendTextMessageViewModel
{
//public string RecipientPhoneNumber { get; set; }
//public string Message { get; set; }
public string RecipientPhoneNumber { get; set; }
public string Message { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ViewData["CurrentPage"] = "Bulk Send WhatsApps";
Layout = "~/Views/Shared/AdminLTE/_AdminLayout.cshtml";
ViewData["ControllerName"] = nameof(HomeController).Replace("Controller", "");
ViewData["ActionName"] = nameof(HomeController.BulkSendWhatsApps);
//ViewData["ActionName"] = nameof(HomeController.BulkSendWhatsApps);
}

<section class="content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ public interface IWhatsAppBusinessClient
/// <param name="mediaUrl">The URL generated from whatsapp cloud api</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>byte[]</returns>
Task<byte[]> DownloadMediaAsync(string mediaUrl, CancellationToken cancellationToken = default);
Task<byte[]> DownloadMediaAsync(string mediaUrl, string appName = null, string version = null, CancellationToken cancellationToken = default);

/// <summary>
/// To download media uploaded from whatsapp
/// </summary>
/// <param name="mediaUrl">The URL generated from whatsapp cloud api</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>byte[]</returns>
byte[] DownloadMedia(string mediaUrl, CancellationToken cancellationToken = default);
byte[] DownloadMedia(string mediaUrl, string appName = null, string version = null, CancellationToken cancellationToken = default);
#endregion

#region Phone Numbers functions
Expand Down
21 changes: 15 additions & 6 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ public async Task<BaseSuccessResponse> DeRegisterWhatsAppBusinessPhoneNumberAsyn
/// <param name="mediaUrl">The URL generated from whatsapp cloud api</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>byte[]</returns>
public byte[] DownloadMedia(string mediaUrl, CancellationToken cancellationToken = default)
public byte[] DownloadMedia(string mediaUrl, string appName = null, string version = null, CancellationToken cancellationToken = default)
{
string formattedWhatsAppEndpoint;
formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DownloadMedia.Replace("{{Media-URL}}", mediaUrl);
return WhatsAppBusinessGetAsync(formattedWhatsAppEndpoint, cancellationToken).GetAwaiter().GetResult();
return WhatsAppBusinessGetAsync(formattedWhatsAppEndpoint, appName, version, cancellationToken).GetAwaiter().GetResult();
}

/// <summary>
Expand All @@ -449,11 +449,11 @@ public byte[] DownloadMedia(string mediaUrl, CancellationToken cancellationToken
/// <param name="mediaUrl">The URL generated from whatsapp cloud api</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>byte[]</returns>
public async Task<byte[]> DownloadMediaAsync(string mediaUrl, CancellationToken cancellationToken = default)
public async Task<byte[]> DownloadMediaAsync(string mediaUrl, string appName = null, string version = null, CancellationToken cancellationToken = default)
{
string formattedWhatsAppEndpoint;
formattedWhatsAppEndpoint = WhatsAppBusinessRequestEndpoint.DownloadMedia.Replace("{{Media-URL}}", mediaUrl);
return await WhatsAppBusinessGetAsync(formattedWhatsAppEndpoint, cancellationToken);
return await WhatsAppBusinessGetAsync(formattedWhatsAppEndpoint, appName, version, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -2889,9 +2889,18 @@ await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
return result;
}

private async Task<byte[]> WhatsAppBusinessGetAsync(string whatsAppBusinessEndpoint, CancellationToken cancellationToken = default)
private async Task<byte[]> WhatsAppBusinessGetAsync(string whatsAppBusinessEndpoint, string AppName = null, string version = null, CancellationToken cancellationToken = default)
{
var productValue = new ProductInfoHeaderValue(_whatsAppConfig.AppName, _whatsAppConfig.Version);
ProductInfoHeaderValue productValue;

if (!string.IsNullOrWhiteSpace(AppName) && !string.IsNullOrWhiteSpace(version))
{
productValue = new ProductInfoHeaderValue(AppName, version);
}
else
{
productValue = new ProductInfoHeaderValue(_whatsAppConfig.AppName, _whatsAppConfig.Version);
}

_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _whatsAppConfig.AccessToken);
_httpClient.DefaultRequestHeaders.UserAgent.Add(productValue);
Expand Down
2 changes: 1 addition & 1 deletion WhatsappBusiness.CloudApi/WhatsappBusiness.CloudApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/gabrieldwight/Whatsapp-Business-Cloud-Api-Net</RepositoryUrl>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Version>1.0.28</Version>
<Version>1.0.29</Version>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' or '$(TargetFramework)' == 'netstandard2.1'">
Expand Down

0 comments on commit fbb2dc5

Please sign in to comment.