Skip to content

Commit

Permalink
Merge pull request #163 from Bindambc/issue_#153
Browse files Browse the repository at this point in the history
#153 -> including support for other API versions >= v16.0.
  • Loading branch information
Bindambc authored May 6, 2024
2 parents f000f9d + 306194e commit 1196223
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 167 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/maven-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
Expand All @@ -26,4 +26,6 @@ jobs:
run: mvn -B package --file pom.xml
#coverage
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ hs_err_pid*
/.idea/
/target/
/src/test/java/com/whatsapp/api/TestConstants.java
/src/test/java/com/whatsapp/api/TestConstants.java
/htmlReport
/machinet.conf
16 changes: 5 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<com.squareup.retrofit2.version>2.9.0</com.squareup.retrofit2.version>
<org.mockito.version>5.2.0</org.mockito.version>
<org.junit.jupiter.version>5.10.1</org.junit.jupiter.version>
<com.squareup.retrofit2.version>2.11.0</com.squareup.retrofit2.version>
<org.mockito.version>5.11.0</org.mockito.version>
<org.junit.jupiter.version>5.10.2</org.junit.jupiter.version>
<com.squareup.okhttp3.version>5.0.0-alpha.12</com.squareup.okhttp3.version>

</properties>
Expand Down Expand Up @@ -81,12 +81,6 @@
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver3</artifactId>
Expand Down Expand Up @@ -117,7 +111,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<version>3.13.0</version>
<configuration>
<source>17</source>
<target>17</target>
Expand All @@ -142,7 +136,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<version>0.8.12</version>
<executions>
<execution>
<goals>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/whatsapp/api/WhatsappApiFactory.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
*
Expand Down
23 changes: 13 additions & 10 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 Down Expand Up @@ -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)//
Expand All @@ -70,8 +68,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 @@ -108,14 +107,18 @@ public static void setHttpProxy(String host, int port, String username, String p
* @return the s
*/
public static <S> S createService(Class<S> 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);
}

Expand All @@ -133,7 +136,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.getBaseDomain();
return createService(serviceClass, token, baseUrl);

}
Expand Down
22 changes: 22 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,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,53 @@
*/
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.
*
* @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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 25 additions & 9 deletions src/main/java/com/whatsapp/api/impl/WhatsappBusinessCloudApi.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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);

}
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -84,7 +100,7 @@ public UploadResponse uploadMedia(String phoneNumberId, String fileName, FileTyp
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media">official documentation</a>
*/
public Media retrieveMediaUrl(String mediaId) {
return executeSync(whatsappBusinessCloudApiService.retrieveMediaUrl(mediaId));
return executeSync(whatsappBusinessCloudApiService.retrieveMediaUrl(apiVersion.getValue(), mediaId));

}

Expand All @@ -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));
}

/**
Expand All @@ -122,7 +138,7 @@ public Response deleteMedia(String mediaId) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/guides/mark-message-as-read">official documentation</a>
*/
public Response markMessageAsRead(String phoneNumberId, ReadMessage message) {
return executeSync(whatsappBusinessCloudApiService.markMessageAsRead(phoneNumberId, message));
return executeSync(whatsappBusinessCloudApiService.markMessageAsRead(apiVersion.getValue(), phoneNumberId, message));
}

/**
Expand All @@ -135,7 +151,7 @@ public Response markMessageAsRead(String phoneNumberId, ReadMessage message) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/two-step-verification">official documentation</a>
*/
public Response twoStepVerification(String phoneNumberId, TwoStepCode twoStepCode) {
return executeSync(whatsappBusinessCloudApiService.twoStepVerification(phoneNumberId, twoStepCode));
return executeSync(whatsappBusinessCloudApiService.twoStepVerification(apiVersion.getValue(), phoneNumberId, twoStepCode));
}

}
Loading

0 comments on commit 1196223

Please sign in to comment.