AddPointer helper, DumpHtml helper, fix query parameter escaping

This commit is contained in:
2024-01-15 15:41:38 -06:00
parent 5ff914758e
commit 7de9813174
2 changed files with 30 additions and 4 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
cover.cov cover.cov
banner banner
.*.go .*.go
dumps/

View File

@@ -5,6 +5,7 @@ import (
"io" "io"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url"
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
@@ -18,7 +19,7 @@ import (
// BuildRequestWithBody builds a request with the given method, path, parameters, and body // BuildRequestWithBody builds a request with the given method, path, parameters, and body
func BuildRequestWithBody(method string, path string, params map[string]string, body io.Reader) *http.Request { func BuildRequestWithBody(method string, path string, params map[string]string, body io.Reader) *http.Request {
// Builds a URL for the given path and parameters // Builds a URL for the given path and parameters
url := baseURL + path requestUrl := baseURL + path
if params != nil { if params != nil {
takenFirst := false takenFirst := false
@@ -29,11 +30,11 @@ func BuildRequestWithBody(method string, path string, params map[string]string,
takenFirst = true takenFirst = true
} }
url += paramChar + key + "=" + value requestUrl += paramChar + url.QueryEscape(key) + "=" + url.QueryEscape(value)
} }
} }
request, _ := http.NewRequest(method, url, body) request, _ := http.NewRequest(method, requestUrl, body)
AddUserAgent(request) AddUserAgent(request)
return request return request
} }
@@ -213,3 +214,27 @@ func GetFirstEnv(key ...string) string {
return "" return ""
} }
// GetPointer returns a pointer to the given value.
// This function is useful for discordgo, which inexplicably requires pointers to integers for minLength arguments.
func GetPointer(value int) *int {
return &value
}
// DumpHtml dumps a response body to a file for debugging purposes
func DumpHtml(res *http.Response) {
// Use current time as filename + /dumps/ prefix
filename := fmt.Sprintf("dumps/%d.html", time.Now().Unix())
file, err := os.Create(filename)
if err != nil {
log.Err(err).Msg("Error creating file")
return
}
defer file.Close()
_, err = io.Copy(file, res.Body)
if err != nil {
log.Err(err).Msg("Error copying response body")
return
}
}