Skip to content

Commit

Permalink
update http client default to go fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
ochom committed Dec 29, 2024
1 parent 64ce666 commit a365d72
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 99 deletions.
10 changes: 4 additions & 6 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cache

import (
"fmt"
"time"

"github.com/ochom/gutils/env"
Expand Down Expand Up @@ -49,13 +48,12 @@ func SetWithExpiry[T any](key string, value T, expiry time.Duration) error {
}

// Get ...
func Get[T any](key string) (T, error) {
v := conn.get(key)
if v == nil {
return *new(T), fmt.Errorf("key %s not found", key)
func Get[T any](key string) T {
if v := conn.get(key); v != nil {
return helpers.FromBytes[T](v)
}

return helpers.FromBytes[T](v), nil
return *new(T)
}

// Delete ...
Expand Down
16 changes: 8 additions & 8 deletions gttp/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ func (*defaultClient) getClient() *http.Client {
return client
}

// Post sends a POST request to the specified URL.
func (c *defaultClient) Post(url string, headers M, body any, timeout ...time.Duration) (resp *Response, err error) {
return c.SendRequest(url, "POST", headers, body, timeout...)
// post sends a POST request to the specified URL.
func (c *defaultClient) post(url string, headers M, body any, timeout ...time.Duration) (resp *Response, err error) {
return c.sendRequest(url, "POST", headers, body, timeout...)
}

// Get sends a GET request to the specified URL.
func (c *defaultClient) Get(url string, headers M, timeout ...time.Duration) (resp *Response, err error) {
return c.SendRequest(url, "GET", headers, nil, timeout...)
// get sends a GET request to the specified URL.
func (c *defaultClient) get(url string, headers M, timeout ...time.Duration) (resp *Response, err error) {
return c.sendRequest(url, "GET", headers, nil, timeout...)
}

// SendRequest sends a request to the specified URL.
func (c *defaultClient) SendRequest(url, method string, headers M, body any, timeout ...time.Duration) (resp *Response, err error) {
// sendRequest sends a request to the specified URL.
func (c *defaultClient) sendRequest(url, method string, headers M, body any, timeout ...time.Duration) (resp *Response, err error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(helpers.ToBytes(body)))
if err != nil {
return
Expand Down
21 changes: 11 additions & 10 deletions gttp/gofiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (

type fiberClient struct{}

// Post sends a POST request to the specified URL.
func (c *fiberClient) Post(url string, headers M, body any, timeouts ...time.Duration) (resp *Response, err error) {
return c.SendRequest(url, "POST", headers, body, timeouts...)
// post sends a POST request to the specified URL.
func (c *fiberClient) post(url string, headers M, body any, timeouts ...time.Duration) (resp *Response, err error) {
return c.sendRequest(url, "POST", headers, body, timeouts...)
}

// Get sends a GET request to the specified URL.
func (c *fiberClient) Get(url string, headers M, timeouts ...time.Duration) (resp *Response, err error) {
return c.SendRequest(url, "GET", headers, nil, timeouts...)
// get sends a GET request to the specified URL.
func (c *fiberClient) get(url string, headers M, timeouts ...time.Duration) (resp *Response, err error) {
return c.sendRequest(url, "GET", headers, nil, timeouts...)
}

// SendRequest sends a request to the specified URL.
func (c *fiberClient) SendRequest(url, method string, headers M, body any, timeouts ...time.Duration) (resp *Response, err error) {
// sendRequest sends a request to the specified URL.
func (c *fiberClient) sendRequest(url, method string, headers M, body any, timeouts ...time.Duration) (resp *Response, err error) {
timeout := time.Hour
if len(timeouts) > 0 {
timeout = timeouts[0]
Expand All @@ -37,12 +37,13 @@ func (c *fiberClient) SendRequest(url, method string, headers M, body any, timeo
case <-ctx.Done():
return &Response{500, nil}, ctx.Err()
default:
return c.sendRequest(url, method, headers, body)
return c.makeRequest(url, method, headers, body)
}
}
}

func (c *fiberClient) sendRequest(url, method string, headers M, body any) (resp *Response, err error) {
// makeRequest sends a request to the specified URL.
func (c *fiberClient) makeRequest(url, method string, headers M, body any) (resp *Response, err error) {
client := fiber.AcquireClient()
var req *fiber.Agent

Expand Down
36 changes: 26 additions & 10 deletions gttp/gttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gttp
import (
"time"

"github.com/ochom/gutils/logs"
"github.com/ochom/gutils/env"
)

type ClientType int
Expand All @@ -14,19 +14,35 @@ const (
)

type Client interface {
Post(url string, headers M, body any, timeout ...time.Duration) (resp *Response, err error)
Get(url string, headers M, timeout ...time.Duration) (resp *Response, err error)
SendRequest(url, method string, headers M, body any, timeout ...time.Duration) (resp *Response, err error)
post(url string, headers M, body any, timeout ...time.Duration) (resp *Response, err error)
get(url string, headers M, timeout ...time.Duration) (resp *Response, err error)
sendRequest(url, method string, headers M, body any, timeout ...time.Duration) (resp *Response, err error)
}

func NewClient(clientType ClientType) Client {
switch clientType {
var client Client

func init() {
switch env.Int("HTTP_CLIENT", 1) {
case DefaultHttp:
return &defaultClient{}
client = new(defaultClient)
case GoFiber:
return &fiberClient{}
client = new(fiberClient)
default:
logs.Error("Unknown provider: %d", clientType)
return nil
panic("unknown http client")
}
}

// Post sends a POST request to the specified URL.
func Post(url string, headers M, body any, timeout ...time.Duration) (resp *Response, err error) {
return client.post(url, headers, body, timeout...)
}

// Get sends a GET request to the specified URL.
func Get(url string, headers M, timeout ...time.Duration) (resp *Response, err error) {
return client.get(url, headers, timeout...)
}

// SendRequest sends a request to the specified URL.
func SendRequest(url, method string, headers M, body any, timeout ...time.Duration) (resp *Response, err error) {
return client.sendRequest(url, method, headers, body, timeout...)
}
65 changes: 0 additions & 65 deletions gttp/http_test.go

This file was deleted.

0 comments on commit a365d72

Please sign in to comment.