Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import com.ararahq.arara.sdk.Arara;
// Criar o cliente
Arara arara = Arara.builder()
.apiKey("sua-chave-api-aqui")
.baseUrl("https://api.ararahq.com/api")
.baseUrl("https://api.ararahq.com")
.build();
```

Expand Down
2 changes: 2 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lombok.addLombokGeneratedAnnotation = true
config.stopBubbling = true
21 changes: 21 additions & 0 deletions src/main/java/com/ararahq/arara/sdk/Arara.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import com.ararahq.arara.sdk.config.AraraConfig;
import com.ararahq.arara.sdk.http.AraraHttpClient;
import com.ararahq.arara.sdk.services.ApiKeyService;
import com.ararahq.arara.sdk.services.CampaignService;
import com.ararahq.arara.sdk.services.ContactService;
import com.ararahq.arara.sdk.services.ConversationService;
import com.ararahq.arara.sdk.services.MessageService;
import com.ararahq.arara.sdk.services.NumberService;
import com.ararahq.arara.sdk.services.OrganizationService;
import com.ararahq.arara.sdk.services.SmartLinkService;
import com.ararahq.arara.sdk.services.TemplateService;
import com.ararahq.arara.sdk.services.UserService;
import com.ararahq.arara.sdk.services.WalletService;
import lombok.Getter;

import java.time.Duration;
Expand All @@ -20,6 +27,13 @@ public class Arara {
private final UserService users;
private final CampaignService campaigns;
private final TemplateService templates;
private final ContactService contacts;
private final ConversationService conversations;
private final WalletService wallet;
private final NumberService numbers;
private final SmartLinkService smartLinks;
private final ApiKeyService apiKeys;
private final OrganizationService organizations;

private Arara(AraraConfig config) {
if (config.getApiKey() == null || config.getApiKey().isEmpty()) {
Expand All @@ -31,6 +45,13 @@ private Arara(AraraConfig config) {
this.users = new UserService(httpClient);
this.campaigns = new CampaignService(httpClient);
this.templates = new TemplateService(httpClient);
this.contacts = new ContactService(httpClient);
this.conversations = new ConversationService(httpClient);
this.wallet = new WalletService(httpClient);
this.numbers = new NumberService(httpClient);
this.smartLinks = new SmartLinkService(httpClient);
this.apiKeys = new ApiKeyService(httpClient);
this.organizations = new OrganizationService(httpClient);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Builder
public class AraraConfig {
@Builder.Default
String baseUrl = "https://api.ararahq.com/api";
String baseUrl = "https://api.ararahq.com";

String apiKey;

Expand Down
98 changes: 80 additions & 18 deletions src/main/java/com/ararahq/arara/sdk/http/AraraHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,56 @@ public AraraHttpClient(AraraConfig config) {
* Performs a GET request.
*/
public <T> T get(String path, Class<T> responseType) {
Request request = new Request.Builder()
.url(baseUrl + path)
.get()
.build();
return execute(request, responseType);
return execute(buildGet(path), responseType);
}

/**
* Performs a GET request with a generic response type.
*/
public <T> T get(String path, TypeReference<T> responseType) {
return execute(buildGet(path), responseType);
}

/**
* Performs a POST request with JSON body.
*/
public <T> T post(String path, Object body, Class<T> responseType) {
return requestWithBody("POST", path, body, responseType);
return execute(buildBody("POST", path, body), responseType);
}

/**
* Performs a POST request with a generic response type.
*/
public <T> T post(String path, Object body, TypeReference<T> responseType) {
return execute(buildBody("POST", path, body), responseType);
}

/**
* Performs a PUT request with JSON body.
*/
public <T> T put(String path, Object body, Class<T> responseType) {
return execute(buildBody("PUT", path, body), responseType);
}

/**
* Performs a PUT request with a generic response type.
*/
public <T> T put(String path, Object body, TypeReference<T> responseType) {
return execute(buildBody("PUT", path, body), responseType);
}

/**
* Performs a PATCH request with JSON body.
*/
public <T> T patch(String path, Object body, Class<T> responseType) {
return requestWithBody("PATCH", path, body, responseType);
return execute(buildBody("PATCH", path, body), responseType);
}

/**
* Performs a PATCH request with a generic response type.
*/
public <T> T patch(String path, Object body, TypeReference<T> responseType) {
return execute(buildBody("PATCH", path, body), responseType);
}

/**
Expand All @@ -88,33 +119,64 @@ public void delete(String path) {
execute(request, Void.class);
}

private <T> T requestWithBody(String method, String path, Object body, Class<T> responseType) {
try {
String json = objectMapper.writeValueAsString(body);
RequestBody requestBody = RequestBody.create(json, JSON);
private Request buildGet(String path) {
return new Request.Builder()
.url(baseUrl + path)
.get()
.build();
}

Request request = new Request.Builder()
private Request buildBody(String method, String path, Object body) {
try {
RequestBody requestBody = body == null
? RequestBody.create("", JSON)
: RequestBody.create(objectMapper.writeValueAsString(body), JSON);
return new Request.Builder()
.url(baseUrl + path)
.method(method, requestBody)
.build();

return execute(request, responseType);
} catch (IOException e) {
throw new AraraException("Error serializing object to JSON", e);
}
}

private <T> T execute(Request request, Class<T> responseType) {
if (responseType == Void.class) {
call(request, true);
return null;
}
String body = call(request, false);
if (body == null || body.isEmpty()) {
return null;
}
try {
return objectMapper.readValue(body, responseType);
} catch (IOException e) {
throw new AraraNetworkException("Error deserializing Arara API response", e);
}
}

private <T> T execute(Request request, TypeReference<T> responseType) {
String body = call(request, false);
if (body == null || body.isEmpty()) {
return null;
}
try {
return objectMapper.readValue(body, responseType);
} catch (IOException e) {
throw new AraraNetworkException("Error deserializing Arara API response", e);
}
}

private String call(Request request, boolean discardBody) {
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
handleErrorResponse(response);
}

if (responseType == Void.class || response.body() == null) {
if (discardBody || response.body() == null) {
return null;
}

return objectMapper.readValue(response.body().string(), responseType);
return response.body().string();
} catch (IOException e) {
log.error("Network error accessing Arara API: {}", request.url(), e);
throw new AraraNetworkException("Communication failure with Arara API", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ararahq.arara.sdk.models;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.math.BigDecimal;
import java.time.Instant;

/**
* Current auto-recharge configuration for the wallet.
*/
@Value
@Builder
@Jacksonized
public class AutoRechargeSettings {
boolean enabled;
BigDecimal threshold;
BigDecimal amount;
Instant lastAttemptAt;
String lastFailureReason;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ararahq.arara.sdk.models;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.util.List;
import java.util.Map;

/**
* Payload for patching the organization's WhatsApp business profile.
*/
@Value
@Builder
@Jacksonized
public class BusinessProfilePatch {
String displayName;
String vertical;
String description;
String aboutShort;
String email;
List<String> websites;
Map<String, Object> businessHours;
String profilePhotoUrl;
String awayMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ararahq.arara.sdk.models;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* The organization's WhatsApp business profile and Meta sync state.
*/
@Value
@Builder
@Jacksonized
public class BusinessProfileResponse {
UUID id;
String displayName;
String vertical;
String description;
String aboutShort;
String email;
List<String> websites;
Map<String, Object> businessHours;
String profilePhotoUrl;
String awayMessage;
String metaSyncStatus;
Instant metaSyncedAt;
String metaSyncError;
Instant updatedAt;
}
21 changes: 21 additions & 0 deletions src/main/java/com/ararahq/arara/sdk/models/ContactMessageItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ararahq.arara.sdk.models;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.util.UUID;

/**
* A single message exchanged with a contact.
*/
@Value
@Builder
@Jacksonized
public class ContactMessageItem {
UUID id;
String direction;
String status;
String templateName;
String body;
String createdAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ararahq.arara.sdk.models;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.util.List;

/**
* Recent message history for a contact.
*/
@Value
@Builder
@Jacksonized
public class ContactMessagesResponse {
String phone;
int total;
List<ContactMessageItem> messages;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ararahq.arara.sdk.models;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.util.List;

/**
* Payload for patching an existing contact.
*/
@Value
@Builder
@Jacksonized
public class ContactPatchRequest {
String name;
String email;
List<String> tags;
}
19 changes: 19 additions & 0 deletions src/main/java/com/ararahq/arara/sdk/models/ContactRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ararahq.arara.sdk.models;

import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import java.util.Map;

/**
* Payload for creating or updating a contact in a batch import.
*/
@Value
@Builder
@Jacksonized
public class ContactRequest {
String name;
String phone;
String email;
Map<String, Object> attributes;
}
Loading
Loading