From e083c42c806d8b227eb1ad8f33f06fc900e5c576 Mon Sep 17 00:00:00 2001 From: Mauricio Binda da Costa Date: Sat, 27 Jan 2024 12:45:18 -0300 Subject: [PATCH] #153 -> including support for other API versions >= v16.0. --- .../api/WhatsappApiServiceGenerator.java | 17 +++++++------ .../api/configuration/ApiVersion.java | 20 ++++++++++++++++ .../api/configuration/WhatsappApiConfig.java | 15 +++++++++++- .../WhatsappBusinessCloudApiService.java | 14 +++++------ .../WhatsappBusinessManagementApiService.java | 24 +++++++++---------- .../SendTemplateButtonMessageExample.java | 4 ++++ 6 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/whatsapp/api/configuration/ApiVersion.java diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java index a5907cdfd..13ee804d9 100644 --- a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java +++ b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java @@ -1,17 +1,15 @@ package com.whatsapp.api; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; import com.whatsapp.api.configuration.WhatsappApiConfig; import com.whatsapp.api.domain.errors.Error; import com.whatsapp.api.domain.errors.WhatsappApiError; import com.whatsapp.api.domain.media.MediaFile; import com.whatsapp.api.exception.WhatsappApiException; import com.whatsapp.api.interceptor.AuthenticationInterceptor; -import com.whatsapp.api.utils.proxy.CustomProxyAuthenticator; import com.whatsapp.api.utils.proxy.CustomHttpProxySelector; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; - +import com.whatsapp.api.utils.proxy.CustomProxyAuthenticator; import okhttp3.OkHttpClient; import okhttp3.ResponseBody; import retrofit2.Call; @@ -32,7 +30,7 @@ public class WhatsappApiServiceGenerator { static OkHttpClient sharedClient; private static final Converter.Factory converterFactory = JacksonConverterFactory.create( - new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) ); @SuppressWarnings("unchecked") @@ -46,7 +44,7 @@ private WhatsappApiServiceGenerator() { sharedClient = createDefaultHttpClient(); } - public static OkHttpClient createDefaultHttpClient(){ + public static OkHttpClient createDefaultHttpClient() { return new OkHttpClient.Builder()// .callTimeout(20, TimeUnit.SECONDS)// .pingInterval(20, TimeUnit.SECONDS)// @@ -66,8 +64,9 @@ public static OkHttpClient createDefaultHttpClient(){ * you can pass {@code null} as parameters for {@code username} and {@code pwd}. * *

+ * * @param host the host (Not null) - * @param port the port + * @param port the port * @param username the username * @param pwd the pwd * @see Proxy Selector @@ -129,7 +128,7 @@ public static S createService(Class serviceClass, String token, String ba */ public static S createService(Class serviceClass, String token) { - var baseUrl = WhatsappApiConfig.BASE_DOMAIN; + var baseUrl = WhatsappApiConfig.BASE_DOMAIN + WhatsappApiConfig.API_VERSION + "/"; return createService(serviceClass, token, baseUrl); } diff --git a/src/main/java/com/whatsapp/api/configuration/ApiVersion.java b/src/main/java/com/whatsapp/api/configuration/ApiVersion.java new file mode 100644 index 000000000..0275b1390 --- /dev/null +++ b/src/main/java/com/whatsapp/api/configuration/ApiVersion.java @@ -0,0 +1,20 @@ +package com.whatsapp.api.configuration; + +public enum ApiVersion { + + @Deprecated() + V16_0("v16.0"), + V17_0("v17.0"), + V18_0("v18.0"), + V19_0("v19.0"); + + private final String value; + + ApiVersion(String value) { + this.value = value; + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/com/whatsapp/api/configuration/WhatsappApiConfig.java b/src/main/java/com/whatsapp/api/configuration/WhatsappApiConfig.java index 22dfd4497..0ad8f454a 100644 --- a/src/main/java/com/whatsapp/api/configuration/WhatsappApiConfig.java +++ b/src/main/java/com/whatsapp/api/configuration/WhatsappApiConfig.java @@ -5,10 +5,14 @@ */ public class WhatsappApiConfig { + + private WhatsappApiConfig() { + } + /** * The constant API_VERSION. */ - public final static String API_VERSION = "v16.0"; + public static String API_VERSION = ApiVersion.V19_0.getValue(); /** * The constant BASE_DOMAIN. */ @@ -23,5 +27,14 @@ public static void setBaseDomain(String baseDomain) { BASE_DOMAIN = baseDomain; } + /** + * Sets api version. + * + * @param apiVersion the api version enum + */ + public static void setApiVersion(ApiVersion apiVersion) { + API_VERSION = apiVersion.getValue(); + } + } diff --git a/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java b/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java index fe5cb6654..1151eff96 100644 --- a/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java +++ b/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java @@ -21,8 +21,6 @@ import retrofit2.http.Streaming; import retrofit2.http.Url; -import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION; - /** * The interface Whatsapp business cloud api service. */ @@ -35,7 +33,7 @@ public interface WhatsappBusinessCloudApiService { * @param message the message * @return the call */ - @POST("/" + API_VERSION + "/{Phone-Number-ID}/messages") + @POST("/{Phone-Number-ID}/messages") Call sendMessage(@Path("Phone-Number-ID") String phoneNumberId, @Body Message message); /** @@ -47,7 +45,7 @@ public interface WhatsappBusinessCloudApiService { * @return the call */ @Multipart - @POST("/" + API_VERSION + "/{Phone-Number-ID}/media") + @POST( "/{Phone-Number-ID}/media") Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, @Part MultipartBody.Part file, @Part MultipartBody.Part messageProduct); @@ -57,7 +55,7 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, * @param mediaId the media id * @return the call */ - @GET("/" + API_VERSION + "/{media-id}") + @GET("/{media-id}") Call retrieveMediaUrl(@Path("media-id") String mediaId); /** @@ -77,7 +75,7 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, * @param mediaId the media id * @return the call */ - @DELETE("/" + API_VERSION + "/{media-id}") + @DELETE("/{media-id}") Call deleteMedia(@Path("media-id") String mediaId); /** @@ -87,7 +85,7 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, * @param message the message * @return the call */ - @POST("/" + API_VERSION + "/{Phone-Number-ID}/messages") + @POST("/{Phone-Number-ID}/messages") Call markMessageAsRead(@Path("Phone-Number-ID") String phoneNumberId, @Body ReadMessage message); /** @@ -97,7 +95,7 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, * @param twoStepCode the two-step code * @return the call */ - @POST("/" + API_VERSION + "/{Phone-Number-ID}") + @POST("/{Phone-Number-ID}") Call twoStepVerification(@Path("Phone-Number-ID") String phoneNumberId, @Body TwoStepCode twoStepCode); } diff --git a/src/main/java/com/whatsapp/api/service/WhatsappBusinessManagementApiService.java b/src/main/java/com/whatsapp/api/service/WhatsappBusinessManagementApiService.java index bb98d6570..71a115d7b 100644 --- a/src/main/java/com/whatsapp/api/service/WhatsappBusinessManagementApiService.java +++ b/src/main/java/com/whatsapp/api/service/WhatsappBusinessManagementApiService.java @@ -21,8 +21,6 @@ import java.util.Map; -import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION; - /** * The interface Whatsapp business management api service. */ @@ -36,7 +34,7 @@ public interface WhatsappBusinessManagementApiService { * @param messageTemplate the message template * @return the call */ - @POST("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates") + @POST( "/{whatsapp-business-account-ID}/message_templates") Call