Skip to content

Commit 7570c02

Browse files
Merge pull request #3 from reSMS-dev/feat/SMS-102_add_otp
feat(SMS-102): add otp service and models
2 parents 62f2440 + cabb3ad commit 7570c02

File tree

9 files changed

+178
-0
lines changed

9 files changed

+178
-0
lines changed

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

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

3+
import dev.resms.services.otp.Otp;
34
import dev.resms.services.sms.Sms;
45
import lombok.RequiredArgsConstructor;
56

@@ -17,4 +18,13 @@ public class ReSMS {
1718
public Sms sms() {
1819
return new Sms(apiKey);
1920
}
21+
22+
/**
23+
* Returns an Otp object that can be used to interact with the Otp service.
24+
*
25+
* @return An Otp object.
26+
*/
27+
public Otp otp() {
28+
return new Otp(apiKey);
29+
}
2030
}

src/main/java/dev/resms/core/net/HttpMethod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
public enum HttpMethod {
55
GET,
66
POST,
7+
DELETE,
78
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package dev.resms.services.otp;
2+
3+
import dev.resms.core.exception.ReSMSException;
4+
import dev.resms.core.net.AbstractHttpResponse;
5+
import dev.resms.core.net.HttpMethod;
6+
import dev.resms.core.service.BaseService;
7+
import dev.resms.services.otp.model.CreateOtpOptions;
8+
import dev.resms.services.otp.model.CreateOtpResponse;
9+
import dev.resms.services.otp.model.DeleteOtpResponse;
10+
import dev.resms.services.otp.model.VerifyOtpOptions;
11+
import dev.resms.services.otp.model.VerifyOtpResponse;
12+
13+
public class Otp extends BaseService {
14+
private static final String CREATE_OTP_PATH = "/otp";
15+
private static final String VERIFY_OTP_PATH = "/otp/verify";
16+
private static final String DELETE_OTP_PATH = "/otp";
17+
18+
/**
19+
* Constructs an instance of the {@code Otp} class.
20+
*
21+
* @param apiKey The apiKey used for authentication.
22+
*/
23+
public Otp(final String apiKey) {
24+
super(apiKey);
25+
}
26+
27+
/**
28+
* Create an OTP based on the provided OTP request
29+
*
30+
* @param createOtpOptions The request containing OTP details.
31+
* @return The response indicating the status of the OTP creation.
32+
* @throws ReSMSException If an error occurs while creating the OTP.
33+
*/
34+
public CreateOtpResponse create(CreateOtpOptions createOtpOptions) throws ReSMSException {
35+
String payload = super.reSMSMapper.toJson(createOtpOptions);
36+
37+
AbstractHttpResponse<String> response =
38+
super.httpClient.perform(CREATE_OTP_PATH, apiKey, HttpMethod.POST, payload);
39+
40+
if (!response.isSuccessful()) {
41+
throw new ReSMSException(
42+
"Failed to create otp: " + response.getCode() + " " + response.getBody());
43+
}
44+
45+
return reSMSMapper.fromJson(response.getBody(), CreateOtpResponse.class);
46+
}
47+
48+
/**
49+
* Verify an OTP based on the provided OTP verify request
50+
*
51+
* @param verifyOtpOptions The request containing OTP verify details.
52+
* @return The response indicating the status of the OTP verification.
53+
* @throws ReSMSException If an error occurs while verifying the OTP.
54+
*/
55+
public VerifyOtpResponse verify(VerifyOtpOptions verifyOtpOptions) throws ReSMSException {
56+
String payload = super.reSMSMapper.toJson(verifyOtpOptions);
57+
58+
AbstractHttpResponse<String> response =
59+
super.httpClient.perform(VERIFY_OTP_PATH, apiKey, HttpMethod.POST, payload);
60+
61+
if (!response.isSuccessful()) {
62+
throw new ReSMSException(
63+
"Failed to create otp: " + response.getCode() + " " + response.getBody());
64+
}
65+
66+
return reSMSMapper.fromJson(response.getBody(), VerifyOtpResponse.class);
67+
}
68+
69+
/**
70+
* Delete an OTP based on its id
71+
*
72+
* @param otpId The id of the OTP to delete.
73+
* @return The response indicating the status of the OTP deletion.
74+
* @throws ReSMSException If an error occurs while deleting the OTP.
75+
*/
76+
public DeleteOtpResponse delete(String otpId) throws ReSMSException {
77+
String payload = "{\"otpId\": \"" + otpId + "\"}";
78+
79+
AbstractHttpResponse<String> response =
80+
super.httpClient.perform(DELETE_OTP_PATH, apiKey, HttpMethod.DELETE, payload);
81+
82+
if (!response.isSuccessful()) {
83+
throw new ReSMSException(
84+
"Failed to create otp: " + response.getCode() + " " + response.getBody());
85+
}
86+
87+
return reSMSMapper.fromJson(response.getBody(), DeleteOtpResponse.class);
88+
}
89+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dev.resms.services.otp.model;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
@Builder
10+
public class CreateOtpOptions {
11+
private String to;
12+
private String message;
13+
private String senderId;
14+
private OtpCode codeType;
15+
private int codeLength;
16+
private int validityMinutes;
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dev.resms.services.otp.model;
2+
3+
import dev.resms.core.model.Response;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class CreateOtpResponse extends Response {
8+
private CreateOtpResponseData data;
9+
10+
@Getter
11+
public static class CreateOtpResponseData {
12+
private String phoneNumber;
13+
private String expiresAt;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dev.resms.services.otp.model;
2+
3+
import dev.resms.core.model.Response;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class DeleteOtpResponse extends Response {
8+
private String otpId;
9+
private String phoneNumber;
10+
private String revokedAt;
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dev.resms.services.otp.model;
2+
3+
public enum OtpCode {
4+
NUMERIC,
5+
ALPHA,
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.resms.services.otp.model;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
@Builder
10+
public class VerifyOtpOptions {
11+
private String to;
12+
private String code;
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dev.resms.services.otp.model;
2+
3+
import dev.resms.core.model.Response;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class VerifyOtpResponse extends Response {
8+
private VerifyOtpResponseData data;
9+
10+
@Getter
11+
public static class VerifyOtpResponseData {
12+
private String otpId;
13+
private String phoneNumber;
14+
private String verifiedAt;
15+
}
16+
}

0 commit comments

Comments
 (0)