2020-04-30 17:39:33 +02:00
|
|
|
package requests
|
|
|
|
|
|
|
|
|
|
import (
|
2020-05-18 21:36:00 +02:00
|
|
|
"bytes"
|
|
|
|
|
"compress/gzip"
|
2020-04-30 17:39:33 +02:00
|
|
|
"fmt"
|
2020-05-18 21:36:00 +02:00
|
|
|
"io/ioutil"
|
2020-04-30 17:39:33 +02:00
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func newReplacer(values map[string]interface{}) *strings.Replacer {
|
|
|
|
|
var replacerItems []string
|
|
|
|
|
for k, v := range values {
|
2020-08-25 23:24:31 +02:00
|
|
|
replacerItems = append(replacerItems, fmt.Sprintf("{{%s}}", k), fmt.Sprintf("%s", v), k, fmt.Sprintf("%s", v))
|
2020-04-30 17:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.NewReplacer(replacerItems...)
|
|
|
|
|
}
|
2020-05-18 21:36:00 +02:00
|
|
|
|
|
|
|
|
// HandleDecompression if the user specified a custom encoding (as golang transport doesn't do this automatically)
|
2020-09-28 02:17:35 +02:00
|
|
|
func HandleDecompression(r *HTTPRequest, bodyOrig []byte) (bodyDec []byte, err error) {
|
|
|
|
|
if r.Request == nil {
|
|
|
|
|
return bodyOrig, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encodingHeader := strings.ToLower(r.Request.Header.Get("Accept-Encoding"))
|
2020-05-18 21:36:00 +02:00
|
|
|
if encodingHeader == "gzip" {
|
|
|
|
|
gzipreader, err := gzip.NewReader(bytes.NewReader(bodyOrig))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return bodyDec, err
|
|
|
|
|
}
|
|
|
|
|
defer gzipreader.Close()
|
|
|
|
|
|
|
|
|
|
bodyDec, err = ioutil.ReadAll(gzipreader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return bodyDec, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bodyDec, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bodyOrig, nil
|
|
|
|
|
}
|
2020-09-28 02:17:35 +02:00
|
|
|
|
|
|
|
|
// ZipMapValues converts values from strings slices to flat string
|
|
|
|
|
func ZipMapValues(m map[string][]string) (m1 map[string]string) {
|
|
|
|
|
m1 = make(map[string]string)
|
|
|
|
|
for k, v := range m {
|
|
|
|
|
m1[k] = strings.Join(v, "")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExpandMapValues converts values from flat string to strings slice
|
|
|
|
|
func ExpandMapValues(m map[string]string) (m1 map[string][]string) {
|
|
|
|
|
m1 = make(map[string][]string)
|
|
|
|
|
for k, v := range m {
|
|
|
|
|
m1[k] = []string{v}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|