Skip to content

Commit

Permalink
feat(http): Add Http Client Service Interface Function
Browse files Browse the repository at this point in the history
- Add IsNetworkTimeout(error) bool
  • Loading branch information
mushoffa committed Apr 26, 2022
1 parent 2211727 commit e5a0e38
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
24 changes: 17 additions & 7 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package http
import (
"crypto/tls"
"time"
net "net/http"
"net"
http1 "net/http"

"github.com/parnurzeal/gorequest"
)
Expand All @@ -19,8 +20,9 @@ const (
)

type HttpClientService interface {
Get(string, net.Header) ([]byte, error)
Post(string, interface{}, net.Header) ([]byte, error)
Get(string, http1.Header) ([]byte, error)
Post(string, interface{}, http1.Header) ([]byte, error)
IsNetworkTimeout(error) bool
}

type HttpClient struct {
Expand Down Expand Up @@ -52,7 +54,7 @@ func (c *HttpClient) RetryBad(retry int) *HttpClient {
return c
}

func (c *HttpClient) Get(url string, headers net.Header) ([]byte, error) {
func (c *HttpClient) Get(url string, headers http1.Header) ([]byte, error) {
request := gorequest.New()
request.SetDebug(c.isDebuggable)
agent := request.Get(url)
Expand All @@ -63,7 +65,7 @@ func (c *HttpClient) Get(url string, headers net.Header) ([]byte, error) {

_, body, errs := agent.
Timeout(c.timeout).
Retry(c.retryBad, time.Second, net.StatusInternalServerError).
Retry(c.retryBad, time.Second, http1.StatusInternalServerError).
End()

if errs != nil {
Expand All @@ -73,7 +75,7 @@ func (c *HttpClient) Get(url string, headers net.Header) ([]byte, error) {
return []byte(body), nil
}

func (c *HttpClient) Post(url string, jsonData interface{}, headers net.Header) ([]byte, error) {
func (c *HttpClient) Post(url string, jsonData interface{}, headers http1.Header) ([]byte, error) {
request := gorequest.New()
request.SetDebug(c.isDebuggable)

Expand All @@ -91,12 +93,20 @@ func (c *HttpClient) Post(url string, jsonData interface{}, headers net.Header)
_, body, errs := agent.
Send(jsonData).
Timeout(c.timeout).
Retry(c.retryBad, time.Second, net.StatusInternalServerError).
Retry(c.retryBad, time.Second, http1.StatusInternalServerError).
End()

if errs != nil {
return nil, errs[0]
}

return []byte(body), nil
}

func (c *HttpClient) IsNetworkTimeout(err error) bool {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return true
}

return false
}
19 changes: 18 additions & 1 deletion http/client_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package http

import (
// "fmt"
"testing"
"time"

"github.com/mushoffa/go-library/http"
)

var (
client = http.NewHttpClient()
)

func TestHttpClientGet_Success(t *testing.T) {
client := http.NewHttpClient()
// client := http.NewHttpClient()
response, err := client.Get("http://www.google.com", nil)
if err != nil {
t.Errorf("Error on http client GET request: %v", err)
Expand All @@ -16,4 +22,15 @@ func TestHttpClientGet_Success(t *testing.T) {
if response == nil {
t.Errorf("Error on http client GET response")
}
}

func TestIsNetworkTimeout_Success(t *testing.T) {
client.Timeout(5 * time.Second)
_, err := client.Get("YOUT_TIMEOUT_API", nil)
if err != nil {
isTimeout := client.IsNetworkTimeout(err)
if !isTimeout {
t.Errorf("Supposed to be timeout")
}
}
}

0 comments on commit e5a0e38

Please sign in to comment.