Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
LordRahl90 committed Jan 27, 2024
1 parent 3ac2285 commit 1236f50
Show file tree
Hide file tree
Showing 10 changed files with 675 additions and 114 deletions.
36 changes: 34 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,42 @@ on:

jobs:

lint:
name: Linting
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21.1'
cache: false

- name: Lint and Vet
uses: golangci/golangci-lint-action@v3
with:
version: latest

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21.1'

- name: Test
run: go test -v ./... --cover

build:
runs-on: ubuntu-latest
needs:
- lint
- test
steps:
- uses: actions/checkout@v3

Expand All @@ -24,5 +58,3 @@ jobs:
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
53 changes: 33 additions & 20 deletions fastotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,75 @@ import (
)

const (
BaseURL = "https://api.fastotp.co"
baseURL = "https://api.fastotp.co"
)

type FastOtp struct {
APIKey *string
// FastOTP is the main struct for the FastOtp package.
type FastOTP struct {
APIKey string
BaseURL string
client HttpClient
}

// ErrorResponse is the error struct for the FastOtp package.
type ErrorResponse struct {
Errors map[string][]string `json:"errors"`
Message string `json:"message"`
}

// OTP is the struct for the OTP object.
type OTP struct {
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
UpdatedAt time.Time `json:"updated_at"`
DeliveryDetails DeliveryDetails `json:"delivery_details"`
ID string `json:"id"`
Identifier string `json:"identifier"`
Status string `json:"status"`
Type string `json:"type"`
Status OTPStatus `json:"status"`
Type OTPType `json:"type"`
DeliveryMethods []string `json:"delivery_methods"`
}

// OTPResponse is the struct for the OTP response object.
type OTPResponse struct {
OTP OTP `json:"otp"`
}

// DeliveryDetails is the struct for the DeliveryDetails object.
type DeliveryDetails struct {
Email string `json:"email"`
}

type OtpDelivery map[string]string
// OTPDelivery is the struct for the OtpDelivery object.
type OTPDelivery map[string]string

// GenerateOTPPayload is the struct for the GenerateOTPPayload object.
type GenerateOTPPayload struct {
Delivery OtpDelivery `json:"delivery"`
Delivery OTPDelivery `json:"delivery"`
Identifier string `json:"identifier"`
Type string `json:"type"`
Type OTPType `json:"type"`
TokenLength int `json:"token_length"`
Validity int `json:"validity"`
}

// ValidateOTPPayload is the struct for the ValidateOTPPayload object.
type ValidateOTPPayload struct {
Identifier string `json:"identifier"`
Token string `json:"token"`
}

func NewFastOTP(apiKey string) *FastOtp {
return &FastOtp{APIKey: &apiKey, BaseURL: BaseURL}
// NewFastOTP creates a new FastOtp instance.
func NewFastOTP(apiKey string) *FastOTP {
return &FastOTP{
APIKey: apiKey,
BaseURL: baseURL,
client: httpclient.NewAPIClient(baseURL, apiKey),
}
}

func (f *FastOtp) GenerateOTP(payload GenerateOTPPayload) (*OTP, error) {
cl := httpclient.NewAPIClient(f.BaseURL, *f.APIKey)
resp, err := cl.Post("/generate", payload)
// GenerateOTP generates an OTP.
func (f *FastOTP) GenerateOTP(payload GenerateOTPPayload) (*OTP, error) {
resp, err := f.client.Post("/generate", payload)
if err != nil {
return nil, err
}
Expand All @@ -91,9 +105,9 @@ func (f *FastOtp) GenerateOTP(payload GenerateOTPPayload) (*OTP, error) {
return &otpResponse.OTP, nil
}

func (f *FastOtp) ValidateOTP(payload ValidateOTPPayload) (*OTP, error) {
cl := httpclient.NewAPIClient(f.BaseURL, *f.APIKey)
resp, err := cl.Post("/validate", payload)
// ValidateOTP validates an OTP.
func (f *FastOTP) ValidateOTP(payload ValidateOTPPayload) (*OTP, error) {
resp, err := f.client.Post("/validate", payload)
if err != nil {
return nil, err
}
Expand All @@ -120,11 +134,10 @@ func (f *FastOtp) ValidateOTP(payload ValidateOTPPayload) (*OTP, error) {
return &otpResponse.OTP, nil
}

func (f *FastOtp) GetOtp(id string) (*OTP, error) {
cl := httpclient.NewAPIClient(f.BaseURL, *f.APIKey)
resp, err := cl.Get(id)
// GetOtp gets a new otp
func (f *FastOTP) GetOtp(id string) (*OTP, error) {
resp, err := f.client.Get(id)
if err != nil {
fmt.Println("got here")
return nil, err
}
defer resp.Body.Close()
Expand Down
Loading

0 comments on commit 1236f50

Please sign in to comment.