-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented OTP endpoint and improvded error code handler for potenti…
…al edge cases
- Loading branch information
SP Singh
committed
Oct 15, 2021
1 parent
98e0319
commit 018f30c
Showing
10 changed files
with
499 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package otp | ||
|
||
import ( | ||
"fmt" | ||
"github.com/smsglobal/smsglobal-go/internal/pkg/client" | ||
"github.com/smsglobal/smsglobal-go/internal/types/api" | ||
"github.com/smsglobal/smsglobal-go/pkg/logger" | ||
"net/http" | ||
) | ||
|
||
type ( | ||
Client struct { | ||
Handler *client.Client | ||
Logger *logger.Logger | ||
} | ||
verifyOtp struct { | ||
Code string `json:"code"` | ||
} | ||
) | ||
|
||
var path = "/otp" | ||
|
||
// generateMsisdnPath for given destination number and request type (validate or cancel) | ||
func generateMsisdnPath(msisdn string, requestType string) string { | ||
return fmt.Sprintf("%s/%s/%s", path, msisdn, requestType) | ||
} | ||
|
||
// generateRequestIdPath for given request id and request type (validate or cancel) | ||
func generateRequestIdPath(id string, requestType string) string { | ||
return fmt.Sprintf("%s/requestid/%s/%s", path, id, requestType) | ||
} | ||
|
||
// Send an otp code to the recipient mobile number | ||
func (c *Client) Send(body *api.SendOtp) (*api.Otp, error) { | ||
log := c.Logger.Lgr.With().Str("OTP API Layer", "Send").Logger() | ||
|
||
log.Debug().Msg("Sending an otp message") | ||
|
||
return c.do(path, body) | ||
} | ||
|
||
// VerifyByDestination Verify an otp code input by a user using destination number | ||
func (c *Client) VerifyByDestination(msisdn, code string) (*api.Otp, error) { | ||
log := c.Logger.Lgr.With().Str("OTP API Layer", "VerifyByDestination").Logger() | ||
log.Debug().Msg(fmt.Sprintf("Verifying an otp code: %s using destination number: %s", code, msisdn)) | ||
|
||
p := generateMsisdnPath(msisdn, "validate") | ||
|
||
return c.do(p, &verifyOtp{Code: code}) | ||
} | ||
|
||
// VerifyByRequestId Verify an otp code input by a user using request id | ||
func (c *Client) VerifyByRequestId(id, code string) (*api.Otp, error) { | ||
log := c.Logger.Lgr.With().Str("OTP API Layer", "VerifyByRequestId").Logger() | ||
log.Debug().Msg(fmt.Sprintf("Verifying an otp code: %s using request id: %s", code, id)) | ||
|
||
p := generateRequestIdPath(id, "validate") | ||
|
||
return c.do(p, &verifyOtp{Code: code}) | ||
} | ||
|
||
// CancelByDestination Cancel an OTP request using destination number | ||
func (c *Client) CancelByDestination(msisdn string) (*api.Otp, error) { | ||
log := c.Logger.Lgr.With().Str("OTP API Layer", "CancelByDestination").Logger() | ||
|
||
log.Debug().Msg(fmt.Sprintf("Cancelling an otp request using destination number:: %s", msisdn)) | ||
|
||
p := generateMsisdnPath(msisdn, "cancel") | ||
|
||
return c.do(p, nil) | ||
} | ||
|
||
// CancelByRequestId Cancel an OTP request using request id | ||
func (c *Client) CancelByRequestId(id string) (*api.Otp, error) { | ||
log := c.Logger.Lgr.With().Str("OTP API Layer", "CancelByRequestId").Logger() | ||
|
||
log.Debug().Msg(fmt.Sprintf("Cancelling an otp request using request id: %s", id)) | ||
|
||
p := generateRequestIdPath(id, "cancel") | ||
|
||
return c.do(p, nil) | ||
} | ||
|
||
// do send request to Client and wrap response in api.Otp | ||
func (c *Client) do(p string, v interface{}) (*api.Otp, error) { | ||
req, err := c.Handler.NewRequest(http.MethodPost, p, v) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
o := &api.Otp{} | ||
|
||
err = c.Handler.Do(req, o) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return o, nil | ||
} |
Oops, something went wrong.