From 92d34b41a5f0e6c6a24d465002fab220f9e1874c Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:09:51 +0200 Subject: [PATCH 01/20] check error first --- httphelpers.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/httphelpers.go b/httphelpers.go index b0be3b6..b0f473a 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -305,12 +305,7 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa } } - response := httpResponse{} - resp, err := r.Client.Do(req) - if resp != nil { - response.Code = resp.StatusCode - } if err != nil { if urlErr, ok := err.(*url.Error); ok { if urlErr.Err == io.EOF { @@ -320,6 +315,10 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa return nil, errors.Wrap(err, "while making http request") } + response := httpResponse{ + Code: resp.StatusCode, + } + defer resp.Body.Close() responseBody, err := ioutil.ReadAll(resp.Body) if err != nil { From 3e229e3241642fdda583aeffbef6bfef97ca9e7c Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:10:37 +0200 Subject: [PATCH 02/20] fixed packet collisions --- httphelpers.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/httphelpers.go b/httphelpers.go index b0f473a..745484a 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -330,16 +330,16 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa } func (r *httpRequest) generateUrlWithParameters() (string, error) { - url, err := url.Parse(r.URL) + uri, err := url.Parse(r.URL) if err != nil { return "", err } - if !validURL.MatchString(url.Path) { + if !validURL.MatchString(uri.Path) { return "", errors.New(`BaseAPI must end with a /v1, /v2, /v3 or /v4; setBaseAPI("https://host/v3")`) } - q := url.Query() + q := uri.Query() if r.Parameters != nil && len(r.Parameters) > 0 { for name, values := range r.Parameters { for _, value := range values { @@ -347,9 +347,9 @@ func (r *httpRequest) generateUrlWithParameters() (string, error) { } } } - url.RawQuery = q.Encode() + uri.RawQuery = q.Encode() - return url.String(), nil + return uri.String(), nil } func (r *httpRequest) curlString(req *http.Request, p payload) string { From 0a8ee84e46f052f7d5b2c88b0cf594c7eba7a9a0 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:12:15 +0200 Subject: [PATCH 03/20] switched to errors.As https://go.dev/blog/go1.13-errors --- httphelpers.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httphelpers.go b/httphelpers.go index 745484a..2e50229 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -307,11 +307,13 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa resp, err := r.Client.Do(req) if err != nil { - if urlErr, ok := err.(*url.Error); ok { + var urlErr *url.Error + if errors.As(err, &urlErr) { if urlErr.Err == io.EOF { return nil, errors.Wrap(err, "remote server prematurely closed connection") } } + return nil, errors.Wrap(err, "while making http request") } From d9b1b151581c886d7cac98dfd6ad071c707da6a3 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:15:07 +0200 Subject: [PATCH 04/20] ioutil is deprecated --- httphelpers.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/httphelpers.go b/httphelpers.go index 2e50229..1a5f1a4 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "mime/multipart" "net/http" "net/url" @@ -322,7 +321,7 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa } defer resp.Body.Close() - responseBody, err := ioutil.ReadAll(resp.Body) + responseBody, err := io.ReadAll(resp.Body) if err != nil { return nil, errors.Wrap(err, "while reading response body") } From 0ff541eefbfb143ea85ca0d4fed7495f95451d32 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:15:37 +0200 Subject: [PATCH 05/20] moved defer --- httphelpers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httphelpers.go b/httphelpers.go index 1a5f1a4..b888b81 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -316,11 +316,12 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa return nil, errors.Wrap(err, "while making http request") } + defer resp.Body.Close() + response := httpResponse{ Code: resp.StatusCode, } - defer resp.Body.Close() responseBody, err := io.ReadAll(resp.Body) if err != nil { return nil, errors.Wrap(err, "while reading response body") From 86414ffc9a8ae1b3fffd79a3d636598dcaa2f477 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:18:04 +0200 Subject: [PATCH 06/20] fix error msg --- httphelpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httphelpers.go b/httphelpers.go index b888b81..ff7e4d7 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -338,7 +338,7 @@ func (r *httpRequest) generateUrlWithParameters() (string, error) { } if !validURL.MatchString(uri.Path) { - return "", errors.New(`BaseAPI must end with a /v1, /v2, /v3 or /v4; setBaseAPI("https://host/v3")`) + return "", errors.New(`APIBase() must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`) } q := uri.Query() From 22c664f58b09a5a6221f1a307d81a8fde8d2ef08 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:18:38 +0200 Subject: [PATCH 07/20] fix package collision --- httphelpers.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httphelpers.go b/httphelpers.go index ff7e4d7..981d283 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -249,7 +249,7 @@ func (r *httpRequest) makeDeleteRequest(ctx context.Context) (*httpResponse, err } func (r *httpRequest) NewRequest(ctx context.Context, method string, payload payload) (*http.Request, error) { - url, err := r.generateUrlWithParameters() + uri, err := r.generateUrlWithParameters() if err != nil { return nil, err } @@ -262,7 +262,8 @@ func (r *httpRequest) NewRequest(ctx context.Context, method string, payload pay } else { body = nil } - req, err := http.NewRequest(method, url, body) + + req, err := http.NewRequest(method, uri, body) if err != nil { return nil, err } From d7c98177a7d4c1a0aa6ce2109175560a5c3a56f7 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:19:20 +0200 Subject: [PATCH 08/20] http.NewRequestWithContext --- httphelpers.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/httphelpers.go b/httphelpers.go index 981d283..dbaec31 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -263,13 +263,11 @@ func (r *httpRequest) NewRequest(ctx context.Context, method string, payload pay body = nil } - req, err := http.NewRequest(method, uri, body) + req, err := http.NewRequestWithContext(ctx, method, uri, body) if err != nil { return nil, err } - req = req.WithContext(ctx) - if payload != nil && payload.getContentType() != "" { req.Header.Add("Content-Type", payload.getContentType()) } From b27f1da87e3a48e26db74d6c2c1a0deaf8143b0c Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:20:07 +0200 Subject: [PATCH 09/20] fmt --- httphelpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/httphelpers.go b/httphelpers.go index dbaec31..c490da3 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -284,6 +284,7 @@ func (r *httpRequest) NewRequest(ctx context.Context, method string, payload pay } req.Header.Add(header, value) } + return req, nil } From 53b6e7d78b71c9fdc267817085f33bbc36c9dfcb Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:21:00 +0200 Subject: [PATCH 10/20] http constants --- httphelpers.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httphelpers.go b/httphelpers.go index c490da3..0781bc9 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -233,19 +233,19 @@ func (r *httpRequest) addHeader(name, value string) { } func (r *httpRequest) makeGetRequest(ctx context.Context) (*httpResponse, error) { - return r.makeRequest(ctx, "GET", nil) + return r.makeRequest(ctx, http.MethodGet, nil) } func (r *httpRequest) makePostRequest(ctx context.Context, payload payload) (*httpResponse, error) { - return r.makeRequest(ctx, "POST", payload) + return r.makeRequest(ctx, http.MethodPost, payload) } func (r *httpRequest) makePutRequest(ctx context.Context, payload payload) (*httpResponse, error) { - return r.makeRequest(ctx, "PUT", payload) + return r.makeRequest(ctx, http.MethodPut, payload) } func (r *httpRequest) makeDeleteRequest(ctx context.Context) (*httpResponse, error) { - return r.makeRequest(ctx, "DELETE", nil) + return r.makeRequest(ctx, http.MethodDelete, nil) } func (r *httpRequest) NewRequest(ctx context.Context, method string, payload payload) (*http.Request, error) { From c9fc54f5094a197703c1a14dca45504af5baba78 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:22:53 +0200 Subject: [PATCH 11/20] added TODO --- httphelpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/httphelpers.go b/httphelpers.go index 0781bc9..0458a33 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -220,6 +220,7 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { func (f *formDataPayload) getContentType() string { if f.contentType == "" { + // TODO(vtopc): handle error: f.getPayloadBuffer() } return f.contentType From 086b08a06a825c49dd2ff5caae70fe2f76e50c42 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:36:07 +0200 Subject: [PATCH 12/20] added TODOs --- httphelpers.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/httphelpers.go b/httphelpers.go index 0458a33..0daf28e 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -176,6 +176,7 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { for _, keyVal := range f.Values { if tmp, err := writer.CreateFormField(keyVal.key); err == nil { + // TODO(DE-1139): handle error: tmp.Write([]byte(keyVal.value)) } else { return nil, err @@ -185,7 +186,9 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { for _, file := range f.Files { if tmp, err := writer.CreateFormFile(file.key, path.Base(file.value)); err == nil { if fp, err := os.Open(file.value); err == nil { + // TODO(DE-1139): defer in a loop: defer fp.Close() + // TODO(DE-1139): handle error: io.Copy(tmp, fp) } else { return nil, err @@ -197,7 +200,9 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { for _, file := range f.ReadClosers { if tmp, err := writer.CreateFormFile(file.key, file.name); err == nil { + // TODO(DE-1139): defer in a loop: defer file.value.Close() + // TODO(DE-1139): handle error: io.Copy(tmp, file.value) } else { return nil, err @@ -207,6 +212,7 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { for _, buff := range f.Buffers { if tmp, err := writer.CreateFormFile(buff.key, buff.name); err == nil { r := bytes.NewReader(buff.value) + // TODO(DE-1139): handle error: io.Copy(tmp, r) } else { return nil, err @@ -220,7 +226,7 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { func (f *formDataPayload) getContentType() string { if f.contentType == "" { - // TODO(vtopc): handle error: + // TODO(DE-1139): handle error: f.getPayloadBuffer() } return f.contentType From 7ff0ae2bf8457b3885d88f269cd3d61359bcaa1f Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:37:16 +0200 Subject: [PATCH 13/20] fmt --- httphelpers.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/httphelpers.go b/httphelpers.go index 0daf28e..1434f3f 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -130,6 +130,7 @@ func (f *urlEncodedPayload) getPayloadBuffer() (*bytes.Buffer, error) { for _, keyVal := range f.Values { data.Add(keyVal.key, keyVal.value) } + return bytes.NewBufferString(data.Encode()), nil } @@ -229,6 +230,7 @@ func (f *formDataPayload) getContentType() string { // TODO(DE-1139): handle error: f.getPayloadBuffer() } + return f.contentType } @@ -335,6 +337,7 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa } response.Data = responseBody + return &response, nil } @@ -392,5 +395,6 @@ func (r *httpRequest) curlString(req *http.Request, p payload) string { } } } + return strings.Join(parts, " ") } From c7950592c45a8af6f51d00fe986d106161319e9f Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:39:25 +0200 Subject: [PATCH 14/20] error msg --- httphelpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httphelpers.go b/httphelpers.go index 1434f3f..3cea332 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -348,7 +348,7 @@ func (r *httpRequest) generateUrlWithParameters() (string, error) { } if !validURL.MatchString(uri.Path) { - return "", errors.New(`APIBase() must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`) + return "", errors.New(`APIBase must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`) } q := uri.Query() From 708243eeeff14171a2f74ce434535a6a38d8d3d7 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:40:38 +0200 Subject: [PATCH 15/20] errors.Is --- httphelpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httphelpers.go b/httphelpers.go index 3cea332..91986a0 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -317,7 +317,7 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa if err != nil { var urlErr *url.Error if errors.As(err, &urlErr) { - if urlErr.Err == io.EOF { + if errors.Is(urlErr.Err, io.EOF) { return nil, errors.Wrap(err, "remote server prematurely closed connection") } } From e5adcc5a33a192e84c9b9415815f0c97513d46b1 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:41:33 +0200 Subject: [PATCH 16/20] nilaway --- httphelpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httphelpers.go b/httphelpers.go index 91986a0..2208acf 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -317,7 +317,7 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa if err != nil { var urlErr *url.Error if errors.As(err, &urlErr) { - if errors.Is(urlErr.Err, io.EOF) { + if urlErr != nil && errors.Is(urlErr.Err, io.EOF) { return nil, errors.Wrap(err, "remote server prematurely closed connection") } } From 822285da8ea036a77636e189a20d295d53251171 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:42:04 +0200 Subject: [PATCH 17/20] single if --- httphelpers.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/httphelpers.go b/httphelpers.go index 2208acf..6aa3ab3 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -316,10 +316,8 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa resp, err := r.Client.Do(req) if err != nil { var urlErr *url.Error - if errors.As(err, &urlErr) { - if urlErr != nil && errors.Is(urlErr.Err, io.EOF) { - return nil, errors.Wrap(err, "remote server prematurely closed connection") - } + if errors.As(err, &urlErr) && urlErr != nil && errors.Is(urlErr.Err, io.EOF) { + return nil, errors.Wrap(err, "remote server prematurely closed connection") } return nil, errors.Wrap(err, "while making http request") From 5ab48194515ddcd914b10a2afe6dd9a26af06663 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:44:52 +0200 Subject: [PATCH 18/20] fixed TestInvalidBaseAPI --- mailgun_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mailgun_test.go b/mailgun_test.go index 9c343ec..0194b55 100644 --- a/mailgun_test.go +++ b/mailgun_test.go @@ -10,6 +10,7 @@ import ( "github.com/facebookgo/ensure" "github.com/mailgun/mailgun-go/v4" + "github.com/stretchr/testify/assert" ) const domain = "valid-mailgun-domain" @@ -34,7 +35,7 @@ func TestInvalidBaseAPI(t *testing.T) { ctx := context.Background() _, err := mg.GetDomain(ctx, "unknown.domain") ensure.NotNil(t, err) - ensure.DeepEqual(t, err.Error(), `BaseAPI must end with a /v1, /v2, /v3 or /v4; setBaseAPI("https://host/v3")`) + assert.EqualError(t, err, `APIBase must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`) } func TestValidBaseAPI(t *testing.T) { From c73cd19589e796bd5ef46a938c5ef571bb0f693e Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:45:52 +0200 Subject: [PATCH 19/20] removed redundant --- mailgun_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/mailgun_test.go b/mailgun_test.go index 0194b55..0c5c013 100644 --- a/mailgun_test.go +++ b/mailgun_test.go @@ -34,7 +34,6 @@ func TestInvalidBaseAPI(t *testing.T) { ctx := context.Background() _, err := mg.GetDomain(ctx, "unknown.domain") - ensure.NotNil(t, err) assert.EqualError(t, err, `APIBase must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`) } From 70d50441528ae0bd9c52764b3f94a9f47f703ee0 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:56:42 +0200 Subject: [PATCH 20/20] removed verbose test run --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 11ec1d9..e5a129e 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ all: test .PHONY: test test: - export GO111MODULE=on; go test . -v + export GO111MODULE=on; go test . -race -count=1 .PHONY: godoc godoc: