From 90df217f43c1d2f08b0be0e7bb00a683cd60e5a3 Mon Sep 17 00:00:00 2001 From: ochom Date: Mon, 9 Dec 2024 01:18:29 +0300 Subject: [PATCH] update handle request timeouts --- gttp/http.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/gttp/http.go b/gttp/http.go index 3f61061..9a4127a 100644 --- a/gttp/http.go +++ b/gttp/http.go @@ -42,7 +42,7 @@ func getTimeout(timeout ...time.Duration) time.Duration { } // Post sends a POST request to the specified URL. -func Post(url string, headers M, body any, timeout ...time.Duration) (res *Response, err error) { +func Post(url string, headers M, body any, timeout ...time.Duration) (resp *Response, err error) { req, err := http.NewRequest("POST", url, bytes.NewBuffer(helpers.ToBytes(body))) if err != nil { return @@ -56,23 +56,23 @@ func Post(url string, headers M, body any, timeout ...time.Duration) (res *Respo ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...)) defer cancel() - reqDo, err := getClient().Do(req.WithContext(ctx)) + res, err := getClient().Do(req.WithContext(ctx)) if err != nil { return } - defer reqDo.Body.Close() + defer res.Body.Close() - bodyBytes, err := io.ReadAll(reqDo.Body) + content, err := io.ReadAll(res.Body) if err != nil { return } - return &Response{reqDo.StatusCode, bodyBytes}, nil + return &Response{res.StatusCode, content}, nil } // Get sends a GET request to the specified URL. -func Get(url string, headers M, timeout ...time.Duration) (res *Response, err error) { +func Get(url string, headers M, timeout ...time.Duration) (resp *Response, err error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return @@ -86,23 +86,23 @@ func Get(url string, headers M, timeout ...time.Duration) (res *Response, err er ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...)) defer cancel() - reqDo, err := getClient().Do(req.WithContext(ctx)) + res, err := getClient().Do(req.WithContext(ctx)) if err != nil { return } - defer reqDo.Body.Close() + defer res.Body.Close() - bodyBytes, err := io.ReadAll(reqDo.Body) + content, err := io.ReadAll(res.Body) if err != nil { return } - return &Response{reqDo.StatusCode, bodyBytes}, nil + return &Response{res.StatusCode, content}, nil } // Custom sends a custom request to the specified URL. -func Custom(url, method string, headers M, body any, timeout ...time.Duration) (res *Response, err error) { +func Custom(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 @@ -116,17 +116,17 @@ func Custom(url, method string, headers M, body any, timeout ...time.Duration) ( ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...)) defer cancel() - reqDo, err := getClient().Do(req.WithContext(ctx)) + res, err := getClient().Do(req.WithContext(ctx)) if err != nil { return } - defer reqDo.Body.Close() + defer res.Body.Close() - bodyBytes, err := io.ReadAll(reqDo.Body) + content, err := io.ReadAll(res.Body) if err != nil { return } - return &Response{reqDo.StatusCode, bodyBytes}, nil + return &Response{res.StatusCode, content}, nil }