Skip to content

Commit

Permalink
feat: Add patch for contacts (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
kewynakshlley committed Dec 29, 2023
1 parent fd0eb5c commit 1f6c130
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 105 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/resend/core/net/HttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public enum HttpMethod {
* HTTP DELETE method.
*/
DELETE,

/**
* HTTP PATCH method.
*/
PATCH,
}
36 changes: 28 additions & 8 deletions src/main/java/com/resend/services/contacts/Contacts.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public Contacts(final String apiKey) {
/**
* Creates a Contact.
*
* @param createContactRequestOptions The Contact details.
* @param createContactOptions The Contact details.
* @return The details of the created contact.
* @throws ResendException If an error occurs during the Contact creation process.
*/
public CreateContactResponseSuccess create(CreateContactRequestOptions createContactRequestOptions) throws ResendException {
String payload = super.resendMapper.writeValue(createContactRequestOptions);
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" +createContactRequestOptions.getAudienceId()+ "/contacts" , super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));
public CreateContactResponseSuccess create(CreateContactOptions createContactOptions) throws ResendException {
String payload = super.resendMapper.writeValue(createContactOptions);
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" + createContactOptions.getAudienceId()+ "/contacts" , super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException("Failed to create contact: " + response.getCode() + " " + response.getBody());
Expand Down Expand Up @@ -62,11 +62,11 @@ public ListContactsResponseSuccess list(String audienceId) throws ResendExceptio
/**
* Retrieves a contact by its unique identifier.
*
* @param params The object with identifier of the contact to delete and its audience id.
* @param params The object with identifier of the contact to retrieve and.
* @return The retrieved contact details.
* @throws ResendException If an error occurs while retrieving the contact.
*/
public GetContactResponseSuccess get(ContactRequestOptions params) throws ResendException {
public GetContactResponseSuccess get(GetContactOptions params) throws ResendException {
AbstractHttpResponse<String> response = this.httpClient.perform("/audiences/" +params.getAudienceId()+ "/contacts/" +params.getId(), super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
Expand All @@ -81,11 +81,11 @@ public GetContactResponseSuccess get(ContactRequestOptions params) throws Resend
/**
* Deletes a contact based on the provided contact ID.
*
* @param params The object with identifier of the contact to delete and its audience id.
* @param params The object with identifier of the contact to delete.
* @return The RemoveContactsResponseSuccess with the details of the removed contact.
* @throws ResendException If an error occurs during the contact deletion process.
*/
public RemoveContactResponseSuccess remove(ContactRequestOptions params) throws ResendException {
public RemoveContactResponseSuccess remove(RemoveContactOptions params) throws ResendException {
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" +params.getAudienceId()+ "/contacts/" +params.getId(), super.apiKey, HttpMethod.DELETE, "", null);

if (!response.isSuccessful()) {
Expand All @@ -96,4 +96,24 @@ public RemoveContactResponseSuccess remove(ContactRequestOptions params) throws

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

/**
* Updates a contact based on the provided contact ID.
*
* @param params The object with identifier of the contact to patch.
* @return The UpdateContactResponseSuccess with the details of the patched contact.
* @throws ResendException If an error occurs during the contact patching process.
*/
public UpdateContactResponseSuccess update(UpdateContactOptions params) throws ResendException {
String payload = super.resendMapper.writeValue(params);
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" +params.getAudienceId()+ "/contacts/" +params.getId(), super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json"));

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

String responseBody = response.getBody();

return resendMapper.readValue(responseBody, UpdateContactResponseSuccess.class);
}
}
133 changes: 133 additions & 0 deletions src/main/java/com/resend/services/contacts/model/ContactOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.resend.services.contacts.model;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Common superclass for contact options.
*/
public abstract class ContactOptions {

/**
* The id of the contact options
*/
@JsonProperty("id")
protected final String id;

/**
* The audience_id of the contact options
*/
@JsonProperty("audience_id")
protected final String audienceId;

/**
* The email of the contact options
*/
@JsonProperty("email")
protected final String email;

/**
* Constructs a ContactOptions object using the provided builder.
*
* @param builder The builder to construct the ContactOptions.
*/
protected ContactOptions(Builder builder) {
this.id = builder.id;
this.audienceId = builder.audienceId;
this.email = builder.email;
}

/**
* Get the id of the ContactOptions.
*
* @return The id of the ContactOptions.
*/
public String getId() {
return id;
}

/**
* Get the audienceId of the ContactOptions.
*
* @return The audienceId of the ContactOptions.
*/
public String getAudienceId() {
return audienceId;
}

/**
* Get the email of the ContactOptions.
*
* @return The email of the ContactOptions.
*/
public String getEmail() {
return email;
}

/**
* Common builder class for constructing ContactOptions objects.
*/
protected static abstract class Builder<T extends ContactOptions, B extends Builder<T, B>> {
/**
* The id of the contact options builder
*/
protected String id;

/**
* The audienceId of the contact options builder
*/
protected String audienceId;

/**
* The email of the contact options builder
*/
protected String email;

/**
* Set the id of the ContactOptions.
*
* @param id The id of the ContactOptions.
* @return The builder instance.
*/
public B id(String id) {
this.id = id;
return self();
}

/**
* Set the audienceId of the ContactOptions.
*
* @param audienceId The audienceId of the ContactOptions.
* @return The builder instance.
*/
public B audienceId(String audienceId) {
this.audienceId = audienceId;
return self();
}

/**
* Set the email of the ContactOptions.
*
* @param email The email of the ContactOptions.
* @return The builder instance.
*/
public B email(String email) {
this.email = email;
return self();
}

/**
* Abstract method to be implemented by subclasses to create an instance of the corresponding ContactOptions class.
*
* @return A new ContactOptions object.
*/
public abstract T build();

/**
* Abstract method to be implemented by subclasses to return the builder instance (used for self-referencing in methods).
*
* @return The builder instance.
*/
protected abstract B self();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Represents a request to create a contact.
*/
public class CreateContactRequestOptions {
public class CreateContactOptions {

@JsonProperty("audience_id")
private final String audienceId;
Expand All @@ -27,7 +27,7 @@ public class CreateContactRequestOptions {
*
* @param builder The builder to construct the Contact.
*/
public CreateContactRequestOptions(Builder builder) {
public CreateContactOptions(Builder builder) {
this.audienceId = builder.audienceId;
this.email = builder.email;
this.unsubscribed = builder.unsubscribed;
Expand Down Expand Up @@ -159,8 +159,8 @@ public Builder lastName(String lastName) {
*
* @return A new CreateContactRequest object.
*/
public CreateContactRequestOptions build() {
return new CreateContactRequestOptions(this);
public CreateContactOptions build() {
return new CreateContactOptions(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Represents a successful response for creating a contact.
* Extends the Contact class.
*/
public class CreateContactResponseSuccess extends BaseContact{
public class CreateContactResponseSuccess extends BaseContact {

/**
* Default constructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.resend.services.contacts.model;

/**
* Class representing options to get a contact.
*/
public class GetContactOptions extends ContactOptions {

/**
* Constructs a GetContactOptions object using the provided builder.
*
* @param builder The builder to construct the GetContactOptions.
*/
public GetContactOptions(Builder builder) {
super(builder);
}

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

/**
* Builder class for constructing GetContactOptions objects.
*/
public static class Builder extends ContactOptions.Builder<GetContactOptions, Builder> {

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

/**
* Return the builder instance.
*
* @return The builder instance.
*/
@Override
protected Builder self() {
return this;
}
}
}
Loading

0 comments on commit 1f6c130

Please sign in to comment.