diff --git a/request.go b/request.go index ea78ba1..ff7ad6f 100644 --- a/request.go +++ b/request.go @@ -6,19 +6,26 @@ import ( "errors" "fmt" "io/ioutil" - "log" "net/http" ) +type RequestOpts struct { + Client http.Client + Method string + URL string + Data []byte + InjectBearer bool +} + // Request - for http requests. Expect method, url and body. Will inject bearer token and tracing id to request. -func Request(ctx context.Context, method string, url string, b []byte, auth bool) ([]byte, int, error) { - req, err := http.NewRequest(method, url, bytes.NewBuffer(b)) +func Request(ctx context.Context, params RequestOpts) ([]byte, int, error) { + req, err := http.NewRequest(params.Method, params.URL, bytes.NewBuffer(params.Data)) if err != nil { return nil, 0, err } req.Header.Add("Content-Type", "application/json") - if auth { + if params.InjectBearer { if err := SetAuthorization(ctx, req); err != nil { return nil, 0, err } @@ -27,8 +34,7 @@ func Request(ctx context.Context, method string, url string, b []byte, auth bool return nil, 0, err } - client := http.Client{} - resp, err := client.Do(req) + resp, err := params.Client.Do(req) if err != nil { return nil, 0, err } @@ -43,7 +49,6 @@ func Request(ctx context.Context, method string, url string, b []byte, auth bool } if resp.StatusCode >= 400 { - log.Printf("[DEBUG] %s %s: %d (%s) %s", method, url, resp.StatusCode, http.StatusText(resp.StatusCode), string(body)) return nil, resp.StatusCode, errors.New(string(body)) } @@ -64,4 +69,4 @@ func SetAuthorization(ctx context.Context, req *http.Request) error { req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", jwtToken)) return nil -} \ No newline at end of file +} diff --git a/request_test.go b/request_test.go index 2048dc9..0edad83 100644 --- a/request_test.go +++ b/request_test.go @@ -23,14 +23,16 @@ func TestRequest(t *testing.T) { defer ts.Close() t.Run("invalid method", func(t *testing.T) { - resp, code, err := Request(context.Background(), " ", "", []byte{}, false) + resp, code, err := Request(context.Background(), RequestOpts{}) require.Error(t, err) require.Nil(t, resp) require.Equal(t, 0, code) }) t.Run("no token", func(t *testing.T) { - resp, code, err := Request(context.Background(), "", "", []byte{}, true) + resp, code, err := Request(context.Background(), RequestOpts{ + InjectBearer: true, + }) require.Error(t, err) require.Nil(t, resp) require.Equal(t, 0, code) @@ -38,7 +40,9 @@ func TestRequest(t *testing.T) { t.Run("no span", func(t *testing.T) { ctx := context.WithValue(context.Background(), "token", "test") - resp, code, err := Request(ctx, "", "", []byte{}, true) + resp, code, err := Request(ctx, RequestOpts{ + InjectBearer: true, + }) require.Error(t, err) require.Nil(t, resp) require.Equal(t, 0, code) @@ -49,7 +53,9 @@ func TestRequest(t *testing.T) { span, ctx := opentracing.StartSpanFromContext(ctx, "test") require.NotNil(t, span) - resp, code, err := Request(ctx, "", "", []byte{}, true) + resp, code, err := Request(ctx, RequestOpts{ + InjectBearer: true, + }) require.Error(t, err) require.Nil(t, resp) require.Equal(t, 0, code) @@ -60,7 +66,11 @@ func TestRequest(t *testing.T) { span, ctx := opentracing.StartSpanFromContext(ctx, "test") require.NotNil(t, span) - resp, code, err := Request(ctx, "GET", ts.URL, []byte{}, true) + resp, code, err := Request(ctx, RequestOpts{ + Method: "GET", + URL: ts.URL, + InjectBearer: true, + }) require.Error(t, err) require.Nil(t, resp) require.Equal(t, http.StatusBadRequest, code) @@ -71,7 +81,11 @@ func TestRequest(t *testing.T) { span, ctx := opentracing.StartSpanFromContext(ctx, "test") require.NotNil(t, span) - resp, code, err := Request(ctx, "POST", ts.URL, []byte{}, true) + resp, code, err := Request(ctx, RequestOpts{ + Method: "POST", + URL: ts.URL, + InjectBearer: true, + }) require.NoError(t, err) require.Equal(t, []byte("OK"), resp) require.Equal(t, http.StatusOK, code) @@ -94,4 +108,4 @@ func TestSetAuthorization(t *testing.T) { req := httptest.NewRequest("GET", "http://test.com", nil) require.NoError(t, SetAuthorization(ctx, req)) }) -} \ No newline at end of file +}