Skip to content

Commit 62f2440

Browse files
Merge pull request #2 from reSMS-dev/feat/SMS-101_Refactor_code_to_use_new_httpclient
feat: update project to use new httpClient
2 parents 065f863 + 8108324 commit 62f2440

25 files changed

+180
-511
lines changed

README.md

Lines changed: 21 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ReSMS SDK for Java
22

3+
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
34
[![Maven Central](https://img.shields.io/maven-central/v/dev.resms/resms-java-sdk.svg)](https://search.maven.org/artifact/dev.resms/resms-java-sdk)
4-
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
55
[![Java Version](https://img.shields.io/badge/Java-11%2B-blue)](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html)
66

77
A lightweight, easy-to-use Java SDK for [ReSMS](https://resms.dev) - the simple and powerful SMS API for developers.
@@ -11,9 +11,6 @@ A lightweight, easy-to-use Java SDK for [ReSMS](https://resms.dev) - the simple
1111
- [Requirements](#-requirements)
1212
- [Installation](#-installation)
1313
- [Quick Start](#-quick-start)
14-
- [Usage Examples](#-usage-examples)
15-
- [Error Handling](#-error-handling)
16-
- [Advanced Configuration](#-advanced-configuration)
1714
- [Documentation](#-documentation)
1815
- [License](#-license)
1916

@@ -50,145 +47,31 @@ implementation("dev.resms:resms-java-sdk:1.0.1")
5047

5148
1. **Get your API key** from the [ReSMS Dashboard](https://resms.dev/dashboard)
5249

53-
2. **Initialize the client**
50+
2. **Send your first SMS**
5451

5552
```java
56-
import dev.resms.ReSMS;
57-
58-
ReSMS client = new ReSMS("your-api-key");
59-
```
60-
61-
3. **Send your first SMS**
62-
63-
```java
64-
import dev.resms.ReSMS;
65-
import dev.resms.exception.ReSMSException;
66-
import dev.resms.model.response.SendSmsResponse;
67-
68-
public class QuickStartExample {
69-
public static void main(String[] args) {
70-
ReSMS client = new ReSMS("your-api-key");
71-
72-
try {
73-
SendSmsResponse response = client.sendSms("+33123456789", "Hello from ReSMS!");
74-
System.out.println("Message sent! ID: " + response.getData().getMessageId());
75-
} catch (ReSMSException e) {
76-
System.err.println("Error sending SMS: " + e.getMessage());
77-
}
78-
}
79-
}
80-
```
81-
82-
## 📱 Usage Examples
83-
84-
### Basic SMS Sending
85-
86-
```java
87-
import dev.resms.ReSMS;
88-
import dev.resms.model.response.SendSmsResponse;
89-
90-
public class BasicExample {
91-
public static void main(String[] args) {
92-
// Initialize the client
93-
ReSMS client = new ReSMS("your-api-key");
94-
95-
try {
96-
// Send an SMS
97-
SendSmsResponse response = client.sendSms("+33123456789", "Welcome to ReSMS!");
98-
99-
// Get the message ID and status
100-
String messageId = response.getData().getMessageId();
101-
String status = response.getStatus();
102-
103-
System.out.println("Message sent successfully!");
104-
System.out.println("Message ID: " + messageId);
105-
System.out.println("Status: " + status);
106-
} catch (ReSMSException e) {
107-
System.err.println("Failed to send SMS: " + e.getMessage());
108-
}
53+
import dev.resms.core.exception.ReSMSException;
54+
import dev.resms.services.sms.model.SendSmsOptions;
55+
56+
public class Main {
57+
public static void main(String[] args) {
58+
ReSMS reSms = new ReSMS("re_123");
59+
60+
SendSmsOptions smsOptions =
61+
SendSmsOptions.builder()
62+
.to("+33123456789")
63+
.message("Welcome to ReSMS!")
64+
.senderId("ReSMS")
65+
.build();
66+
67+
try {
68+
reSms.sms().send(smsOptions);
69+
} catch (ReSMSException e) {
70+
e.printStackTrace();
10971
}
72+
}
11073
}
111-
```
112-
113-
### Using Custom Configuration
114-
115-
```java
116-
import dev.resms.ReSMS;
117-
import dev.resms.config.ReSMSConfig;
118-
119-
public class ConfigExample {
120-
public static void main(String[] args) {
121-
// Create a custom configuration
122-
ReSMSConfig config = new ReSMSConfig("your-api-key");
123-
124-
// Initialize the client with the custom configuration
125-
ReSMS client = new ReSMS(config);
126-
127-
// Now you can use the client as usual
128-
// ...
129-
}
130-
}
131-
```
132-
133-
## ⚠️ Error Handling
134-
135-
The SDK uses a comprehensive exception hierarchy to help you handle errors effectively:
136-
137-
- `ReSMSException`: Base exception class for all ReSMS-related errors
138-
139-
### Specific Exceptions
140-
141-
All these exceptions extend `ReSMSException`:
142-
143-
- `InvalidApiKeyException`: Thrown when the API key is invalid
144-
- `CountryDetectionFailedException`: Thrown when the country of the phone number cannot be detected
145-
- `PhoneNumberParsingFailedException`: Thrown when the phone number cannot be parsed
146-
- `InsufficientSmsQuotaException`: Thrown when you don't have enough SMS quota
147-
- `MessageStatusUpdateFailedException`: Thrown when the message status cannot be updated
148-
149-
### Example: Handling Specific Exceptions
150-
151-
```java
152-
import dev.resms.ReSMS;
153-
import dev.resms.exception.ReSMSException;
154-
import dev.resms.exception.sms.InsufficientSmsQuotaException;
155-
import dev.resms.exception.user.InvalidApiKeyException;
156-
157-
public class ErrorHandlingExample {
158-
public static void main(String[] args) {
159-
ReSMS client = new ReSMS("your-api-key");
160-
161-
try {
162-
client.sendSms("+33123456789", "Test message");
163-
System.out.println("Message sent successfully!");
164-
} catch (InvalidApiKeyException e) {
165-
System.err.println("Authentication error: " + e.getMessage());
166-
System.err.println("Please check your API key");
167-
} catch (InsufficientSmsQuotaException e) {
168-
System.err.println("Quota exceeded: " + e.getMessage());
169-
System.err.println("Please upgrade your plan");
170-
} catch (ReSMSException e) {
171-
System.err.println("ReSMS error: " + e.getMessage());
172-
} catch (Exception e) {
173-
System.err.println("Unexpected error: " + e.getMessage());
174-
}
175-
}
176-
}
177-
```
178-
179-
## ⚙️ Advanced Configuration
180-
181-
The `ReSMSConfig` class allows you to customize the SDK's behavior:
182-
183-
```java
184-
import dev.resms.ReSMS;
185-
import dev.resms.config.ReSMSConfig;
186-
187-
// Create a configuration with your API key
188-
ReSMSConfig config = new ReSMSConfig("your-api-key");
18974

190-
// Initialize the client with the configuration
191-
ReSMS client = new ReSMS(config);
19275
```
19376

19477
## 📚 Documentation

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

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,20 @@
11
package dev.resms;
22

3-
import dev.resms.config.ReSMSConfig;
4-
import dev.resms.exception.ReSMSException;
5-
import dev.resms.model.request.SendSmsRequest;
6-
import dev.resms.model.response.SendSmsResponse;
7-
import dev.resms.service.SmsService;
8-
import lombok.NonNull;
3+
import dev.resms.services.sms.Sms;
4+
import lombok.RequiredArgsConstructor;
95

106
/** ReSMS Java SDK - Client principal */
7+
@RequiredArgsConstructor
118
public class ReSMS {
12-
private final SmsService smsService;
9+
/** The API key for the ReSMS service. */
10+
private final String apiKey;
1311

1412
/**
15-
* Creates a new ReSMS client with a default timeout of 10 seconds
13+
* Returns an Sms object that can be used to interact with the SMS service.
1614
*
17-
* @param apiKey API key for authentication
15+
* @return An Sms object.
1816
*/
19-
public ReSMS(@NonNull String apiKey) {
20-
this(new ReSMSConfig(apiKey));
21-
}
22-
23-
public ReSMS(@NonNull ReSMSConfig config) {
24-
this.smsService = new SmsService(config);
25-
}
26-
27-
/**
28-
* Sends an SMS message - Main method
29-
*
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
34-
*/
35-
public SendSmsResponse sendSms(@NonNull String to, @NonNull String message)
36-
throws ReSMSException {
37-
SendSmsRequest request = new SendSmsRequest(to, message);
38-
return this.sendSms(request);
39-
}
40-
41-
/**
42-
* Sends an SMS message using request object
43-
*
44-
* @param request SendSmsRequest object
45-
* @return SendSmsResponse containing the message ID and status
46-
* @throws ReSMSException if fails
47-
*/
48-
public SendSmsResponse sendSms(@NonNull SendSmsRequest request) throws ReSMSException {
49-
return smsService.sendSms(request);
17+
public Sms sms() {
18+
return new Sms(apiKey);
5019
}
5120
}

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

Lines changed: 0 additions & 92 deletions
This file was deleted.

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/dev/resms/exception/ReSMSException.java renamed to src/main/java/dev/resms/core/exception/ReSMSException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.resms.exception;
1+
package dev.resms.core.exception;
22

33
public class ReSMSException extends RuntimeException {
44
public ReSMSException(String message) {

src/main/java/dev/resms/model/Response.java renamed to src/main/java/dev/resms/core/model/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.resms.model;
1+
package dev.resms.core.model;
22

33
import lombok.Getter;
44

src/main/java/dev/resms/core/net/impl/HttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class HttpClient implements IHttpClient<Response> {
1818

1919
/** The base URL for the API. */
20-
public static final String BASE_API = "https://api.resms.dev/";
20+
public static final String BASE_API = "https://api.resms.dev";
2121

2222
/** The OkHttpClient instance for handling HTTP requests. */
2323
private final OkHttpClient httpClient;
@@ -37,7 +37,7 @@ public HttpClient() {
3737
* @return An {@link AbstractHttpResponse} representing the response from the server.
3838
*/
3939
@Override
40-
public AbstractHttpResponse<Response> perform(
40+
public AbstractHttpResponse perform(
4141
final String path, final String apiKey, final HttpMethod method, final String payload) {
4242

4343
RequestBody requestBody = null;

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)