Skip to content

Commit

Permalink
Merge pull request #4 from neymarjimoh/chore/context
Browse files Browse the repository at this point in the history
add ctx propagation on all lib functions
  • Loading branch information
CeoFred authored Jan 28, 2024
2 parents 3ac2285 + 2f1c9a0 commit 93aaa2d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func main() {
// Create an instance of FastOtp
client := fastotp.NewFastOTP(apiKey)

// Create context for library functions
ctx := context.Background()

// Define OTP generation payload
payload := fastotp.GenerateOTPPayload{
Delivery: OtpDelivery{
Expand All @@ -42,7 +45,7 @@ func main() {
}

// Generate OTP
otp, err := client.GenerateOTP(payload)
otp, err := client.GenerateOTP(ctx, payload)
if err != nil {
log.Fatal(err)
}
Expand All @@ -51,9 +54,10 @@ func main() {

// Validate OTP
otp, err = client.ValidateOTP(
ctx,
ValidateOTPPayload{
Identifier: "user123",
Token: "123456"
Token: "123456",
}
)
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions fastotp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fastotp

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -62,8 +63,8 @@ func NewFastOTP(apiKey string) *FastOtp {
return &FastOtp{APIKey: &apiKey, BaseURL: BaseURL}
}

func (f *FastOtp) GenerateOTP(payload GenerateOTPPayload) (*OTP, error) {
cl := httpclient.NewAPIClient(f.BaseURL, *f.APIKey)
func (f *FastOtp) GenerateOTP(ctx context.Context, payload GenerateOTPPayload) (*OTP, error) {
cl := httpclient.NewAPIClient(f.BaseURL, *f.APIKey, ctx)
resp, err := cl.Post("/generate", payload)
if err != nil {
return nil, err
Expand Down Expand Up @@ -91,8 +92,8 @@ 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)
func (f *FastOtp) ValidateOTP(ctx context.Context, payload ValidateOTPPayload) (*OTP, error) {
cl := httpclient.NewAPIClient(f.BaseURL, *f.APIKey, ctx)
resp, err := cl.Post("/validate", payload)
if err != nil {
return nil, err
Expand Down Expand Up @@ -120,8 +121,8 @@ 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)
func (f *FastOtp) GetOtp(ctx context.Context, id string) (*OTP, error) {
cl := httpclient.NewAPIClient(f.BaseURL, *f.APIKey, ctx)
resp, err := cl.Get(id)
if err != nil {
fmt.Println("got here")
Expand Down
7 changes: 5 additions & 2 deletions fastotp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fastotp

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -55,7 +56,8 @@ func TestGenerateOTP(t *testing.T) {
Validity: 120,
}

otp, err := fastOtp.GenerateOTP(payload)
ctx := context.Background()
otp, err := fastOtp.GenerateOTP(ctx, payload)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -109,7 +111,8 @@ func TestValidateOTP(t *testing.T) {
Token: "123456",
}

otp, err := fastOtp.ValidateOTP(payload)
ctx := context.Background()
otp, err := fastOtp.ValidateOTP(ctx, payload)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand Down
25 changes: 14 additions & 11 deletions lib/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,62 @@ package httpclient

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)

// APIClient is a wrapper for making HTTP requests to the fastotp API.
type APIClient struct {
BaseURL string
APIKey string
baseURL string
apiKey string
ctx context.Context
}

// NewAPIClient creates a new instance of APIClient.
func NewAPIClient(baseURL, apiKey string) *APIClient {
func NewAPIClient(baseURL, apiKey string, ctx context.Context) *APIClient {
return &APIClient{
BaseURL: baseURL,
APIKey: apiKey,
baseURL: baseURL,
apiKey: apiKey,
ctx: ctx,
}
}

// 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
url := c.baseURL + endpoint

// Convert payload to JSON
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}

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

req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", c.APIKey)
req.Header.Set("x-api-key", c.apiKey)

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

// Get sends a GET request to the specified endpoint, appending id as a path parameter
func (c *APIClient) Get(id string) (*http.Response, error) {
url := fmt.Sprintf("%s/%s", c.BaseURL, id)
url := fmt.Sprintf("%s/%s", c.baseURL, id)
fmt.Println(url)

req, err := http.NewRequest(http.MethodGet, url, nil)
req, err := http.NewRequestWithContext(c.ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", c.APIKey)
req.Header.Set("x-api-key", c.apiKey)

client := http.DefaultClient
return client.Do(req)
Expand Down

0 comments on commit 93aaa2d

Please sign in to comment.