diff --git a/src/main/java/com/whatsapp/api/WhatsappApiFactory.java b/src/main/java/com/whatsapp/api/WhatsappApiFactory.java
index 16474b8ac..5853e8386 100644
--- a/src/main/java/com/whatsapp/api/WhatsappApiFactory.java
+++ b/src/main/java/com/whatsapp/api/WhatsappApiFactory.java
@@ -1,6 +1,7 @@
package com.whatsapp.api;
+import com.whatsapp.api.configuration.ApiVersion;
import com.whatsapp.api.impl.WhatsappBusinessCloudApi;
import com.whatsapp.api.impl.WhatsappBusinessManagementApi;
@@ -44,6 +45,16 @@ public WhatsappBusinessCloudApi newBusinessCloudApi() {
return new WhatsappBusinessCloudApi(token);
}
+ /**
+ * Creates a new synchronous/blocking Whatsapp business cloud api client, especifying the api version
+ *
+ * @return the whatsapp business cloud api
+ */
+ public WhatsappBusinessCloudApi newBusinessCloudApi(ApiVersion apiVersion) {
+
+ return new WhatsappBusinessCloudApi(token, apiVersion);
+ }
+
/**
* Creates a new synchronous/blocking Whatsapp business management api client
*
diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java
index 606688e9d..aea47236d 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;
@@ -50,7 +48,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)//
@@ -70,8 +68,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
@@ -108,14 +107,18 @@ public static void setHttpProxy(String host, int port, String username, String p
* @return the s
*/
public static S createService(Class serviceClass, String token, String baseUrl) {
- Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(converterFactory);
+ Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
+ .baseUrl(baseUrl)
+ .addConverterFactory(converterFactory);
if (token == null) {
retrofitBuilder.client(sharedClient);
} else {
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(token);
- OkHttpClient adaptedClient = sharedClient.newBuilder().addInterceptor(interceptor).build();
+ OkHttpClient adaptedClient = sharedClient.newBuilder()
+ .addInterceptor(interceptor)
+ .build();
retrofitBuilder.client(adaptedClient);
}
@@ -133,7 +136,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.getBaseDomain();
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..509c3c7bc
--- /dev/null
+++ b/src/main/java/com/whatsapp/api/configuration/ApiVersion.java
@@ -0,0 +1,22 @@
+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..eaf447265 100644
--- a/src/main/java/com/whatsapp/api/configuration/WhatsappApiConfig.java
+++ b/src/main/java/com/whatsapp/api/configuration/WhatsappApiConfig.java
@@ -5,14 +5,18 @@
*/
public class WhatsappApiConfig {
+
+ private WhatsappApiConfig() {
+ }
+
/**
* The constant API_VERSION.
*/
- public final static String API_VERSION = "v16.0";
+ private static ApiVersion apiVersion = ApiVersion.V19_0;
/**
* The constant BASE_DOMAIN.
*/
- public static String BASE_DOMAIN = "https://graph.facebook.com/";
+ private static String baseDomain = "https://graph.facebook.com/";
/**
* Sets base domain.
@@ -20,7 +24,34 @@ public class WhatsappApiConfig {
* @param baseDomain the base domain
*/
public static void setBaseDomain(String baseDomain) {
- BASE_DOMAIN = baseDomain;
+ WhatsappApiConfig.baseDomain = baseDomain;
+ }
+
+ /**
+ * Sets api version.
+ *
+ * @param apiVersion the api version enum
+ */
+ public static void setApiVersion(ApiVersion apiVersion) {
+ WhatsappApiConfig.apiVersion = apiVersion;
+ }
+
+ /**
+ * Gets api version
+ *
+ * @return apiVersion
+ */
+ public static ApiVersion getApiVersion() {
+ return apiVersion;
+ }
+
+ /**
+ * Gets api version
+ *
+ * @return apiVersion
+ */
+ public static String getBaseDomain() {
+ return baseDomain;
}
diff --git a/src/main/java/com/whatsapp/api/domain/config/CommerceDataItem.java b/src/main/java/com/whatsapp/api/domain/config/CommerceDataItem.java
index 553cd263f..aa30ddcb5 100644
--- a/src/main/java/com/whatsapp/api/domain/config/CommerceDataItem.java
+++ b/src/main/java/com/whatsapp/api/domain/config/CommerceDataItem.java
@@ -2,6 +2,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonValue;
/**
* The type Commerce data item.
diff --git a/src/main/java/com/whatsapp/api/impl/WhatsappBusinessCloudApi.java b/src/main/java/com/whatsapp/api/impl/WhatsappBusinessCloudApi.java
index 010e757bc..5a1bbe9ff 100644
--- a/src/main/java/com/whatsapp/api/impl/WhatsappBusinessCloudApi.java
+++ b/src/main/java/com/whatsapp/api/impl/WhatsappBusinessCloudApi.java
@@ -1,5 +1,6 @@
package com.whatsapp.api.impl;
+import com.whatsapp.api.configuration.ApiVersion;
import com.whatsapp.api.domain.media.FileType;
import com.whatsapp.api.domain.media.Media;
import com.whatsapp.api.domain.media.MediaFile;
@@ -14,9 +15,8 @@
import okhttp3.MultipartBody;
import okhttp3.MultipartBody.Part;
-import static com.whatsapp.api.WhatsappApiServiceGenerator.createService;
-import static com.whatsapp.api.WhatsappApiServiceGenerator.executeDownloadSync;
-import static com.whatsapp.api.WhatsappApiServiceGenerator.executeSync;
+import static com.whatsapp.api.WhatsappApiServiceGenerator.*;
+import static com.whatsapp.api.configuration.WhatsappApiConfig.getApiVersion;
/**
* Implementation of WhatsApp Business Platform Cloud API with synchronous/blocking
@@ -28,12 +28,28 @@ public class WhatsappBusinessCloudApi {
private final WhatsappBusinessCloudApiService whatsappBusinessCloudApiService;
+ private final ApiVersion apiVersion;
+
/**
* Instantiates a new Whatsapp business cloud api.
*
* @param token the token
*/
public WhatsappBusinessCloudApi(String token) {
+ this.apiVersion = getApiVersion();
+ this.whatsappBusinessCloudApiService = createService(WhatsappBusinessCloudApiService.class, token);
+
+ }
+
+
+ /**
+ * Instantiates a new Whatsapp business cloud api, specifying API version
+ *
+ * @param token the token
+ * @param apiVersion api version
+ */
+ public WhatsappBusinessCloudApi(String token, ApiVersion apiVersion) {
+ this.apiVersion = apiVersion;
this.whatsappBusinessCloudApiService = createService(WhatsappBusinessCloudApiService.class, token);
}
@@ -49,7 +65,7 @@ public WhatsappBusinessCloudApi(String token) {
*/
public MessageResponse sendMessage(String phoneNumberId, Message message) {
- return executeSync(whatsappBusinessCloudApiService.sendMessage(phoneNumberId, message));
+ return executeSync(whatsappBusinessCloudApiService.sendMessage(apiVersion.getValue(), phoneNumberId, message));
}
/**
@@ -73,7 +89,7 @@ public UploadResponse uploadMedia(String phoneNumberId, String fileName, FileTyp
var messageProduct = Part.createFormData("messaging_product", "whatsapp");
- return executeSync(whatsappBusinessCloudApiService.uploadMedia(phoneNumberId, body, messageProduct));
+ return executeSync(whatsappBusinessCloudApiService.uploadMedia(apiVersion.getValue(), phoneNumberId, body, messageProduct));
}
/**
@@ -84,7 +100,7 @@ public UploadResponse uploadMedia(String phoneNumberId, String fileName, FileTyp
* @see official documentation
*/
public Media retrieveMediaUrl(String mediaId) {
- return executeSync(whatsappBusinessCloudApiService.retrieveMediaUrl(mediaId));
+ return executeSync(whatsappBusinessCloudApiService.retrieveMediaUrl(apiVersion.getValue(), mediaId));
}
@@ -110,7 +126,7 @@ public MediaFile downloadMediaFile(String url) {
*/
public Response deleteMedia(String mediaId) {
- return executeSync(whatsappBusinessCloudApiService.deleteMedia(mediaId));
+ return executeSync(whatsappBusinessCloudApiService.deleteMedia(apiVersion.getValue(), mediaId));
}
/**
@@ -122,7 +138,7 @@ public Response deleteMedia(String mediaId) {
* @see official documentation
*/
public Response markMessageAsRead(String phoneNumberId, ReadMessage message) {
- return executeSync(whatsappBusinessCloudApiService.markMessageAsRead(phoneNumberId, message));
+ return executeSync(whatsappBusinessCloudApiService.markMessageAsRead(apiVersion.getValue(), phoneNumberId, message));
}
/**
@@ -135,7 +151,7 @@ public Response markMessageAsRead(String phoneNumberId, ReadMessage message) {
* @see official documentation
*/
public Response twoStepVerification(String phoneNumberId, TwoStepCode twoStepCode) {
- return executeSync(whatsappBusinessCloudApiService.twoStepVerification(phoneNumberId, twoStepCode));
+ return executeSync(whatsappBusinessCloudApiService.twoStepVerification(apiVersion.getValue(), phoneNumberId, twoStepCode));
}
}
diff --git a/src/main/java/com/whatsapp/api/impl/WhatsappBusinessManagementApi.java b/src/main/java/com/whatsapp/api/impl/WhatsappBusinessManagementApi.java
index 411ac6c3f..620c3cd97 100644
--- a/src/main/java/com/whatsapp/api/impl/WhatsappBusinessManagementApi.java
+++ b/src/main/java/com/whatsapp/api/impl/WhatsappBusinessManagementApi.java
@@ -1,5 +1,6 @@
package com.whatsapp.api.impl;
+import com.whatsapp.api.configuration.ApiVersion;
import com.whatsapp.api.domain.config.CommerceDataItem;
import com.whatsapp.api.domain.config.GraphCommerceSettings;
import com.whatsapp.api.domain.phone.PhoneNumber;
@@ -18,12 +19,15 @@
import static com.whatsapp.api.WhatsappApiServiceGenerator.createService;
import static com.whatsapp.api.WhatsappApiServiceGenerator.executeSync;
+import static com.whatsapp.api.configuration.WhatsappApiConfig.getApiVersion;
/**
* The type Whatsapp business management api.
*/
public class WhatsappBusinessManagementApi {
+ private final ApiVersion apiVersion;
+
private final WhatsappBusinessManagementApiService whatsappBusinessManagementApiService;
/**
@@ -32,6 +36,18 @@ public class WhatsappBusinessManagementApi {
* @param token the token
*/
public WhatsappBusinessManagementApi(String token) {
+ this.apiVersion = getApiVersion();
+ this.whatsappBusinessManagementApiService = createService(WhatsappBusinessManagementApiService.class, token);
+ }
+
+ /**
+ * Instantiates a new Whatsapp business management api, specifying api version
+ *
+ * @param token the token
+ * @param apiVersion api version
+ */
+ public WhatsappBusinessManagementApi(String token, ApiVersion apiVersion) {
+ this.apiVersion = apiVersion;
this.whatsappBusinessManagementApiService = createService(WhatsappBusinessManagementApiService.class, token);
}
@@ -45,7 +61,7 @@ public WhatsappBusinessManagementApi(String token) {
*/
public Template createMessageTemplate(String whatsappBusinessAccountId, MessageTemplate messageTemplate) {
- return executeSync(whatsappBusinessManagementApiService.createMessageTemplate(whatsappBusinessAccountId, messageTemplate));
+ return executeSync(whatsappBusinessManagementApiService.createMessageTemplate(apiVersion.getValue(), whatsappBusinessAccountId, messageTemplate));
}
/**
@@ -58,7 +74,7 @@ public Template createMessageTemplate(String whatsappBusinessAccountId, MessageT
*/
public Template updateMessageTemplate(String whatsappBusinessAccountId, String messageTemplateId, MessageTemplate messageTemplate) {
- return executeSync(whatsappBusinessManagementApiService.updateMessageTemplate(whatsappBusinessAccountId, messageTemplateId, messageTemplate));
+ return executeSync(whatsappBusinessManagementApiService.updateMessageTemplate(apiVersion.getValue(), whatsappBusinessAccountId, messageTemplateId, messageTemplate));
}
/**
@@ -70,7 +86,7 @@ public Template updateMessageTemplate(String whatsappBusinessAccountId, String m
*/
public Response deleteMessageTemplate(String whatsappBusinessAccountId, String name) {
- return executeSync(whatsappBusinessManagementApiService.deleteMessageTemplate(whatsappBusinessAccountId, name));
+ return executeSync(whatsappBusinessManagementApiService.deleteMessageTemplate(apiVersion.getValue(), whatsappBusinessAccountId, name));
}
/**
@@ -84,7 +100,7 @@ public Response deleteMessageTemplate(String whatsappBusinessAccountId, String n
* @see Message templates
*/
public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId) {
- return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(whatsappBusinessAccountId));
+ return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(apiVersion.getValue(), whatsappBusinessAccountId));
}
/**
@@ -96,7 +112,7 @@ public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId) {
* @see Message templates
*/
public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId, int limit) {
- return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(whatsappBusinessAccountId, Map.of("limit", limit)));
+ return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(apiVersion.getValue(), whatsappBusinessAccountId, Map.of("limit", limit)));
}
/**
@@ -108,7 +124,7 @@ public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId, int
* @see Message templates
*/
public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId, String templateName) {
- return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(whatsappBusinessAccountId, Map.of("name", templateName)));
+ return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(apiVersion.getValue(), whatsappBusinessAccountId, Map.of("name", templateName)));
}
/**
@@ -120,7 +136,7 @@ public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId, Stri
* @return message templates
*/
public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId, int limit, String after) {
- return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(whatsappBusinessAccountId, Map.of("limit", limit, "after", after)));
+ return executeSync(whatsappBusinessManagementApiService.retrieveTemplates(apiVersion.getValue(), whatsappBusinessAccountId, Map.of("limit", limit, "after", after)));
}
/**
@@ -132,7 +148,7 @@ public MessageTemplates retrieveTemplates(String whatsappBusinessAccountId, int
*/
public PhoneNumber retrievePhoneNumber(String phoneNumberId) {
- return executeSync(whatsappBusinessManagementApiService.retrievePhoneNumber(phoneNumberId, new HashMap<>()));
+ return executeSync(whatsappBusinessManagementApiService.retrievePhoneNumber(apiVersion.getValue(), phoneNumberId, new HashMap<>()));
}
@@ -150,7 +166,7 @@ public PhoneNumber retrievePhoneNumber(String phoneNumberId) {
*/
public PhoneNumber retrievePhoneNumber(String phoneNumberId, String... fields) {
Objects.requireNonNull(fields, "fields cannot be null");
- return executeSync(whatsappBusinessManagementApiService.retrievePhoneNumber(phoneNumberId, Map.of("fields", String.join(",", fields))));
+ return executeSync(whatsappBusinessManagementApiService.retrievePhoneNumber(apiVersion.getValue(), phoneNumberId, Map.of("fields", String.join(",", fields))));
}
@@ -163,7 +179,7 @@ public PhoneNumber retrievePhoneNumber(String phoneNumberId, String... fields) {
* @see api docs
*/
public PhoneNumbers retrievePhoneNumbers(String whatsappBusinessAccountId) {
- return executeSync(whatsappBusinessManagementApiService.retrievePhoneNumbers(whatsappBusinessAccountId));
+ return executeSync(whatsappBusinessManagementApiService.retrievePhoneNumbers(apiVersion.getValue(), whatsappBusinessAccountId));
}
@@ -175,7 +191,7 @@ public PhoneNumbers retrievePhoneNumbers(String whatsappBusinessAccountId) {
* @return the response
*/
public Response requestCode(String phoneNumberId, RequestCode requestCode) {
- return executeSync(whatsappBusinessManagementApiService.requestCode(phoneNumberId, requestCode));
+ return executeSync(whatsappBusinessManagementApiService.requestCode(apiVersion.getValue(), phoneNumberId, requestCode));
}
/**
@@ -187,7 +203,7 @@ public Response requestCode(String phoneNumberId, RequestCode requestCode) {
* @return the response
*/
public Response verifyCode(String phoneNumberId, VerifyCode verifyCode) {
- return executeSync(whatsappBusinessManagementApiService.verifyCode(phoneNumberId, verifyCode));
+ return executeSync(whatsappBusinessManagementApiService.verifyCode(apiVersion.getValue(), phoneNumberId, verifyCode));
}
@@ -205,7 +221,7 @@ public Response verifyCode(String phoneNumberId, VerifyCode verifyCode) {
* @see api docs
*/
public GraphCommerceSettings getWhatsappCommerceSettings(String phoneNumberId, String... fields) {
- return executeSync(whatsappBusinessManagementApiService.getWhatsappCommerceSettings(phoneNumberId, Map.of("fields", String.join(",", fields))));
+ return executeSync(whatsappBusinessManagementApiService.getWhatsappCommerceSettings(apiVersion.getValue(), phoneNumberId, Map.of("fields", String.join(",", fields))));
}
/**
@@ -217,7 +233,7 @@ public GraphCommerceSettings getWhatsappCommerceSettings(String phoneNumberId, S
* @see api docs
*/
public Response updateWhatsappCommerceSettings(String phoneNumberId, CommerceDataItem commerceDataItem) {
- return executeSync(whatsappBusinessManagementApiService.updateWhatsappCommerceSettings(phoneNumberId, commerceDataItem));
+ return executeSync(whatsappBusinessManagementApiService.updateWhatsappCommerceSettings(apiVersion.getValue(), phoneNumberId, commerceDataItem));
}
}
diff --git a/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java b/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java
index fe5cb6654..fc1c7f5f3 100644
--- a/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java
+++ b/src/main/java/com/whatsapp/api/service/WhatsappBusinessCloudApiService.java
@@ -10,18 +10,7 @@
import okhttp3.MultipartBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
-import retrofit2.http.Body;
-import retrofit2.http.DELETE;
-import retrofit2.http.GET;
-import retrofit2.http.Headers;
-import retrofit2.http.Multipart;
-import retrofit2.http.POST;
-import retrofit2.http.Part;
-import retrofit2.http.Path;
-import retrofit2.http.Streaming;
-import retrofit2.http.Url;
-
-import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION;
+import retrofit2.http.*;
/**
* The interface Whatsapp business cloud api service.
@@ -35,8 +24,8 @@ public interface WhatsappBusinessCloudApiService {
* @param message the message
* @return the call
*/
- @POST("/" + API_VERSION + "/{Phone-Number-ID}/messages")
- Call sendMessage(@Path("Phone-Number-ID") String phoneNumberId, @Body Message message);
+ @POST("/{api-version}/{Phone-Number-ID}/messages")
+ Call sendMessage(@Path("api-version") String apiVersion, @Path("Phone-Number-ID") String phoneNumberId, @Body Message message);
/**
* Upload media call.
@@ -47,8 +36,8 @@ public interface WhatsappBusinessCloudApiService {
* @return the call
*/
@Multipart
- @POST("/" + API_VERSION + "/{Phone-Number-ID}/media")
- Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, @Part MultipartBody.Part file,
+ @POST("/{api-version}/{Phone-Number-ID}/media")
+ Call uploadMedia(@Path("api-version") String apiVersion, @Path("Phone-Number-ID") String phoneNumberId, @Part MultipartBody.Part file,
@Part MultipartBody.Part messageProduct);
/**
@@ -57,8 +46,8 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId,
* @param mediaId the media id
* @return the call
*/
- @GET("/" + API_VERSION + "/{media-id}")
- Call retrieveMediaUrl(@Path("media-id") String mediaId);
+ @GET("/{api-version}/{media-id}")
+ Call retrieveMediaUrl(@Path("api-version") String apiVersion, @Path("media-id") String mediaId);
/**
* Download media file call.
@@ -77,8 +66,8 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId,
* @param mediaId the media id
* @return the call
*/
- @DELETE("/" + API_VERSION + "/{media-id}")
- Call deleteMedia(@Path("media-id") String mediaId);
+ @DELETE("/{api-version}/{media-id}")
+ Call deleteMedia(@Path("api-version") String apiVersion, @Path("media-id") String mediaId);
/**
* Send mark Meassge as read call.
@@ -87,8 +76,8 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId,
* @param message the message
* @return the call
*/
- @POST("/" + API_VERSION + "/{Phone-Number-ID}/messages")
- Call markMessageAsRead(@Path("Phone-Number-ID") String phoneNumberId, @Body ReadMessage message);
+ @POST("/{api-version}/{Phone-Number-ID}/messages")
+ Call markMessageAsRead(@Path("api-version") String apiVersion, @Path("Phone-Number-ID") String phoneNumberId, @Body ReadMessage message);
/**
* Two-step verification call.
@@ -97,7 +86,7 @@ Call uploadMedia(@Path("Phone-Number-ID") String phoneNumberId,
* @param twoStepCode the two-step code
* @return the call
*/
- @POST("/" + API_VERSION + "/{Phone-Number-ID}")
- Call twoStepVerification(@Path("Phone-Number-ID") String phoneNumberId, @Body TwoStepCode twoStepCode);
+ @POST("/{api-version}/{Phone-Number-ID}")
+ Call twoStepVerification(@Path("api-version") String apiVersion, @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..13e690041 100644
--- a/src/main/java/com/whatsapp/api/service/WhatsappBusinessManagementApiService.java
+++ b/src/main/java/com/whatsapp/api/service/WhatsappBusinessManagementApiService.java
@@ -11,18 +11,10 @@
import com.whatsapp.api.domain.templates.response.MessageTemplates;
import com.whatsapp.api.domain.templates.response.Template;
import retrofit2.Call;
-import retrofit2.http.Body;
-import retrofit2.http.DELETE;
-import retrofit2.http.GET;
-import retrofit2.http.POST;
-import retrofit2.http.Path;
-import retrofit2.http.Query;
-import retrofit2.http.QueryMap;
+import retrofit2.http.*;
import java.util.Map;
-import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION;
-
/**
* The interface Whatsapp business management api service.
*/
@@ -36,8 +28,8 @@ public interface WhatsappBusinessManagementApiService {
* @param messageTemplate the message template
* @return the call
*/
- @POST("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates")
- Call createMessageTemplate(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Body MessageTemplate messageTemplate);
+ @POST("/{api-version}/{whatsapp-business-account-ID}/message_templates")
+ Call createMessageTemplate(@Path("api-version") String apiVersion, @Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Body MessageTemplate messageTemplate);
/**
* Update message template call.
@@ -47,8 +39,8 @@ public interface WhatsappBusinessManagementApiService {
* @param messageTemplate the message template
* @return the call
*/
- @POST("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates/{message-template-id}")
- Call updateMessageTemplate(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Path("message-template-id") String messageTemplateId, @Body MessageTemplate messageTemplate);
+ @POST("/{api-version}/{whatsapp-business-account-ID}/message_templates/{message-template-id}")
+ Call updateMessageTemplate(@Path("api-version") String apiVersion, @Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Path("message-template-id") String messageTemplateId, @Body MessageTemplate messageTemplate);
/**
* Delete message template call.
@@ -57,8 +49,8 @@ public interface WhatsappBusinessManagementApiService {
* @param name the name
* @return the call
*/
- @DELETE("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates")
- Call deleteMessageTemplate(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Query("name") String name);
+ @DELETE("/{api-version}/{whatsapp-business-account-ID}/message_templates")
+ Call deleteMessageTemplate(@Path("api-version") String apiVersion, @Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Query("name") String name);
/**
* Retrieve templates call.
@@ -66,8 +58,8 @@ public interface WhatsappBusinessManagementApiService {
* @param whatsappBusinessAccountId the whatsapp business account id
* @return the call
*/
- @GET("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates")
- Call retrieveTemplates(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId);
+ @GET("/{api-version}/{whatsapp-business-account-ID}/message_templates")
+ Call retrieveTemplates(@Path("api-version") String apiVersion, @Path("whatsapp-business-account-ID") String whatsappBusinessAccountId);
/**
* Retrieve templates call.
@@ -76,8 +68,8 @@ public interface WhatsappBusinessManagementApiService {
* @param filters the filters
* @return the call
*/
- @GET("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates")
- Call retrieveTemplates(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @QueryMap Map filters);
+ @GET("/{api-version}/{whatsapp-business-account-ID}/message_templates")
+ Call retrieveTemplates(@Path("api-version") String apiVersion, @Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @QueryMap Map filters);
/**
@@ -87,8 +79,8 @@ public interface WhatsappBusinessManagementApiService {
* @param queryParams the query params
* @return the call
*/
- @GET("/" + API_VERSION + "/{phone-number-ID}")
- Call retrievePhoneNumber(@Path("phone-number-ID") String phoneNumberId, @QueryMap Map queryParams);
+ @GET("/{api-version}/{phone-number-ID}")
+ Call retrievePhoneNumber(@Path("api-version") String apiVersion, @Path("phone-number-ID") String phoneNumberId, @QueryMap Map queryParams);
/**
@@ -97,8 +89,8 @@ public interface WhatsappBusinessManagementApiService {
* @param whatsappBusinessAccountId the whatsapp business account id
* @return the call
*/
- @GET("/" + API_VERSION + "/{whatsapp-business-account-ID}/phone_numbers")
- Call retrievePhoneNumbers(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId);
+ @GET("/{api-version}/{whatsapp-business-account-ID}/phone_numbers")
+ Call retrievePhoneNumbers(@Path("api-version") String apiVersion, @Path("whatsapp-business-account-ID") String whatsappBusinessAccountId);
/**
* Request code call.
@@ -107,8 +99,8 @@ public interface WhatsappBusinessManagementApiService {
* @param requestCode the request code
* @return the call
*/
- @POST("/" + API_VERSION + "/{phone-number-ID}/request_code")
- Call requestCode(@Path("phone-number-ID") String phoneNumberId, @Body RequestCode requestCode);
+ @POST("/{api-version}/{phone-number-ID}/request_code")
+ Call requestCode(@Path("api-version") String apiVersion, @Path("phone-number-ID") String phoneNumberId, @Body RequestCode requestCode);
/**
* Verify code call.
@@ -117,8 +109,8 @@ public interface WhatsappBusinessManagementApiService {
* @param verifyCode the verify code
* @return the call
*/
- @POST("/" + API_VERSION + "/{phone-number-ID}/verify_code")
- Call verifyCode(@Path("phone-number-ID") String phoneNumberId, @Body VerifyCode verifyCode);
+ @POST("/{api-version}/{phone-number-ID}/verify_code")
+ Call verifyCode(@Path("api-version") String apiVersion, @Path("phone-number-ID") String phoneNumberId, @Body VerifyCode verifyCode);
/**
* Business phone number's whatsApp commerce settings call.
@@ -127,16 +119,16 @@ public interface WhatsappBusinessManagementApiService {
* @param queryParams the query params
* @return the call
*/
- @GET("/" + API_VERSION + "/{phone-number-ID}/whatsapp_commerce_settings")
- Call getWhatsappCommerceSettings(@Path("phone-number-ID") String phoneNumberId, @QueryMap Map queryParams);
+ @GET("/{api-version}/{phone-number-ID}/whatsapp_commerce_settings")
+ Call getWhatsappCommerceSettings(@Path("api-version") String apiVersion, @Path("phone-number-ID") String phoneNumberId, @QueryMap Map queryParams);
/**
* Business phone number's whatsApp commerce settings call.
*
- * @param phoneNumberId the phone number id
+ * @param phoneNumberId the phone number id
* @param commerceDataItem the query params
* @return the call
*/
- @POST("/" + API_VERSION + "/{phone-number-ID}/whatsapp_commerce_settings")
- Call updateWhatsappCommerceSettings(@Path("phone-number-ID") String phoneNumberId, @Body CommerceDataItem commerceDataItem);
+ @POST("/{api-version}/{phone-number-ID}/whatsapp_commerce_settings")
+ Call updateWhatsappCommerceSettings(@Path("api-version") String apiVersion, @Path("phone-number-ID") String phoneNumberId, @Body CommerceDataItem commerceDataItem);
}
diff --git a/src/test/java/com/whatsapp/api/MockServerUtilsTest.java b/src/test/java/com/whatsapp/api/MockServerUtilsTest.java
index 22b7c2523..095f384a3 100644
--- a/src/test/java/com/whatsapp/api/MockServerUtilsTest.java
+++ b/src/test/java/com/whatsapp/api/MockServerUtilsTest.java
@@ -4,6 +4,7 @@
import com.whatsapp.api.impl.WhatsappBusinessCloudApi;
import com.whatsapp.api.impl.WhatsappBusinessManagementApi;
import mockwebserver3.MockWebServer;
+import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -43,9 +44,11 @@ public void setUp() throws IOException {
@AfterEach
public void tearDown() throws IOException {
-
+ mockWebServer.close();
mockWebServer.shutdown();
}
+
+
}
diff --git a/src/test/java/com/whatsapp/api/examples/SendContactMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendContactMessageExample.java
index bfc96790d..cf61eea93 100644
--- a/src/test/java/com/whatsapp/api/examples/SendContactMessageExample.java
+++ b/src/test/java/com/whatsapp/api/examples/SendContactMessageExample.java
@@ -2,6 +2,7 @@
import com.whatsapp.api.TestConstants;
import com.whatsapp.api.WhatsappApiFactory;
+import com.whatsapp.api.configuration.ApiVersion;
import com.whatsapp.api.domain.messages.Address;
import com.whatsapp.api.domain.messages.Contact;
import com.whatsapp.api.domain.messages.ContactMessage;
@@ -26,7 +27,9 @@ public static void main(String[] args) {
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);
- WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
+ WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi(ApiVersion.V18_0);
+
+
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildContactMessage(new ContactMessage()//
diff --git a/src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java
index c2aae8dd5..ee5d97c32 100644
--- a/src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java
+++ b/src/test/java/com/whatsapp/api/examples/SendLocationMessageExample.java
@@ -1,6 +1,8 @@
package com.whatsapp.api.examples;
import com.whatsapp.api.WhatsappApiFactory;
+import com.whatsapp.api.configuration.ApiVersion;
+import com.whatsapp.api.configuration.WhatsappApiConfig;
import com.whatsapp.api.domain.messages.LocationMessage;
import com.whatsapp.api.domain.messages.Message.MessageBuilder;
import com.whatsapp.api.domain.messages.response.MessageResponse;
@@ -14,8 +16,10 @@ public class SendLocationMessageExample {
public static void main(String[] args) {
+ WhatsappApiConfig.setApiVersion(ApiVersion.V17_0);
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);
+
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
var locationMessage = new LocationMessage()//
.setLongitude("38.819830")
diff --git a/src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessageExample.java
index f1141d911..540579225 100644
--- a/src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessageExample.java
+++ b/src/test/java/com/whatsapp/api/examples/SendTemplateButtonMessageExample.java
@@ -2,6 +2,8 @@
import com.whatsapp.api.TestConstants;
import com.whatsapp.api.WhatsappApiFactory;
+import com.whatsapp.api.configuration.ApiVersion;
+import com.whatsapp.api.configuration.WhatsappApiConfig;
import com.whatsapp.api.domain.messages.BodyComponent;
import com.whatsapp.api.domain.messages.ButtonComponent;
import com.whatsapp.api.domain.messages.ButtonPayloadParameter;
@@ -18,6 +20,8 @@
public class SendTemplateButtonMessageExample {
public static void main(String[] args) {
+ // setting the api version
+ WhatsappApiConfig.setApiVersion(ApiVersion.V17_0);
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
diff --git a/src/test/java/com/whatsapp/api/examples/SendTextMessageExample.java b/src/test/java/com/whatsapp/api/examples/SendTextMessageExample.java
index 227b5b4fc..c84f69cbe 100644
--- a/src/test/java/com/whatsapp/api/examples/SendTextMessageExample.java
+++ b/src/test/java/com/whatsapp/api/examples/SendTextMessageExample.java
@@ -2,6 +2,8 @@
import com.whatsapp.api.TestConstants;
import com.whatsapp.api.WhatsappApiFactory;
+import com.whatsapp.api.configuration.ApiVersion;
+import com.whatsapp.api.configuration.WhatsappApiConfig;
import com.whatsapp.api.domain.messages.Message.MessageBuilder;
import com.whatsapp.api.domain.messages.TextMessage;
import com.whatsapp.api.domain.messages.response.MessageResponse;
@@ -16,6 +18,7 @@ public class SendTextMessageExample {
public static void main(String[] args) {
+ WhatsappApiConfig.setApiVersion(ApiVersion.V18_0);
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java
index 822ea9738..08b854605 100644
--- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java
+++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessCloudApiTest.java
@@ -1,61 +1,12 @@
package com.whatsapp.api.impl;
import com.whatsapp.api.MockServerUtilsTest;
+import com.whatsapp.api.WhatsappApiFactory;
+import com.whatsapp.api.configuration.ApiVersion;
import com.whatsapp.api.domain.media.FileType;
-import com.whatsapp.api.domain.messages.Action;
-import com.whatsapp.api.domain.messages.Address;
-import com.whatsapp.api.domain.messages.AudioMessage;
-import com.whatsapp.api.domain.messages.Body;
-import com.whatsapp.api.domain.messages.BodyComponent;
-import com.whatsapp.api.domain.messages.Button;
-import com.whatsapp.api.domain.messages.ButtonComponent;
-import com.whatsapp.api.domain.messages.ButtonPayloadParameter;
-import com.whatsapp.api.domain.messages.ButtonTextParameter;
-import com.whatsapp.api.domain.messages.Contact;
-import com.whatsapp.api.domain.messages.ContactMessage;
-import com.whatsapp.api.domain.messages.Currency;
-import com.whatsapp.api.domain.messages.CurrencyParameter;
-import com.whatsapp.api.domain.messages.DateTime;
-import com.whatsapp.api.domain.messages.DateTimeParameter;
-import com.whatsapp.api.domain.messages.Document;
-import com.whatsapp.api.domain.messages.DocumentMessage;
-import com.whatsapp.api.domain.messages.DocumentParameter;
-import com.whatsapp.api.domain.messages.Email;
-import com.whatsapp.api.domain.messages.Footer;
-import com.whatsapp.api.domain.messages.Header;
-import com.whatsapp.api.domain.messages.HeaderComponent;
-import com.whatsapp.api.domain.messages.Image;
-import com.whatsapp.api.domain.messages.ImageMessage;
-import com.whatsapp.api.domain.messages.ImageParameter;
-import com.whatsapp.api.domain.messages.InteractiveMessage;
-import com.whatsapp.api.domain.messages.Language;
-import com.whatsapp.api.domain.messages.LocationMessage;
+import com.whatsapp.api.domain.messages.*;
import com.whatsapp.api.domain.messages.Message.MessageBuilder;
-import com.whatsapp.api.domain.messages.Name;
-import com.whatsapp.api.domain.messages.Org;
-import com.whatsapp.api.domain.messages.Phone;
-import com.whatsapp.api.domain.messages.ReactionMessage;
-import com.whatsapp.api.domain.messages.ReadMessage;
-import com.whatsapp.api.domain.messages.Reply;
-import com.whatsapp.api.domain.messages.Row;
-import com.whatsapp.api.domain.messages.Section;
-import com.whatsapp.api.domain.messages.StickerMessage;
-import com.whatsapp.api.domain.messages.TemplateMessage;
-import com.whatsapp.api.domain.messages.TextMessage;
-import com.whatsapp.api.domain.messages.TextParameter;
-import com.whatsapp.api.domain.messages.Url;
-import com.whatsapp.api.domain.messages.Video;
-import com.whatsapp.api.domain.messages.VideoMessage;
-import com.whatsapp.api.domain.messages.VideoParameter;
-import com.whatsapp.api.domain.messages.type.AddressType;
-import com.whatsapp.api.domain.messages.type.ButtonSubType;
-import com.whatsapp.api.domain.messages.type.ButtonType;
-import com.whatsapp.api.domain.messages.type.CalendarType;
-import com.whatsapp.api.domain.messages.type.EmailType;
-import com.whatsapp.api.domain.messages.type.HeaderType;
-import com.whatsapp.api.domain.messages.type.InteractiveMessageType;
-import com.whatsapp.api.domain.messages.type.PhoneType;
-import com.whatsapp.api.domain.messages.type.UrlType;
+import com.whatsapp.api.domain.messages.type.*;
import com.whatsapp.api.domain.phone.TwoStepCode;
import com.whatsapp.api.domain.templates.type.LanguageType;
import com.whatsapp.api.exception.WhatsappApiException;
@@ -71,13 +22,13 @@
import java.io.IOException;
import java.net.URISyntaxException;
-import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION;
+import static com.whatsapp.api.configuration.WhatsappApiConfig.getApiVersion;
-public class WhatsappBusinessCloudApiTest extends MockServerUtilsTest {
+class WhatsappBusinessCloudApiTest extends MockServerUtilsTest {
private final String PHONE_NUMBER_1 = "121212121212";
private final String PHONE_NUMBER_ID = "888888888888";
-
+ private final String API_VERSION = getApiVersion().getValue();
private final String EXPECTED_FOLDER = "/expected/message/";
private final String DEFAULT_SEND_MESSAGE_RESPONSE = """
@@ -1175,4 +1126,27 @@ void testTwoStepVerification() throws IOException, URISyntaxException, Interrupt
Assertions.assertTrue(response.success());
}
+
+ @Test
+ void testApiVersion() throws InterruptedException {
+
+ mockWebServer.enqueue(new MockResponse().newBuilder().code(200).body(DEFAULT_SEND_MESSAGE_RESPONSE).build());
+
+ var message = MessageBuilder.builder()//
+ .setTo(PHONE_NUMBER_1)//
+ .buildTextMessage(new TextMessage()//
+ .setBody(Formatter.bold("Hello world!") + "\nSome code here: \n" + Formatter.code("hello world code here"))//
+ .setPreviewUrl(false));
+
+ var factory = WhatsappApiFactory.newInstance("token");
+ var clientApi = factory.newBusinessCloudApi(ApiVersion.V18_0);
+
+ clientApi.sendMessage(PHONE_NUMBER_ID, message);
+
+ RecordedRequest recordedRequest = mockWebServer.takeRequest();
+
+ Assertions.assertEquals("/" + ApiVersion.V18_0.getValue() + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath());
+
+ }
+
}
diff --git a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java
index cd070f7d0..788d5b7d2 100644
--- a/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java
+++ b/src/test/java/com/whatsapp/api/impl/WhatsappBusinessManagementApiTest.java
@@ -29,10 +29,11 @@
import java.net.URISyntaxException;
import java.util.Collections;
-import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION;
+import static com.whatsapp.api.configuration.WhatsappApiConfig.getApiVersion;
-class WhatsappBusinessManagementApiTest extends MockServerUtilsTest
-{
+class WhatsappBusinessManagementApiTest extends MockServerUtilsTest {
+
+ private final String API_VERSION = getApiVersion().getValue();
public final String DEFAULT_TEMPLATE_RESPONSE = """
{