Skip to content

Commit

Permalink
calc: Use MakeRequest helper and expand it to accept ErrorResponses
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Jul 2, 2024
1 parent 59e80bc commit 4b0ab1f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
39 changes: 13 additions & 26 deletions plugin/calc/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package calc
import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
Expand Down Expand Up @@ -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
}

Expand Down
43 changes: 36 additions & 7 deletions utils/httpUtils/httpUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4b0ab1f

Please sign in to comment.