Skip to content

Commit ff07817

Browse files
feat: javadoc
1 parent 2c56093 commit ff07817

18 files changed

Lines changed: 799 additions & 26 deletions

src/main/java/dev/resms/ReSMS.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,74 @@
77
import dev.resms.service.SmsService;
88
import lombok.NonNull;
99

10-
/** ReSMS Java SDK - Client principal */
10+
/**
11+
* This class serves as the primary entry point for the ReSMS Java SDK, providing a simple interface
12+
* for sending SMS messages through the ReSMS API.
13+
*
14+
* <p>Example usage:
15+
*
16+
* <pre>{@code
17+
* // Initialize with API key
18+
* ReSMS client = new ReSMS("your-api-key");
19+
*
20+
* // Send a simple SMS
21+
* try {
22+
* SendSmsResponse response = client.sendSms("+1234567890", "Hello from ReSMS!");
23+
* System.out.println("Message sent with ID: " + response.getData().getMessageId());
24+
* } catch (ReSMSException e) {
25+
* System.err.println("Failed to send SMS: " + e.getMessage());
26+
* }
27+
* }</pre>
28+
*
29+
* @since 1.0.0
30+
* @see dev.resms.service.SmsService
31+
* @see dev.resms.config.ReSMSConfig
32+
*/
1133
public class ReSMS {
34+
/** The SMS service used to send messages */
1235
private final SmsService smsService;
1336

1437
/**
15-
* Creates a new ReSMS client with a default timeout of 10 seconds
38+
* Creates a new ReSMS client with the specified API key.
1639
*
17-
* @param apiKey API key for authentication
40+
* <p>This constructor uses default configuration settings with the provided API key.
41+
*
42+
* @param apiKey The API key for authentication with the ReSMS service. Must not be {@code null}
43+
* or blank.
44+
* @throws IllegalArgumentException if the API key is blank
45+
* @see ReSMSConfig
1846
*/
1947
public ReSMS(@NonNull String apiKey) {
2048
this(new ReSMSConfig(apiKey));
2149
}
2250

51+
/**
52+
* Creates a new ReSMS client with the specified configuration.
53+
*
54+
* <p>This constructor allows for more customized configuration options.
55+
*
56+
* @param config The configuration object containing API key and other settings. Must not be
57+
* {@code null}.
58+
* @see ReSMSConfig
59+
*/
2360
public ReSMS(@NonNull ReSMSConfig config) {
2461
this.smsService = new SmsService(config);
2562
}
2663

2764
/**
28-
* Sends an SMS message - Main method
65+
* Sends an SMS message to the specified phone number.
66+
*
67+
* <p>This is a convenience method that creates a {@link SendSmsRequest} object and delegates to
68+
* {@link #sendSms(SendSmsRequest)}.
2969
*
30-
* @param to Phone number to send the message to
31-
* @param message Message content
32-
* @return SendSmsResponse containing the message ID and status
33-
* @throws ReSMSException if fails
70+
* @param to The phone number to send the message to, in E.164 format (e.g., "+1234567890"). Must
71+
* not be {@code null}.
72+
* @param message The content of the SMS message. Must not be {@code null}.
73+
* @return A {@link SendSmsResponse} containing the message ID and status information.
74+
* @throws ReSMSException if the message cannot be sent due to API errors, network issues,
75+
* validation failures, or insufficient quota.
76+
* @see SendSmsRequest
77+
* @see SendSmsResponse
3478
*/
3579
public SendSmsResponse sendSms(@NonNull String to, @NonNull String message)
3680
throws ReSMSException {
@@ -39,11 +83,18 @@ public SendSmsResponse sendSms(@NonNull String to, @NonNull String message)
3983
}
4084

4185
/**
42-
* Sends an SMS message using request object
86+
* Sends an SMS message using a request object.
87+
*
88+
* <p>This method provides more control over the SMS sending process by accepting a pre-configured
89+
* request object.
4390
*
44-
* @param request SendSmsRequest object
45-
* @return SendSmsResponse containing the message ID and status
46-
* @throws ReSMSException if fails
91+
* @param request The {@link SendSmsRequest} object containing the recipient phone number, message
92+
* content, and optional parameters. Must not be {@code null}.
93+
* @return A {@link SendSmsResponse} containing the message ID and status information.
94+
* @throws ReSMSException if the message cannot be sent due to API errors, network issues,
95+
* validation failures, or insufficient quota.
96+
* @see SendSmsRequest
97+
* @see SendSmsResponse
4798
*/
4899
public SendSmsResponse sendSms(@NonNull SendSmsRequest request) throws ReSMSException {
49100
return smsService.sendSms(request);

src/main/java/dev/resms/api/ReSMSApiClient.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,50 @@
1919
import java.net.http.HttpResponse;
2020
import org.apache.hc.core5.http.HttpStatus;
2121

22+
/**
23+
* This class handles the low-level HTTP communication with the ReSMS API service. It is responsible
24+
* for serializing requests, making HTTP calls, deserializing responses, and translating API errors
25+
* into appropriate exception types.
26+
*
27+
* <p>This class is typically not used directly by end users, but rather through the {@link
28+
* dev.resms.service.SmsService} and {@link dev.resms.ReSMS} classes.
29+
*
30+
* <p>The client uses Java's {@link HttpClient} for HTTP communication and <a
31+
* href="https://github.com/square/moshi">Moshi</a> for JSON serialization/deserialization.
32+
*
33+
* @since 1.0.0
34+
* @see dev.resms.service.SmsService
35+
* @see dev.resms.ReSMS
36+
* @see dev.resms.config.ReSMSConfig
37+
*/
2238
public class ReSMSApiClient {
39+
/** The API endpoint path for sending SMS messages. */
2340
private static final String SEND_SMS_PATH = "sms/send";
2441

42+
/** The configuration object containing API key and other settings. */
2543
private final ReSMSConfig config;
44+
45+
/** The HTTP client used for making API requests. */
2646
private final HttpClient httpClient;
47+
48+
/** JSON adapter for serializing {@link SendSmsRequest} objects. */
2749
private final JsonAdapter<SendSmsRequest> requestAdapter;
50+
51+
/** JSON adapter for deserializing {@link SendSmsResponse} objects. */
2852
private final JsonAdapter<SendSmsResponse> responseAdapter;
53+
54+
/** JSON adapter for deserializing {@link ErrorResponse} objects. */
2955
private final JsonAdapter<ErrorResponse> errorResponseAdapter;
3056

57+
/**
58+
* Creates a new API client with the specified configuration.
59+
*
60+
* <p>Initializes the HTTP client and JSON adapters for serialization/deserialization.
61+
*
62+
* @param config The configuration object containing API key and other settings. Must not be
63+
* {@code null}.
64+
* @see ReSMSConfig
65+
*/
3166
public ReSMSApiClient(ReSMSConfig config) {
3267
this.config = config;
3368
this.httpClient = HttpClient.newBuilder().build();
@@ -39,6 +74,30 @@ public ReSMSApiClient(ReSMSConfig config) {
3974
this.errorResponseAdapter = moshi.adapter(ErrorResponse.class);
4075
}
4176

77+
/**
78+
* Sends an SMS message by making an HTTP request to the ReSMS API.
79+
*
80+
* <p>This method performs the following steps:
81+
*
82+
* <ol>
83+
* <li>Serializes the request object to JSON
84+
* <li>Creates an HTTP request with appropriate headers
85+
* <li>Sends the HTTP request to the API
86+
* <li>Processes the HTTP response
87+
* <li>Deserializes the response JSON
88+
* <li>Returns the response object or throws an appropriate exception
89+
* </ol>
90+
*
91+
* @param requestBody The {@link SendSmsRequest} object containing the recipient phone number,
92+
* message content, and optional parameters. Must not be {@code null}.
93+
* @return A {@link SendSmsResponse} containing the message ID and status information.
94+
* @throws ReSMSException if the message cannot be sent due to API errors, network issues, or
95+
* other failures. The specific subclass of {@link ReSMSException} depends on the error type
96+
* returned by the API.
97+
* @see SendSmsRequest
98+
* @see SendSmsResponse
99+
* @see ReSMSException
100+
*/
42101
public SendSmsResponse sendSms(SendSmsRequest requestBody) throws ReSMSException {
43102
String jsonBody = requestAdapter.toJson(requestBody);
44103

src/main/java/dev/resms/config/ReSMSConfig.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,51 @@
33
import lombok.Getter;
44
import lombok.NonNull;
55

6-
/** Configuration class for ReSMS SDK */
6+
/**
7+
* This class holds configuration settings for the ReSMS SDK, including the API key required for
8+
* authentication with the ReSMS API service.
9+
*
10+
* <p>Example usage:
11+
*
12+
* <pre>{@code
13+
* // Create a basic configuration with just the API key
14+
* ReSMSConfig config = new ReSMSConfig("your-api-key");
15+
*
16+
* // Use the configuration to initialize the ReSMS client
17+
* ReSMS client = new ReSMS(config);
18+
* }</pre>
19+
*
20+
* <p>The configuration object is immutable once created.
21+
*
22+
* @since 1.0.0
23+
* @see dev.resms.ReSMS
24+
*/
725
@Getter
826
public class ReSMSConfig {
27+
/**
28+
* The base URL for the ReSMS API.
29+
*
30+
* <p>This constant defines the endpoint for all API requests made by the SDK.
31+
*/
932
public static final String BASE_URL = "https://api.resms.dev/";
1033

34+
/**
35+
* The API key used for authentication with the ReSMS service.
36+
*
37+
* <p>This field is final and can only be set during object construction.
38+
*/
1139
private final String apiKey;
1240

41+
/**
42+
* Creates a new configuration object with the specified API key.
43+
*
44+
* <p>The API key is validated to ensure it is not blank. Leading and trailing whitespace is
45+
* automatically trimmed.
46+
*
47+
* @param apiKey The API key for authentication with the ReSMS service. Must not be {@code null}
48+
* or blank.
49+
* @throws IllegalArgumentException if the API key is blank or {@code null}
50+
*/
1351
public ReSMSConfig(@NonNull String apiKey) {
1452
if (apiKey.isBlank()) {
1553
throw new IllegalArgumentException("API key cannot be blank");

src/main/java/dev/resms/exception/ReSMSException.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
package dev.resms.exception;
22

3+
/**
4+
* This is the base exception class for all exceptions thrown by the ReSMS SDK. It extends {@link
5+
* RuntimeException}, making all ReSMS exceptions unchecked.
6+
*
7+
* <p>This class serves as a parent for more specific exception types that provide detailed
8+
* information about specific error conditions. When catching exceptions from the ReSMS SDK, you can
9+
* either catch this base exception type or specific subclasses for more granular error handling.
10+
*
11+
* <p>Example usage:
12+
*
13+
* <pre>{@code
14+
* try {
15+
* ReSMS client = new ReSMS("your-api-key");
16+
* client.sendSms("+1234567890", "Hello from ReSMS!");
17+
* } catch (InvalidApiKeyException e) {
18+
* // Handle invalid API key specifically
19+
* System.err.println("Your API key is invalid: " + e.getMessage());
20+
* } catch (ReSMSException e) {
21+
* // Handle any other ReSMS exception
22+
* System.err.println("Failed to send SMS: " + e.getMessage());
23+
* }
24+
* }</pre>
25+
*
26+
* @since 1.0.0
27+
* @see java.lang.RuntimeException
28+
* @see dev.resms.exception.user.InvalidApiKeyException
29+
* @see dev.resms.exception.sms.InsufficientSmsQuotaException
30+
*/
331
public class ReSMSException extends RuntimeException {
32+
/**
33+
* Constructs a new ReSMS exception with the specified detail message.
34+
*
35+
* <p>The cause is not initialized, and may subsequently be initialized by a call to {@link
36+
* #initCause}.
37+
*
38+
* @param message The detail message. The detail message is saved for later retrieval by the
39+
* {@link #getMessage()} method.
40+
*/
441
public ReSMSException(String message) {
542
super(message);
643
}
744

45+
/**
46+
* Constructs a new ReSMS exception with the specified detail message and cause.
47+
*
48+
* <p>Note that the detail message associated with {@code cause} is <i>not</i> automatically
49+
* incorporated in this exception's detail message.
50+
*
51+
* @param message The detail message (which is saved for later retrieval by the {@link
52+
* #getMessage()} method).
53+
* @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method).
54+
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or
55+
* unknown.)
56+
*/
857
public ReSMSException(String message, Throwable cause) {
958
super(message, cause);
1059
}

src/main/java/dev/resms/exception/senderid/NoDefaultSenderIdException.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,44 @@
22

33
import dev.resms.exception.ReSMSException;
44

5+
/**
6+
* This exception is thrown when an SMS message is being sent without specifying a sender ID, and
7+
* there is no default sender ID configured for the account. A sender ID is required for sending SMS
8+
* messages as it identifies the sender to the recipient.
9+
*
10+
* <p>This exception typically occurs when:
11+
*
12+
* <ul>
13+
* <li>The account has not configured any sender IDs
14+
* <li>The account has multiple sender IDs but none is set as the default
15+
* <li>The default sender ID has been removed or deactivated
16+
* </ul>
17+
*
18+
* <p>Example handling:
19+
*
20+
* <pre>{@code
21+
* try {
22+
* // Attempting to send SMS without specifying a sender ID
23+
* ReSMS client = new ReSMS("your-api-key");
24+
* client.sendSms("+1234567890", "Hello from ReSMS!");
25+
* } catch (NoDefaultSenderIdException e) {
26+
* System.err.println("No default sender ID: " + e.getMessage());
27+
* System.err.println("Please configure a default sender ID in your ReSMS dashboard or specify a sender ID in your request.");
28+
* }
29+
* }</pre>
30+
*
31+
* @since 1.0.0
32+
* @see dev.resms.exception.ReSMSException
33+
* @see dev.resms.exception.senderid.SenderIdNotFoundException
34+
*/
535
public class NoDefaultSenderIdException extends ReSMSException {
36+
/**
37+
* Constructs a new NoDefaultSenderIdException with the specified detail message.
38+
*
39+
* @param message The detail message explaining the issue with the default sender ID. This message
40+
* typically includes guidance on how to configure a default sender ID or how to specify a
41+
* sender ID in the request.
42+
*/
643
public NoDefaultSenderIdException(String message) {
744
super(message);
845
}

src/main/java/dev/resms/exception/senderid/SenderIdNotFoundException.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,48 @@
22

33
import dev.resms.exception.ReSMSException;
44

5+
/**
6+
* This exception is thrown when an SMS message is being sent with a specified sender ID that does
7+
* not exist or is not associated with the account. A valid sender ID is required for sending SMS
8+
* messages as it identifies the sender to the recipient.
9+
*
10+
* <p>This exception typically occurs when:
11+
*
12+
* <ul>
13+
* <li>The specified sender ID has never been registered with the account
14+
* <li>The sender ID has been deleted or deactivated
15+
* <li>There is a typo or case mismatch in the sender ID
16+
* <li>The sender ID belongs to a different account
17+
* </ul>
18+
*
19+
* <p>Example handling:
20+
*
21+
* <pre>{@code
22+
* try {
23+
* // Attempting to send SMS with a non-existent sender ID
24+
* SendSmsRequest request = new SendSmsRequest("+1234567890", "Hello from ReSMS!");
25+
* request.setSenderId("NonExistentID");
26+
*
27+
* ReSMS client = new ReSMS("your-api-key");
28+
* client.sendSms(request);
29+
* } catch (SenderIdNotFoundException e) {
30+
* System.err.println("Sender ID not found: " + e.getMessage());
31+
* System.err.println("Please verify the sender ID or use a different one.");
32+
* }
33+
* }</pre>
34+
*
35+
* @since 1.0.0
36+
* @see dev.resms.exception.ReSMSException
37+
* @see dev.resms.exception.senderid.NoDefaultSenderIdException
38+
*/
539
public class SenderIdNotFoundException extends ReSMSException {
40+
/**
41+
* Constructs a new SenderIdNotFoundException with the specified detail message.
42+
*
43+
* @param message The detail message explaining the issue with the sender ID. This message
44+
* typically includes information about which sender ID was not found and may suggest
45+
* available sender IDs for the account.
46+
*/
647
public SenderIdNotFoundException(String message) {
748
super(message);
849
}

0 commit comments

Comments
 (0)