mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-06 09:25:27 +08:00
50 lines
976 B
Go
50 lines
976 B
Go
package client
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/andybalholm/brotli"
|
|
)
|
|
|
|
func readResponseBody(resp *http.Response) ([]byte, error) {
|
|
encoding := strings.ToLower(strings.TrimSpace(resp.Header.Get("Content-Encoding")))
|
|
var reader io.Reader = resp.Body
|
|
switch encoding {
|
|
case "gzip":
|
|
gz, err := gzip.NewReader(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = gz.Close() }()
|
|
reader = gz
|
|
case "br":
|
|
reader = brotli.NewReader(resp.Body)
|
|
}
|
|
return io.ReadAll(reader)
|
|
}
|
|
|
|
func preview(b []byte) string {
|
|
s := strings.TrimSpace(string(b))
|
|
if len(s) > 160 {
|
|
return s[:160]
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (c *Client) jsonHeaders(headers map[string]string) map[string]string {
|
|
out := cloneStringMap(headers)
|
|
out["Content-Type"] = "application/json"
|
|
return out
|
|
}
|
|
|
|
func cloneStringMap(in map[string]string) map[string]string {
|
|
out := make(map[string]string, len(in))
|
|
for k, v := range in {
|
|
out[k] = v
|
|
}
|
|
return out
|
|
}
|