Skip to content

Commit

Permalink
made few refactor and contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Collins authored and Marvin Collins committed Jan 23, 2024
1 parent f44b6f5 commit 3d028fc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work
.idea
/.idea
20 changes: 12 additions & 8 deletions fastotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/CeoFred/fast-otp/lib"
)

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

type FastOtp struct {
APIKey *string
BaseURL string
Expand Down Expand Up @@ -38,18 +42,18 @@ type DeliveryDetails struct {
Email string `json:"email"`
}

type OtpDelivery map[string]string

type GenerateOTPPayload struct {
Delivery struct {
Email string `json:"email"`
} `json:"delivery"`
Identifier string `json:"identifier"`
TokenLength int `json:"token_length"`
Type string `json:"type"`
Validity int `json:"validity"`
Delivery OtpDelivery `json:"delivery"`
Identifier string `json:"identifier"`
TokenLength int `json:"token_length"`
Type string `json:"type"`
Validity int `json:"validity"`
}

func NewFastOTP(apiKey string) *FastOtp {
return &FastOtp{APIKey: &apiKey, BaseURL: "https://api.fastotp.co"}
return &FastOtp{APIKey: &apiKey, BaseURL: BaseUrl}
}

func (f *FastOtp) GenerateOTP(payload GenerateOTPPayload) (*OTP, error) {
Expand Down
10 changes: 5 additions & 5 deletions fastotp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func TestGenerateOTP(t *testing.T) {

fastOtp := &FastOtp{APIKey: new(string), BaseURL: server.URL}

delivery := OtpDelivery{
"email": "[email protected]",
}

payload := GenerateOTPPayload{
Delivery: struct {
Email string `json:"email"`
}{
Email: "[email protected]",
},
Delivery: delivery,
Identifier: "test_identifier",
TokenLength: 6,
Type: "alpha_numeric",
Expand Down
8 changes: 4 additions & 4 deletions lib/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpclient
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

Expand All @@ -20,7 +21,6 @@ func NewAPIClient(baseURL, apiKey string) *APIClient {
}
}


// Post sends a POST request to the specified endpoint with the given payload.
func (c *APIClient) Post(endpoint string, payload interface{}) (*http.Response, error) {
url := c.BaseURL + endpoint
Expand All @@ -31,14 +31,14 @@ func (c *APIClient) Post(endpoint string, payload interface{}) (*http.Response,
return nil, err
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.APIKey)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.APIKey))

client := http.DefaultClient
return client.Do(req)
}
}

0 comments on commit 3d028fc

Please sign in to comment.