summaryrefslogtreecommitdiff
path: root/golang/pkg/api/api.go
blob: 662bb87b97d46bc54b358bf31301fd1d10be5f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package api

import (
	"bytes"
	"encoding/json"
	"io"
	"log/slog"
	"net/http"
	"time"
)

type Error[T any] struct {
	Code    int    `json:"code"`
	Data    T      `json:"data"`
	Message string `json:"message"`
}

type APIMessage[T any, E any] struct {
	Data  T        `json:"data"`
	Error Error[E] `json:"error"`
	//meta
}

type Response struct {
	Response []byte
	Err      error
}

func Send[T any](c *Client, method, path string, payload any) (message APIMessage[T, any], err error) {
	resp := make(chan *Response)
	c.channel <- &Request{
		method:   method,
		path:     path,
		payload:  payload,
		priority: 10,
		resp:     resp,
	}
	res := <-resp
	if res.Err != nil {
		return message, res.Err
	}
	err = json.Unmarshal(res.Response, &message)
	return message, err
}

func (c *Client) sendOne(method, path string, payload any) (body []byte, err error) {
	slog.Debug("Request", "method", method, "path", path, "payload", payload)
	var req *http.Request
	if payload != nil {
		body, err = json.Marshal(payload)
		if err == nil {
			req, err = http.NewRequest(method, c.baseURL+path, bytes.NewBuffer(body))
		} else {
			return nil, err
		}
	} else {
		req, err = http.NewRequest(method, c.baseURL+path, nil)
	}
	if err != nil {
		return nil, err
	}
	req.Header = *c.headers
	req = req.WithContext(c.ctx)

	resp, err := c.httpClient.Do(req)
	if err != nil {
		slog.Error("sendOne Do", "method", method, "path", path, "error", err)
		return nil, err
	}
	defer func() {
		if e := resp.Body.Close(); err == nil {
			err = e
		}
	}()
	if body, err = io.ReadAll(resp.Body); err != nil {
		slog.Error("sendOne ReadAll", "method", method, "path", path, "error", err)
		return nil, err
	}
	slog.Debug("Response", "body", string(body))
	switch resp.StatusCode {
	case 429:
		e := decode429(body)
		time.Sleep(time.Duration(e.Error.Data.RetryAfter * float64(time.Second)))
		return c.sendOne(method, path, payload)
	}
	return body, nil
}