Skip to content

Commit

Permalink
Added support for graph api V14
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed Aug 6, 2022
1 parent 58b4da1 commit cc30206
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class ServiceCollectionExtension
/// </summary>
/// <param name="services"></param>
/// <param name="whatsAppBusinessPhoneNumberId"></param>
public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection services, WhatsAppBusinessCloudApiConfig whatsAppConfig)
public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection services, WhatsAppBusinessCloudApiConfig whatsAppConfig, bool isLatestGraphApiVersion = false)
{
Random jitterer = new Random();

Expand All @@ -39,7 +39,7 @@ public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection se

services.AddHttpClient<IWhatsAppBusinessClient, WhatsAppBusinessClient>(options =>
{
options.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress;
options.BaseAddress = (isLatestGraphApiVersion) ? WhatsAppBusinessRequestEndpoint.V14BaseAddress : WhatsAppBusinessRequestEndpoint.BaseAddress;
options.Timeout = TimeSpan.FromMinutes(10);
}).ConfigurePrimaryHttpMessageHandler(messageHandler =>
{
Expand All @@ -54,7 +54,7 @@ public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection se
}).AddPolicyHandler(request => request.Method.Equals(HttpMethod.Get) ? retryPolicy : noOpPolicy);
}

public static void AddWhatsAppBusinessCloudApiService<THandler>(this IServiceCollection services, WhatsAppBusinessCloudApiConfig whatsAppConfig) where THandler : HttpMessageHandler
public static void AddWhatsAppBusinessCloudApiService<THandler>(this IServiceCollection services, WhatsAppBusinessCloudApiConfig whatsAppConfig, bool isLatestGraphApiVersion = false) where THandler : HttpMessageHandler
{
Random jitterer = new Random();

Expand All @@ -76,12 +76,11 @@ public static void AddWhatsAppBusinessCloudApiService<THandler>(this IServiceCol

services.AddHttpClient<IWhatsAppBusinessClient, WhatsAppBusinessClient>(options =>
{
options.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress;
options.BaseAddress = (isLatestGraphApiVersion) ? WhatsAppBusinessRequestEndpoint.V14BaseAddress : WhatsAppBusinessRequestEndpoint.BaseAddress;
options.Timeout = TimeSpan.FromMinutes(10);
})
.SetHandlerLifetime(Timeout.InfiniteTimeSpan)
.ConfigurePrimaryHttpMessageHandler<THandler>()
.AddPolicyHandler(request => request.Method.Equals(HttpMethod.Get) ? retryPolicy : noOpPolicy);
}).SetHandlerLifetime(Timeout.InfiniteTimeSpan)
.ConfigurePrimaryHttpMessageHandler<THandler>()
.AddPolicyHandler(request => request.Method.Equals(HttpMethod.Get) ? retryPolicy : noOpPolicy);
}
}
}
4 changes: 2 additions & 2 deletions WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class WhatsAppBusinessClient : IWhatsAppBusinessClient
private readonly JsonSerializer _serializer = new JsonSerializer();
private readonly WhatsAppBusinessCloudApiConfig _whatsAppConfig;

public WhatsAppBusinessClient(WhatsAppBusinessCloudApiConfig whatsAppConfig)
public WhatsAppBusinessClient(WhatsAppBusinessCloudApiConfig whatsAppConfig, bool isLatestGraphApiVersion = false)
{
var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError()
.WaitAndRetryAsync(1, retryAttempt =>
Expand All @@ -44,7 +44,7 @@ public WhatsAppBusinessClient(WhatsAppBusinessCloudApiConfig whatsAppConfig)
var services = new ServiceCollection();
services.AddHttpClient("WhatsAppBusinessApiClient", client =>
{
client.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress;
client.BaseAddress = (isLatestGraphApiVersion) ? WhatsAppBusinessRequestEndpoint.V14BaseAddress : WhatsAppBusinessRequestEndpoint.BaseAddress;
client.Timeout = TimeSpan.FromMinutes(10);
}).ConfigurePrimaryHttpMessageHandler(messageHandler =>
{
Expand Down
7 changes: 6 additions & 1 deletion WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ namespace WhatsappBusiness.CloudApi
public static class WhatsAppBusinessRequestEndpoint
{
/// <summary>
/// WhatsApp Business Cloud API BaseAdress
/// WhatsApp Business Cloud API V13 BaseAddress
/// </summary>
public static Uri BaseAddress { get; private set; } = new Uri("https://graph.facebook.com/v13.0/");

/// <summary>
/// WhatsApp Business Cloud API V14 BaseAddress
/// </summary>
public static Uri V14BaseAddress { get; private set; } = new Uri("https://graph.facebook.com/v14.0/");

/// <summary>
/// To register your phone to WhatsApp Business
/// </summary>
Expand Down

0 comments on commit cc30206

Please sign in to comment.