quintodrome/utils/cache/cached_http_client.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
2.2 KiB
Go
Raw Normal View History

2024-05-11 15:36:33 -08:00
package cache
2021-02-07 19:26:05 -09:00
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"strings"
"time"
)
2021-02-08 12:33:09 -09:00
const cacheSizeLimit = 100
2021-02-07 19:26:05 -09:00
2024-05-11 15:36:33 -08:00
type HTTPClient struct {
2024-06-21 13:57:43 -08:00
cache SimpleCache[string, string]
2021-02-07 19:26:05 -09:00
hc httpDoer
ttl time.Duration
2021-02-07 19:26:05 -09:00
}
type httpDoer interface {
Do(req *http.Request) (*http.Response, error)
}
type requestData struct {
Method string
Header http.Header
URL string
Body *string
}
2024-05-11 15:36:33 -08:00
func NewHTTPClient(wrapped httpDoer, ttl time.Duration) *HTTPClient {
c := &HTTPClient{hc: wrapped, ttl: ttl}
2024-06-21 13:57:43 -08:00
c.cache = NewSimpleCache[string, string](Options{
SizeLimit: cacheSizeLimit,
DefaultTTL: ttl,
})
return c
}
func (c *HTTPClient) Do(req *http.Request) (*http.Response, error) {
key := c.serializeReq(req)
respStr, err := c.cache.GetWithLoader(key, func(key string) (string, time.Duration, error) {
2021-02-07 19:26:05 -09:00
req, err := c.deserializeReq(key)
if err != nil {
return "", 0, err
2021-02-07 19:26:05 -09:00
}
resp, err := c.hc.Do(req)
if err != nil {
return "", 0, err
2021-02-07 19:26:05 -09:00
}
defer resp.Body.Close()
return c.serializeResponse(resp), c.ttl, nil
2021-02-07 19:26:05 -09:00
})
if err != nil {
return nil, err
}
return c.deserializeResponse(req, respStr)
2021-02-07 19:26:05 -09:00
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) serializeReq(req *http.Request) string {
2021-02-07 19:26:05 -09:00
data := requestData{
Method: req.Method,
Header: req.Header,
URL: req.URL.String(),
}
if req.Body != nil {
bodyData, _ := io.ReadAll(req.Body)
2021-02-07 19:26:05 -09:00
bodyStr := base64.StdEncoding.EncodeToString(bodyData)
data.Body = &bodyStr
}
j, _ := json.Marshal(&data)
return string(j)
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) deserializeReq(reqStr string) (*http.Request, error) {
2021-02-07 19:26:05 -09:00
var data requestData
_ = json.Unmarshal([]byte(reqStr), &data)
var body io.Reader
if data.Body != nil {
bodyStr, _ := base64.StdEncoding.DecodeString(*data.Body)
body = strings.NewReader(string(bodyStr))
}
2021-02-08 06:14:29 -09:00
req, err := http.NewRequest(data.Method, data.URL, body)
if err != nil {
return nil, err
}
req.Header = data.Header
return req, nil
2021-02-07 19:26:05 -09:00
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) serializeResponse(resp *http.Response) string {
2021-02-07 19:26:05 -09:00
var b = &bytes.Buffer{}
_ = resp.Write(b)
return b.String()
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) deserializeResponse(req *http.Request, respStr string) (*http.Response, error) {
2021-02-07 19:26:05 -09:00
r := bufio.NewReader(strings.NewReader(respStr))
return http.ReadResponse(r, req)
}