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
12 changes: 12 additions & 0 deletions src/main/java/com/resend/core/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public BaseService(final String apiKey) {
this.resendMapper = new ResendMapper();
}

/**
* Constructs a BaseService instance with a provided HTTP client, intended for testing.
*
* @param apiKey The apiKey to use.
* @param httpClient The HTTP client to use.
*/
protected BaseService(final String apiKey, final IHttpClient httpClient) {
this.apiKey = apiKey;
this.httpClient = httpClient;
this.resendMapper = new ResendMapper();
}

/**
* Gets the HTTP client associated with this service instance.
*
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/com/resend/services/domains/DomainClaims.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.resend.services.domains;

import com.resend.core.exception.ResendException;
import com.resend.core.net.AbstractHttpResponse;
import com.resend.core.net.HttpMethod;
import com.resend.core.net.IHttpClient;
import com.resend.core.service.BaseService;
import com.resend.services.domains.model.ClaimDomainOptions;
import com.resend.services.domains.model.DomainClaimResponseSuccess;
import okhttp3.MediaType;

/**
* Represents the Resend Domain Claims module.
*/
public final class DomainClaims extends BaseService {

/**
* Constructs an instance of the {@code DomainClaims} class.
*
* @param apiKey The apiKey used for authentication.
*/
public DomainClaims(final String apiKey) {
super(apiKey);
}

/**
* Package-private constructor for testing, allowing injection of a mock HTTP client.
*
* @param apiKey The apiKey used for authentication.
* @param httpClient The HTTP client to use.
*/
DomainClaims(final String apiKey, final IHttpClient httpClient) {
super(apiKey, httpClient);
}

/**
* Claims a domain already verified by another team.
*
* @param claimDomainOptions The request object containing the domain claim details.
* @return A DomainClaimResponseSuccess representing the created claim.
* @throws ResendException If an error occurs during the domain claim process.
*/
public DomainClaimResponseSuccess create(ClaimDomainOptions claimDomainOptions) throws ResendException {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
if (claimDomainOptions == null) {
throw new ResendException("claimDomainOptions must not be null");
}
String payload = super.resendMapper.writeValue(claimDomainOptions);
AbstractHttpResponse<String> response = httpClient.perform("/domains/claim", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

String responseBody = response.getBody();
return resendMapper.readValue(responseBody, DomainClaimResponseSuccess.class);
}

/**
* Retrieves the latest claim for a domain.
*
* @param domainId The placeholder domain ID returned when the claim was created.
* @return A DomainClaimResponseSuccess representing the current claim state.
* @throws ResendException If an error occurs during the retrieval process.
*/
public DomainClaimResponseSuccess get(String domainId) throws ResendException {
AbstractHttpResponse<String> response = httpClient.perform("/domains/" + domainId + "/claim", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

String responseBody = response.getBody();
return resendMapper.readValue(responseBody, DomainClaimResponseSuccess.class);
}

/**
* Triggers DNS verification for a domain claim.
*
* @param domainId The placeholder domain ID returned when the claim was created.
* @return A DomainClaimResponseSuccess representing the claim after verification is triggered.
* @throws ResendException If an error occurs during the verification process.
*/
public DomainClaimResponseSuccess verify(String domainId) throws ResendException {
AbstractHttpResponse<String> response = httpClient.perform("/domains/" + domainId + "/claim/verify", super.apiKey, HttpMethod.POST, "", null);

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

String responseBody = response.getBody();
return resendMapper.readValue(responseBody, DomainClaimResponseSuccess.class);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/resend/services/domains/Domains.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ public UpdateDomainResponseSuccess update(UpdateDomainOptions updateDomainOption
return resendMapper.readValue(responseBody, UpdateDomainResponseSuccess.class);
}

/**
* Returns a DomainClaims object that can be used to interact with the Domain Claims service.
*
* @return A DomainClaims object.
*/
public DomainClaims claims() {
return new DomainClaims(apiKey);
}

/**
* Deletes a domain based on the provided domain ID and returns a RemoveDomainResponse.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package com.resend.services.domains.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Represents a request to claim a domain already verified by another team.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ClaimDomainOptions {

@JsonProperty("name")
private final String name;

@JsonProperty("region")
private final String region;

@JsonProperty("custom_return_path")
private final String customReturnPath;

@JsonProperty("open_tracking")
private final Boolean openTracking;

@JsonProperty("click_tracking")
private final Boolean clickTracking;

@JsonProperty("tracking_subdomain")
private final String trackingSubdomain;

/**
* Constructs a ClaimDomainOptions object using the provided builder.
*
* @param builder The builder to construct the ClaimDomainOptions from.
*/
public ClaimDomainOptions(Builder builder) {
this.name = builder.name;
this.region = builder.region;
this.customReturnPath = builder.customReturnPath;
this.openTracking = builder.openTracking;
this.clickTracking = builder.clickTracking;
this.trackingSubdomain = builder.trackingSubdomain;
}

/**
* Get the domain name to claim.
*
* @return The domain name.
*/
public String getName() {
return name;
}

/**
* Get the region where emails will be sent from.
*
* @return The region.
*/
public String getRegion() {
return region;
}

/**
* Get the custom return path subdomain.
*
* @return The custom return path.
*/
public String getCustomReturnPath() {
return customReturnPath;
}

/**
* Get whether open tracking is enabled.
*
* @return The open tracking setting.
*/
public Boolean getOpenTracking() {
return openTracking;
}

/**
* Get whether click tracking is enabled.
*
* @return The click tracking setting.
*/
public Boolean getClickTracking() {
return clickTracking;
}

/**
* Get the subdomain used for click and open tracking.
*
* @return The tracking subdomain.
*/
public String getTrackingSubdomain() {
return trackingSubdomain;
}

/**
* Create a new builder instance for constructing ClaimDomainOptions objects.
*
* @return A new builder instance.
*/
public static Builder builder() {
return new Builder();
}

/**
* Builder class for constructing ClaimDomainOptions objects.
*/
public static class Builder {

/**
* Creates a new Builder instance.
*/
public Builder() {
}

private String name;
private String region;
private String customReturnPath;
private Boolean openTracking;
private Boolean clickTracking;
private String trackingSubdomain;

/**
* Set the domain name to claim.
*
* @param name The domain name.
* @return The builder instance.
*/
public Builder name(String name) {
this.name = name;
return this;
}

/**
* Set the region where emails will be sent from.
*
* @param region The region.
* @return The builder instance.
*/
public Builder region(String region) {
this.region = region;
return this;
}

/**
* Set the custom return path subdomain.
*
* @param customReturnPath The custom return path.
* @return The builder instance.
*/
public Builder customReturnPath(String customReturnPath) {
this.customReturnPath = customReturnPath;
return this;
}

/**
* Set whether open tracking is enabled.
*
* @param openTracking The open tracking setting.
* @return The builder instance.
*/
public Builder openTracking(Boolean openTracking) {
this.openTracking = openTracking;
return this;
}

/**
* Set whether click tracking is enabled.
*
* @param clickTracking The click tracking setting.
* @return The builder instance.
*/
public Builder clickTracking(Boolean clickTracking) {
this.clickTracking = clickTracking;
return this;
}

/**
* Set the subdomain used for click and open tracking.
*
* @param trackingSubdomain The tracking subdomain.
* @return The builder instance.
*/
public Builder trackingSubdomain(String trackingSubdomain) {
this.trackingSubdomain = trackingSubdomain;
return this;
}

/**
* Build a new ClaimDomainOptions object.
*
* @return A new ClaimDomainOptions object.
*/
public ClaimDomainOptions build() {
return new ClaimDomainOptions(this);
}
}
}
Loading
Loading