From cc30206f484ccc6ba5e3d13ea2148e37cafa363d Mon Sep 17 00:00:00 2001 From: Gabriel Odero Date: Sat, 6 Aug 2022 20:27:21 +0300 Subject: [PATCH] Added support for graph api V14 --- .../Extensions/ServiceCollectionExtension.cs | 15 +++++++-------- .../WhatsAppBusinessClient.cs | 4 ++-- .../WhatsAppBusinessRequestEndpoint.cs | 7 ++++++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/WhatsappBusiness.CloudApi/Extensions/ServiceCollectionExtension.cs b/WhatsappBusiness.CloudApi/Extensions/ServiceCollectionExtension.cs index 832005a..49e154e 100644 --- a/WhatsappBusiness.CloudApi/Extensions/ServiceCollectionExtension.cs +++ b/WhatsappBusiness.CloudApi/Extensions/ServiceCollectionExtension.cs @@ -17,7 +17,7 @@ public static class ServiceCollectionExtension /// /// /// - 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(); @@ -39,7 +39,7 @@ public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection se services.AddHttpClient(options => { - options.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress; + options.BaseAddress = (isLatestGraphApiVersion) ? WhatsAppBusinessRequestEndpoint.V14BaseAddress : WhatsAppBusinessRequestEndpoint.BaseAddress; options.Timeout = TimeSpan.FromMinutes(10); }).ConfigurePrimaryHttpMessageHandler(messageHandler => { @@ -54,7 +54,7 @@ public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection se }).AddPolicyHandler(request => request.Method.Equals(HttpMethod.Get) ? retryPolicy : noOpPolicy); } - public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection services, WhatsAppBusinessCloudApiConfig whatsAppConfig) where THandler : HttpMessageHandler + public static void AddWhatsAppBusinessCloudApiService(this IServiceCollection services, WhatsAppBusinessCloudApiConfig whatsAppConfig, bool isLatestGraphApiVersion = false) where THandler : HttpMessageHandler { Random jitterer = new Random(); @@ -76,12 +76,11 @@ public static void AddWhatsAppBusinessCloudApiService(this IServiceCol services.AddHttpClient(options => { - options.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress; + options.BaseAddress = (isLatestGraphApiVersion) ? WhatsAppBusinessRequestEndpoint.V14BaseAddress : WhatsAppBusinessRequestEndpoint.BaseAddress; options.Timeout = TimeSpan.FromMinutes(10); - }) - .SetHandlerLifetime(Timeout.InfiniteTimeSpan) - .ConfigurePrimaryHttpMessageHandler() - .AddPolicyHandler(request => request.Method.Equals(HttpMethod.Get) ? retryPolicy : noOpPolicy); + }).SetHandlerLifetime(Timeout.InfiniteTimeSpan) + .ConfigurePrimaryHttpMessageHandler() + .AddPolicyHandler(request => request.Method.Equals(HttpMethod.Get) ? retryPolicy : noOpPolicy); } } } diff --git a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs index 56dbda9..a87eb24 100644 --- a/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs +++ b/WhatsappBusiness.CloudApi/WhatsAppBusinessClient.cs @@ -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 => @@ -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 => { diff --git a/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs b/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs index ded04f8..6773652 100644 --- a/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs +++ b/WhatsappBusiness.CloudApi/WhatsAppBusinessRequestEndpoint.cs @@ -5,10 +5,15 @@ namespace WhatsappBusiness.CloudApi public static class WhatsAppBusinessRequestEndpoint { /// - /// WhatsApp Business Cloud API BaseAdress + /// WhatsApp Business Cloud API V13 BaseAddress /// public static Uri BaseAddress { get; private set; } = new Uri("https://graph.facebook.com/v13.0/"); + /// + /// WhatsApp Business Cloud API V14 BaseAddress + /// + public static Uri V14BaseAddress { get; private set; } = new Uri("https://graph.facebook.com/v14.0/"); + /// /// To register your phone to WhatsApp Business ///