Skip to content

Commit

Permalink
#153 -> including support for other API versions >= v16.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bindambc committed Jan 27, 2024
1 parent d8665f2 commit e083c42
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 31 deletions.
17 changes: 8 additions & 9 deletions src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand All @@ -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)//
Expand All @@ -66,8 +64,9 @@ public static OkHttpClient createDefaultHttpClient(){
* you can pass {@code null} as parameters for {@code username} and {@code pwd}.
* </ul>
* <p>
*
* @param host the host (Not null)
* @param port the port
* @param port the port
* @param username the username
* @param pwd the pwd
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-selector/">Proxy Selector</a>
Expand Down Expand Up @@ -129,7 +128,7 @@ public static <S> S createService(Class<S> serviceClass, String token, String ba
*/
public static <S> S createService(Class<S> serviceClass, String token) {

var baseUrl = WhatsappApiConfig.BASE_DOMAIN;
var baseUrl = WhatsappApiConfig.BASE_DOMAIN + WhatsappApiConfig.API_VERSION + "/";
return createService(serviceClass, token, baseUrl);

}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/whatsapp/api/configuration/ApiVersion.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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<MessageResponse> sendMessage(@Path("Phone-Number-ID") String phoneNumberId, @Body Message message);

/**
Expand All @@ -47,7 +45,7 @@ public interface WhatsappBusinessCloudApiService {
* @return the call
*/
@Multipart
@POST("/" + API_VERSION + "/{Phone-Number-ID}/media")
@POST( "/{Phone-Number-ID}/media")
Call<UploadResponse> uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, @Part MultipartBody.Part file,
@Part MultipartBody.Part messageProduct);

Expand All @@ -57,7 +55,7 @@ Call<UploadResponse> uploadMedia(@Path("Phone-Number-ID") String phoneNumberId,
* @param mediaId the media id
* @return the call
*/
@GET("/" + API_VERSION + "/{media-id}")
@GET("/{media-id}")
Call<Media> retrieveMediaUrl(@Path("media-id") String mediaId);

/**
Expand All @@ -77,7 +75,7 @@ Call<UploadResponse> uploadMedia(@Path("Phone-Number-ID") String phoneNumberId,
* @param mediaId the media id
* @return the call
*/
@DELETE("/" + API_VERSION + "/{media-id}")
@DELETE("/{media-id}")
Call<Response> deleteMedia(@Path("media-id") String mediaId);

/**
Expand All @@ -87,7 +85,7 @@ Call<UploadResponse> 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<Response> markMessageAsRead(@Path("Phone-Number-ID") String phoneNumberId, @Body ReadMessage message);

/**
Expand All @@ -97,7 +95,7 @@ Call<UploadResponse> 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<Response> twoStepVerification(@Path("Phone-Number-ID") String phoneNumberId, @Body TwoStepCode twoStepCode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import java.util.Map;

import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION;

/**
* The interface Whatsapp business management api service.
*/
Expand All @@ -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<Template> createMessageTemplate(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Body MessageTemplate messageTemplate);

/**
Expand All @@ -47,7 +45,7 @@ public interface WhatsappBusinessManagementApiService {
* @param messageTemplate the message template
* @return the call
*/
@POST("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates/{message-template-id}")
@POST("/{whatsapp-business-account-ID}/message_templates/{message-template-id}")
Call<Template> updateMessageTemplate(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Path("message-template-id") String messageTemplateId, @Body MessageTemplate messageTemplate);

/**
Expand All @@ -57,7 +55,7 @@ public interface WhatsappBusinessManagementApiService {
* @param name the name
* @return the call
*/
@DELETE("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates")
@DELETE("/{whatsapp-business-account-ID}/message_templates")
Call<Response> deleteMessageTemplate(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @Query("name") String name);

/**
Expand All @@ -66,7 +64,7 @@ public interface WhatsappBusinessManagementApiService {
* @param whatsappBusinessAccountId the whatsapp business account id
* @return the call
*/
@GET("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates")
@GET("/{whatsapp-business-account-ID}/message_templates")
Call<MessageTemplates> retrieveTemplates(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId);

/**
Expand All @@ -76,7 +74,7 @@ public interface WhatsappBusinessManagementApiService {
* @param filters the filters
* @return the call
*/
@GET("/" + API_VERSION + "/{whatsapp-business-account-ID}/message_templates")
@GET("/{whatsapp-business-account-ID}/message_templates")
Call<MessageTemplates> retrieveTemplates(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId, @QueryMap Map<String, Object> filters);


Expand All @@ -87,7 +85,7 @@ public interface WhatsappBusinessManagementApiService {
* @param queryParams the query params
* @return the call
*/
@GET("/" + API_VERSION + "/{phone-number-ID}")
@GET("/{phone-number-ID}")
Call<PhoneNumber> retrievePhoneNumber(@Path("phone-number-ID") String phoneNumberId, @QueryMap Map<String, Object> queryParams);


Expand All @@ -97,7 +95,7 @@ public interface WhatsappBusinessManagementApiService {
* @param whatsappBusinessAccountId the whatsapp business account id
* @return the call
*/
@GET("/" + API_VERSION + "/{whatsapp-business-account-ID}/phone_numbers")
@GET("/{whatsapp-business-account-ID}/phone_numbers")
Call<PhoneNumbers> retrievePhoneNumbers(@Path("whatsapp-business-account-ID") String whatsappBusinessAccountId);

/**
Expand All @@ -107,7 +105,7 @@ public interface WhatsappBusinessManagementApiService {
* @param requestCode the request code
* @return the call
*/
@POST("/" + API_VERSION + "/{phone-number-ID}/request_code")
@POST("/{phone-number-ID}/request_code")
Call<Response> requestCode(@Path("phone-number-ID") String phoneNumberId, @Body RequestCode requestCode);

/**
Expand All @@ -117,7 +115,7 @@ public interface WhatsappBusinessManagementApiService {
* @param verifyCode the verify code
* @return the call
*/
@POST("/" + API_VERSION + "/{phone-number-ID}/verify_code")
@POST("/{phone-number-ID}/verify_code")
Call<Response> verifyCode(@Path("phone-number-ID") String phoneNumberId, @Body VerifyCode verifyCode);

/**
Expand All @@ -127,7 +125,7 @@ public interface WhatsappBusinessManagementApiService {
* @param queryParams the query params
* @return the call
*/
@GET("/" + API_VERSION + "/{phone-number-ID}/whatsapp_commerce_settings")
@GET("/{phone-number-ID}/whatsapp_commerce_settings")
Call<GraphCommerceSettings> getWhatsappCommerceSettings(@Path("phone-number-ID") String phoneNumberId, @QueryMap Map<String, String> queryParams);

/**
Expand All @@ -137,6 +135,6 @@ public interface WhatsappBusinessManagementApiService {
* @param commerceDataItem the query params
* @return the call
*/
@POST("/" + API_VERSION + "/{phone-number-ID}/whatsapp_commerce_settings")
@POST("/{phone-number-ID}/whatsapp_commerce_settings")
Call<Response> updateWhatsappCommerceSettings(@Path("phone-number-ID") String phoneNumberId, @Body CommerceDataItem commerceDataItem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit e083c42

Please sign in to comment.