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
49 changes: 49 additions & 0 deletions src/main/java/com/resend/core/helper/URLHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.resend.core.helper;

import com.resend.core.net.ListParams;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* URL Helper for handling query params.
*/
public class URLHelper {

/**
* Parses the given {@link ListParams} object into a query string.
*
* @param params The {@link ListParams} instance (can be null).
* @return A query string starting with "?" if parameters exist, or an empty string otherwise.
*/
public static String parse(ListParams params) {
if (params == null) {
return "";
}

Map<String, String> queryParams = new LinkedHashMap<>();

if (params.getLimit() != null) {
queryParams.put("limit", params.getLimit().toString());
}
if (params.getAfter() != null && !params.getAfter().isEmpty()) {
queryParams.put("after", params.getAfter());
}
if (params.getBefore() != null && !params.getBefore().isEmpty()) {
queryParams.put("before", params.getBefore());
}

if (queryParams.isEmpty()) {
return "";
}

String query = queryParams.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));

return "?" + query;
}


}
123 changes: 123 additions & 0 deletions src/main/java/com/resend/core/net/ListParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.resend.core.net;

import com.resend.services.broadcasts.model.SendBroadcastOptions;

/**
* Represents a class that wraps the parameters for the list method.
*/
public class ListParams {

/**
* The maximum number of emails to return. Defaults to 10, maximum 100.
*/
private final Integer limit;

/**
* Return emails after this cursor for pagination.
*/
private final String after;

/**
* Return emails before this cursor for pagination.
*/
private final String before;

/**
* Constructs a ListParams object using the provided builder.
*
* @param builder The builder to construct the ListParams.
*/
public ListParams(Builder builder) {
this.limit = builder.limit;
this.after = builder.after;
this.before = builder.before;
}

/**
* Get the pagination limit.
*
* @return pagination limit.
*/
public Integer getLimit() {
return limit;
}

/**
* Get emails after this cursor for pagination.
* @return email after this cursor.
*/
public String getAfter() {
return after;
}

/**
* Get emails before this cursor for pagination.
* @return email before this cursor.
*/
public String getBefore() {
return before;
}

/**
* Builder class for constructing ListParams objects.
*/
public static class Builder {
private Integer limit;

private String before;

private String after;


/**
* Set the maximum number of emails to return.
*
* @param limit The maximum number of emails.
* @return The builder instance.
*/
public Builder limit(Integer limit) {
this.limit = limit;
return this;
}

/**
* Set the emails before this cursor for pagination.
*
* @param before The emails after this cursor for pagination.
* @return The builder instance.
*/
public Builder before(String before) {
this.before = before;
return this;
}

/**
* Set the emails after this cursor for pagination.
*
* @param after The emails after this cursor for pagination.
* @return The builder instance.
*/
public Builder after(String after) {
this.after = after;
return this;
}

/**
* Builds an instance of {@link ListParams} with the specified options.
*
* @return A new {@link ListParams} instance.
*/
public ListParams build() {
return new ListParams(this);
}
}

/**
* Create a new builder instance for constructing ListParams objects.
*
* @return A new builder instance.
*/
public static Builder builder() {
return new Builder();
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/resend/core/net/RequestOptions.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.resend.core.net;

import com.resend.services.domains.model.CreateDomainOptions;

/**
* Represents a request to create a request options.
*/
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/resend/services/apikeys/ApiKeys.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.resend.services.apikeys;

import com.resend.core.exception.ResendException;
import com.resend.core.helper.URLHelper;
import com.resend.core.net.AbstractHttpResponse;
import com.resend.core.net.HttpMethod;
import com.resend.core.net.ListParams;
import com.resend.core.service.BaseService;
import com.resend.services.apikeys.model.CreateApiKeyResponse;
import com.resend.services.apikeys.model.CreateApiKeyOptions;
Expand Down Expand Up @@ -63,6 +65,27 @@ public ListApiKeysResponse list() throws ResendException {
return listApiKeysResponse;
}

/**
* Retrieves a paginated list of api keys and returns a ListApiKeysResponse.
* @param params The params used to customize the list.
*
* @return A ListApiKeysResponse containing the paginated list of api keys.
* @throws ResendException If an error occurs during the api keys list retrieval process.
*/
public ListApiKeysResponse list(ListParams params) throws ResendException {
String pathWithQuery = "/api-keys" + URLHelper.parse(params);
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException("Failed to retrieve api keys: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();

ListApiKeysResponse listApiKeysResponse = resendMapper.readValue(responseBody, ListApiKeysResponse.class);
return listApiKeysResponse;
}

/**
* Deletes an api key based on the provided api key ID and returns a boolean response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class ListApiKeysResponse {
@JsonProperty("data")
private List<ApiKey> data;

@JsonProperty("has_more")
private Boolean hasMore;

@JsonProperty("object")
private String object;

/**
* Default constructor. Creates an instance of ListApiKeysResponse with an empty data list.
*/
Expand All @@ -22,9 +28,13 @@ public ListApiKeysResponse() {
* Creates an instance of ListApiKeyResponse with the specified data.
*
* @param data The list of API key items.
* @param hasMore Indicate if there are more items to be returned.
* @param object the object type of the module.
*/
public ListApiKeysResponse(List<ApiKey> data) {
public ListApiKeysResponse(List<ApiKey> data, Boolean hasMore, String object) {
this.data = data;
this.hasMore = hasMore;
this.object = object;
}

/**
Expand All @@ -35,5 +45,23 @@ public ListApiKeysResponse(List<ApiKey> data) {
public List<ApiKey> getData() {
return data;
}

/**
* Gets the indicator whether there are more items available for pagination.
*
* @return Whether there are more items available for pagination.
*/
public Boolean hasMore() {
return hasMore;
}

/**
* Get the type of the object.
*
* @return The type of the object.
*/
public String getObject() {
return object;
}
}

22 changes: 22 additions & 0 deletions src/main/java/com/resend/services/audiences/Audiences.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.resend.services.audiences;

import com.resend.core.exception.ResendException;
import com.resend.core.helper.URLHelper;
import com.resend.core.net.AbstractHttpResponse;
import com.resend.core.net.HttpMethod;
import com.resend.core.net.ListParams;
import com.resend.core.service.BaseService;
import com.resend.services.audiences.model.*;
import okhttp3.MediaType;
Expand Down Expand Up @@ -58,6 +60,26 @@ public ListAudiencesResponseSuccess list() throws ResendException {
return resendMapper.readValue(responseBody, ListAudiencesResponseSuccess.class);
}

/**
* Retrieves a paginated list of audiences and returns a ListAudiencesResponseSuccess.
* @param params The params used to customize the list.
*
* @return A ListAudiencesResponseSuccess containing the paginated list of audiences.
* @throws ResendException If an error occurs during the audiences list retrieval process.
*/
public ListAudiencesResponseSuccess list(ListParams params) throws ResendException {
String pathWithQuery = "/audiences" + URLHelper.parse(params);
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException("Failed to retrieve audiences: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();

return resendMapper.readValue(responseBody, ListAudiencesResponseSuccess.class);
}

/**
* Retrieves a audience by its unique identifier.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class ListAudiencesResponseSuccess {
@JsonProperty("object")
private String object;

@JsonProperty("has_more")
private Boolean hasMore;

/**
* Default constructor
*/
Expand All @@ -26,10 +29,12 @@ public ListAudiencesResponseSuccess() {
*
* @param data The list of audiences.
* @param object The object of the audiences.
* @param hasMore Indicate if there are more items to be returned.
*/
public ListAudiencesResponseSuccess(List<Audience> data, String object) {
public ListAudiencesResponseSuccess(List<Audience> data, String object, Boolean hasMore) {
this.data = data;
this.object = object;
this.hasMore = hasMore;
}

/**
Expand All @@ -49,4 +54,13 @@ public List<Audience> getData() {
public String getObject() {
return object;
}

/**
* Gets the indicator whether there are more items available for pagination.
*
* @return Whether there are more items available for pagination.
*/
public Boolean hasMore() {
return hasMore;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/resend/services/broadcasts/Broadcasts.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.resend.services.broadcasts;

import com.resend.core.exception.ResendException;
import com.resend.core.helper.URLHelper;
import com.resend.core.net.AbstractHttpResponse;
import com.resend.core.net.HttpMethod;
import com.resend.core.net.ListParams;
import com.resend.core.service.BaseService;
import com.resend.services.broadcasts.model.*;
import okhttp3.MediaType;
Expand Down Expand Up @@ -117,6 +119,26 @@ public ListBroadcastsResponseSuccess list() throws ResendException {
return resendMapper.readValue(responseBody, ListBroadcastsResponseSuccess.class);
}

/**
* Retrieves a paginated list of broadcasts and returns a List.
* @param params The params used to customize the list.
*
* @return A ListBroadcastsResponseSuccess containing the paginated list of broadcasts.
* @throws ResendException If an error occurs during the broadcasts list retrieval process.
*/
public ListBroadcastsResponseSuccess list(ListParams params) throws ResendException {
String pathWithQuery = "/broadcasts" + URLHelper.parse(params);
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException("Failed to retrieve broadcasts: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();

return resendMapper.readValue(responseBody, ListBroadcastsResponseSuccess.class);
}

/**
* Updates a Broadcast.
*
Expand Down
Loading