Skip to content

Commit

Permalink
Merge branch 'main' into fix/ignore_unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
Bindambc authored May 7, 2024
2 parents 3db038e + f7f2cd1 commit a529f8d
Show file tree
Hide file tree
Showing 30 changed files with 672 additions and 347 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/maven-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ jobs:
#coverage
- name: Upload coverage reports to Codecov
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
19 changes: 6 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@
<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.okhttp3.version>5.0.0-alpha.11</com.squareup.okhttp3.version>
<jackson.version>2.16.0</jackson.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>

<distributionManagement>
Expand Down Expand Up @@ -98,12 +97,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 @@ -134,7 +127,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 @@ -159,7 +152,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
43 changes: 30 additions & 13 deletions src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java
Original file line number Diff line number Diff line change
@@ -1,14 +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.whatsapp.api.utils.proxy.CustomProxyAuthenticator;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
Expand All @@ -28,7 +29,14 @@
public class WhatsappApiServiceGenerator {

static OkHttpClient sharedClient;
private static final Converter.Factory converterFactory = JacksonConverterFactory.create();
private static final Converter.Factory converterFactory = JacksonConverterFactory.create(
new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false)
.configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false)
.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false)
);

@SuppressWarnings("unchecked")
private static final Converter<ResponseBody, WhatsappApiError> errorBodyConverter = (Converter<ResponseBody, WhatsappApiError>) converterFactory.responseBodyConverter(WhatsappApiError.class, new Annotation[0], null);

Expand All @@ -37,8 +45,11 @@ private WhatsappApiServiceGenerator() {
}

static {
sharedClient = createDefaultHttpClient();
}

sharedClient = new OkHttpClient.Builder()//
public static OkHttpClient createDefaultHttpClient() {
return new OkHttpClient.Builder()//
.callTimeout(20, TimeUnit.SECONDS)//
.pingInterval(20, TimeUnit.SECONDS)//
.build();
Expand All @@ -57,29 +68,31 @@ private WhatsappApiServiceGenerator() {
* 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 (Not null)
* @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>
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-authenticator/">Proxy Authenticator</a>
*/
public static void setHttpProxy(String host, int port, String username, String pwd) {
Objects.requireNonNull(host, "Host cannot be null");
Objects.requireNonNull(port, "Http Port cannot be null");
CustomHttpProxySelector proxySelector = new CustomHttpProxySelector(host, port);

sharedClient = sharedClient.newBuilder()
.proxySelector(proxySelector)
.build();

if (username == null || pwd == null) {
sharedClient = sharedClient.newBuilder()
.proxySelector(proxySelector)
.build();
//Without authentication
return;
}

CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator(username, pwd);

sharedClient = sharedClient.newBuilder()
.authenticator(proxyAuthenticator)
.proxyAuthenticator(proxyAuthenticator)
.build();
}

Expand All @@ -94,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 @@ -119,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
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public record Message(

@JsonProperty("id") String id) {
@JsonProperty("id") String id,
@JsonProperty("message_status") String messageStatus)
{
}
34 changes: 22 additions & 12 deletions src/main/java/com/whatsapp/api/domain/phone/PhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.phone.type.NameStatusType;
import com.whatsapp.api.domain.phone.type.PlatformType;
import com.whatsapp.api.domain.phone.type.QualityRatingType;

/**
* The type Phone number.
*
* @param id The ID associated with the phone number.
* @param displayPhoneNumber The string representation of the phone number.
* @param nameStatus The current status of the review of your business name.
* @param id The ID associated with the phone number.
* @param displayPhoneNumber The string representation of the phone number.
* @param nameStatus The current status of the review of your business name.
* @param codeVerificationStatus Code Verification Status
* @param qualityRating The quality rating of the phone number based on how messages have been received by recipients in recent days. Valid values are:
* <ul>
* <li>Green: High Quality</li>
* <li>Yellow: Medium Quality</li>
* <li> Red: Low Quality</li>
* <li>NA: Quality has not been determined</li>
* </ul>
* @param verifiedName the verified name
* @param qualityRating The quality rating of the phone number based on how messages have been received by recipients in recent days. Valid values are:
* <ul>
* <li>Green: High Quality</li>
* <li>Yellow: Medium Quality</li>
* <li>Red: Low Quality</li>
* <li>NA: Quality has not been determined</li>
* </ul>
* @param verifiedName the verified name
* @param platformType Platform the business phone number is registered with.
* @param throughput The business phone number's Cloud API throughput level.
* @see <a href="https://www.facebook.com/business/help/896873687365001">About WhatsApp Business Account Message Quality Rating</a>
* @see <a href="https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account-to-number-current-status/">WhatsApp Business Phone Number</a>
* @see <a href="https://developers.facebook.com/docs/whatsapp/business-platform/changelog/#september-12--2023">WhatsApp Business Platform - Changelog - September 12, 2023</a>
*/
@JsonInclude(value = Include.NON_NULL)
public record PhoneNumber(
Expand All @@ -36,5 +41,10 @@ public record PhoneNumber(

@JsonProperty("code_verification_status") String codeVerificationStatus,

@JsonProperty("name_status") NameStatusType nameStatus) {
@JsonProperty("name_status") NameStatusType nameStatus,

@JsonProperty("platform_type") PlatformType platformType,

@JsonProperty("throughput") Throughput throughput)
{
}
16 changes: 16 additions & 0 deletions src/main/java/com/whatsapp/api/domain/phone/Throughput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.whatsapp.api.domain.phone;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.phone.type.LevelType;

/**
* The type Throughput.
*/
@JsonInclude(value = Include.NON_NULL)
public record Throughput(

@JsonProperty("level") LevelType Level)
{
}
Loading

0 comments on commit a529f8d

Please sign in to comment.