Skip to content

Commit

Permalink
add http timeout to gofiber
Browse files Browse the repository at this point in the history
  • Loading branch information
ochom committed Dec 29, 2024
1 parent 399c8d2 commit 2992600
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions gttp/gofiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,9 @@ import (

type fiberClient struct{}

func do(req *fiber.Agent) (int, []byte, error) {
code, content, errs := req.Bytes()
if len(errs) > 0 {
for _, err := range errs {
logs.Error("client error: %s", err.Error())
}

return 0, nil, errs[0]
}

return code, content, nil
}

func getClient(url, method string) *fiber.Agent {
client := fiber.AcquireClient()
var req *fiber.Agent
switch method {
case "POST":
req = client.Post(url)
case "GET":
req = client.Get(url)
case "DELETE":
req = client.Delete(url)
case "PUT":
req = client.Put(url)
case "PATCH":
req = client.Patch(url)
default:
panic(fmt.Errorf("unknown method: %s", method))
}

req.InsecureSkipVerify()
return req
}

// 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) {
req := getClient(url, "POST")
req := c.getClient(url, "POST")
for k, v := range headers {
req.Add(k, v)
}
Expand All @@ -68,7 +33,7 @@ func (c *fiberClient) Post(url string, headers M, body any, timeouts ...time.Dur
return nil, ctx.Err()
default:
req.Body(helpers.ToBytes(body))
code, content, err := do(req)
code, content, err := c.do(req)
return &Response{code, content}, err
}
}
Expand All @@ -77,7 +42,7 @@ func (c *fiberClient) Post(url string, headers M, body any, timeouts ...time.Dur

// Get sends a GET request to the specified URL.
func (c *fiberClient) Get(url string, headers M, timeouts ...time.Duration) (resp *Response, err error) {
req := getClient(url, "GET")
req := c.getClient(url, "GET")
for k, v := range headers {
req.Add(k, v)
}
Expand All @@ -95,7 +60,7 @@ func (c *fiberClient) Get(url string, headers M, timeouts ...time.Duration) (res
case <-ctx.Done():
return nil, ctx.Err()
default:
code, content, err := do(req)
code, content, err := c.do(req)
return &Response{code, content}, err
}
}
Expand All @@ -104,7 +69,7 @@ func (c *fiberClient) Get(url string, headers M, timeouts ...time.Duration) (res

// Custom sends a custom request to the specified URL.
func (c *fiberClient) Custom(url, method string, headers M, body any, timeouts ...time.Duration) (resp *Response, err error) {
req := getClient(url, method)
req := c.getClient(url, method)
for k, v := range headers {
req.Add(k, v)
}
Expand All @@ -123,8 +88,43 @@ func (c *fiberClient) Custom(url, method string, headers M, body any, timeouts .
return nil, ctx.Err()
default:
req.Body(helpers.ToBytes(body))
code, content, err := do(req)
code, content, err := c.do(req)
return &Response{code, content}, err
}
}
}

func (fiberClient) do(req *fiber.Agent) (int, []byte, error) {
code, content, errs := req.Bytes()
if len(errs) > 0 {
for _, err := range errs {
logs.Error("client error: %s", err.Error())
}

return 0, nil, errs[0]
}

return code, content, nil
}

func (fiberClient) getClient(url, method string) *fiber.Agent {
client := fiber.AcquireClient()
var req *fiber.Agent
switch method {
case "POST":
req = client.Post(url)
case "GET":
req = client.Get(url)
case "DELETE":
req = client.Delete(url)
case "PUT":
req = client.Put(url)
case "PATCH":
req = client.Patch(url)
default:
panic(fmt.Errorf("unknown method: %s", method))
}

req.InsecureSkipVerify()
return req
}

0 comments on commit 2992600

Please sign in to comment.