|
| 1 | +package com.resend.services.contactproperties; |
| 2 | + |
| 3 | +import com.resend.core.exception.ResendException; |
| 4 | +import com.resend.core.net.AbstractHttpResponse; |
| 5 | +import com.resend.core.net.HttpMethod; |
| 6 | +import com.resend.core.service.BaseService; |
| 7 | +import com.resend.services.contactproperties.model.*; |
| 8 | +import okhttp3.MediaType; |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents the Resend ContactProperties module. |
| 12 | + */ |
| 13 | +public class ContactProperties extends BaseService { |
| 14 | + |
| 15 | + /** |
| 16 | + * Constructs an instance of the {@code ContactProperties} class. |
| 17 | + * |
| 18 | + * @param apiKey The apiKey used for authentication. |
| 19 | + */ |
| 20 | + public ContactProperties(final String apiKey) { |
| 21 | + super(apiKey); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Creates a ContactProperty. |
| 26 | + * |
| 27 | + * @param createContactPropertyOptions The ContactProperty details. |
| 28 | + * @return The details of the created contact property. |
| 29 | + * @throws ResendException If an error occurs during the ContactProperty creation process. |
| 30 | + */ |
| 31 | + public CreateContactPropertyResponseSuccess create(CreateContactPropertyOptions createContactPropertyOptions) throws ResendException { |
| 32 | + String payload = super.resendMapper.writeValue(createContactPropertyOptions); |
| 33 | + AbstractHttpResponse<String> response = httpClient.perform("/contact-properties", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json")); |
| 34 | + |
| 35 | + if (!response.isSuccessful()) { |
| 36 | + throw new ResendException(response.getCode(), response.getBody()); |
| 37 | + } |
| 38 | + |
| 39 | + String responseBody = response.getBody(); |
| 40 | + return resendMapper.readValue(responseBody, CreateContactPropertyResponseSuccess.class); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Retrieves a list of contact properties and returns a ListContactPropertiesResponseSuccess. |
| 45 | + * |
| 46 | + * @return A ListContactPropertiesResponseSuccess containing the list of contact properties. |
| 47 | + * @throws ResendException If an error occurs during the contact properties list retrieval process. |
| 48 | + */ |
| 49 | + public ListContactPropertiesResponseSuccess list() throws ResendException { |
| 50 | + AbstractHttpResponse<String> response = this.httpClient.perform("/contact-properties", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json")); |
| 51 | + |
| 52 | + if (!response.isSuccessful()) { |
| 53 | + throw new ResendException(response.getCode(), response.getBody()); |
| 54 | + } |
| 55 | + |
| 56 | + String responseBody = response.getBody(); |
| 57 | + |
| 58 | + return resendMapper.readValue(responseBody, ListContactPropertiesResponseSuccess.class); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Retrieves a contact property by its unique identifier. |
| 63 | + * |
| 64 | + * @param id The contact property's id. |
| 65 | + * @return The retrieved contact property details. |
| 66 | + * @throws ResendException If an error occurs while retrieving the contact property. |
| 67 | + */ |
| 68 | + public ContactProperty get(String id) throws ResendException { |
| 69 | + if (id == null || id.isEmpty()) { |
| 70 | + throw new IllegalArgumentException("Contact property id must be provided"); |
| 71 | + } |
| 72 | + |
| 73 | + AbstractHttpResponse<String> response = this.httpClient.perform("/contact-properties/" + id, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json")); |
| 74 | + |
| 75 | + if (!response.isSuccessful()) { |
| 76 | + throw new ResendException(response.getCode(), response.getBody()); |
| 77 | + } |
| 78 | + |
| 79 | + String responseBody = response.getBody(); |
| 80 | + |
| 81 | + return resendMapper.readValue(responseBody, ContactProperty.class); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Updates a contact property based on the provided contact property ID. |
| 86 | + * |
| 87 | + * @param updateContactPropertyOptions The object with the contact property id and fallback value to update. |
| 88 | + * @return The UpdateContactPropertyResponseSuccess with the details of the updated contact property. |
| 89 | + * @throws ResendException If an error occurs during the contact property update process. |
| 90 | + */ |
| 91 | + public UpdateContactPropertyResponseSuccess update(UpdateContactPropertyOptions updateContactPropertyOptions) throws ResendException { |
| 92 | + if (updateContactPropertyOptions.getId() == null || updateContactPropertyOptions.getId().isEmpty()) { |
| 93 | + throw new IllegalArgumentException("Contact property id must be provided"); |
| 94 | + } |
| 95 | + |
| 96 | + String payload = super.resendMapper.writeValue(updateContactPropertyOptions); |
| 97 | + AbstractHttpResponse<String> response = httpClient.perform("/contact-properties/" + updateContactPropertyOptions.getId(), super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json")); |
| 98 | + |
| 99 | + if (!response.isSuccessful()) { |
| 100 | + throw new ResendException(response.getCode(), response.getBody()); |
| 101 | + } |
| 102 | + |
| 103 | + String responseBody = response.getBody(); |
| 104 | + |
| 105 | + return resendMapper.readValue(responseBody, UpdateContactPropertyResponseSuccess.class); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Deletes a contact property based on the provided contact property ID. |
| 110 | + * |
| 111 | + * @param id The identifier of the contact property to delete. |
| 112 | + * @return The RemoveContactPropertyResponseSuccess with the details of the removed contact property. |
| 113 | + * @throws ResendException If an error occurs during the contact property deletion process. |
| 114 | + */ |
| 115 | + public RemoveContactPropertyResponseSuccess remove(String id) throws ResendException { |
| 116 | + if (id == null || id.isEmpty()) { |
| 117 | + throw new IllegalArgumentException("Contact property id must be provided"); |
| 118 | + } |
| 119 | + |
| 120 | + AbstractHttpResponse<String> response = httpClient.perform("/contact-properties/" + id, super.apiKey, HttpMethod.DELETE, "", null); |
| 121 | + |
| 122 | + if (!response.isSuccessful()) { |
| 123 | + throw new ResendException(response.getCode(), response.getBody()); |
| 124 | + } |
| 125 | + |
| 126 | + String responseBody = response.getBody(); |
| 127 | + |
| 128 | + return resendMapper.readValue(responseBody, RemoveContactPropertyResponseSuccess.class); |
| 129 | + } |
| 130 | +} |
0 commit comments