From 4b0ab1f6370d9c9a29f3b29aab0a595ddcf769cf Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 2 Jul 2024 18:30:08 +0200 Subject: [PATCH] calc: Use MakeRequest helper and expand it to accept ErrorResponses --- plugin/calc/calc.go | 39 +++++++++++--------------------- utils/httpUtils/httpUtils.go | 43 ++++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/plugin/calc/calc.go b/plugin/calc/calc.go index dd3e04b..bece2a5 100644 --- a/plugin/calc/calc.go +++ b/plugin/calc/calc.go @@ -3,7 +3,6 @@ package calc import ( "errors" "fmt" - "io" "math/rand" "net/http" "net/url" @@ -72,38 +71,26 @@ func (p *Plugin) Handlers(botInfo *gotgbot.User) []plugin.Handler { func calculate(expr string) (string, error) { expr = strings.ReplaceAll(expr, ",", ".") - var err error + var resp string + var errorResp string + var httpError *httpUtils.HttpError - resp, err := httpUtils.DefaultHttpClient.Get(fmt.Sprintf(ApiUrl, url.QueryEscape(expr))) - if err != nil { - return "", err - } - - if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusBadRequest { - return "", &httpUtils.HttpError{ - StatusCode: resp.StatusCode, - } - } + err := httpUtils.MakeRequest(httpUtils.RequestOptions{ + Method: httpUtils.MethodGet, + URL: fmt.Sprintf(ApiUrl, url.QueryEscape(expr)), + Response: &resp, + ErrorResponse: &errorResp, + }) - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - log.Err(err).Msg("failed to close response body") + if err != nil { + if errors.As(err, &httpError) && httpError.StatusCode == http.StatusBadRequest { + return "", &ApiError{Message: errorResp} } - }(resp.Body) - body, err := io.ReadAll(resp.Body) - if err != nil { return "", err } - result := string(body) - - if resp.StatusCode == http.StatusBadRequest { - return "", &ApiError{Message: result} - } - - result = strings.ReplaceAll(string(body), ".", ",") + result := strings.ReplaceAll(resp, ".", ",") return result, nil } diff --git a/utils/httpUtils/httpUtils.go b/utils/httpUtils/httpUtils.go index 0229875..0b87801 100644 --- a/utils/httpUtils/httpUtils.go +++ b/utils/httpUtils/httpUtils.go @@ -30,6 +30,9 @@ type ( // Response can either be a pointer to a JSON struct or a pointer to a string Response any + // ErrorResponse can either be a pointer to a JSON struct or a pointer to a string. + // NOTE: An error will still be returned! + ErrorResponse any Client *http.Client } @@ -126,20 +129,46 @@ func MakeRequest(opts RequestOptions) error { return err } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Err(err).Msg("Failed to close response body") + } + }(resp.Body) + if resp.StatusCode != http.StatusOK { + if opts.ErrorResponse != nil { + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return &HttpError{ + StatusCode: resp.StatusCode, + } + } + + switch v := opts.ErrorResponse.(type) { + case *string: + *v = string(bodyBytes) + default: + err = json.Unmarshal(bodyBytes, opts.ErrorResponse) + if err != nil { + return &HttpError{ + StatusCode: resp.StatusCode, + } + } + } + + log.Debug(). + Str("url", opts.URL). + Interface("result", opts.ErrorResponse). + Send() + } + return &HttpError{ StatusCode: resp.StatusCode, } } if opts.Response != nil { - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - log.Err(err).Msg("Failed to close response body") - } - }(resp.Body) - bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return err