Skip to content

Commit fb1b83d

Browse files
feat(SMS-102): add otp service and models
1 parent 62f2440 commit fb1b83d

File tree

8 files changed

+170
-0
lines changed

8 files changed

+170
-0
lines changed

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

0 commit comments

Comments
 (0)