|
| 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 | +} |
0 commit comments