140 lines
3.3 KiB
Go
140 lines
3.3 KiB
Go
package tgapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.nix13.pw/scuroneko/laniakea/utils"
|
|
"git.nix13.pw/scuroneko/slog"
|
|
)
|
|
|
|
type APIOpts struct {
|
|
token string
|
|
client *http.Client
|
|
useTestServer bool
|
|
apiUrl string
|
|
}
|
|
|
|
func NewAPIOpts(token string) *APIOpts {
|
|
return &APIOpts{token: token, client: nil, useTestServer: false, apiUrl: "https://api.telegram.org"}
|
|
}
|
|
func (opts *APIOpts) SetHTTPClient(client *http.Client) *APIOpts {
|
|
if client != nil {
|
|
opts.client = client
|
|
}
|
|
return opts
|
|
}
|
|
func (opts *APIOpts) UseTestServer(use bool) *APIOpts {
|
|
opts.useTestServer = use
|
|
return opts
|
|
}
|
|
func (opts *APIOpts) SetAPIUrl(apiUrl string) *APIOpts {
|
|
if apiUrl != "" {
|
|
opts.apiUrl = apiUrl
|
|
}
|
|
return opts
|
|
}
|
|
|
|
type API struct {
|
|
token string
|
|
client *http.Client
|
|
Logger *slog.Logger
|
|
useTestServer bool
|
|
apiUrl string
|
|
}
|
|
|
|
func NewAPI(opts *APIOpts) *API {
|
|
l := slog.CreateLogger().Level(utils.GetLoggerLevel()).Prefix("API")
|
|
l.AddWriter(l.CreateJsonStdoutWriter())
|
|
client := opts.client
|
|
if client == nil {
|
|
client = &http.Client{Timeout: time.Second * 45}
|
|
}
|
|
return &API{opts.token, client, l, opts.useTestServer, opts.apiUrl}
|
|
}
|
|
func (api *API) CloseApi() error {
|
|
return api.Logger.Close()
|
|
}
|
|
|
|
type ApiResponse[R any] struct {
|
|
Ok bool `json:"ok"`
|
|
Description string `json:"description,omitempty"`
|
|
Result R `json:"result,omitempty"`
|
|
ErrorCode int `json:"error_code,omitempty"`
|
|
}
|
|
|
|
type TelegramRequest[R, P any] struct {
|
|
method string
|
|
params P
|
|
}
|
|
|
|
func NewRequest[R, P any](method string, params P) TelegramRequest[R, P] {
|
|
return TelegramRequest[R, P]{method: method, params: params}
|
|
}
|
|
func (r TelegramRequest[R, P]) DoWithContext(ctx context.Context, api *API) (R, error) {
|
|
var zero R
|
|
data, err := json.Marshal(r.params)
|
|
if err != nil {
|
|
return zero, err
|
|
}
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
methodPrefix := ""
|
|
if api.useTestServer {
|
|
methodPrefix = "/test"
|
|
}
|
|
url := fmt.Sprintf("%s/bot%s%s/%s", api.apiUrl, api.token, methodPrefix, r.method)
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, buf)
|
|
if err != nil {
|
|
return zero, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("User-Agent", fmt.Sprintf("Laniakea/%s", utils.VersionString))
|
|
|
|
api.Logger.Debugln("REQ", api.apiUrl, r.method, buf.String())
|
|
res, err := api.client.Do(req)
|
|
if err != nil {
|
|
return zero, err
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
_ = Body.Close()
|
|
}(res.Body)
|
|
|
|
data, err = readBody(res.Body)
|
|
if err != nil {
|
|
return zero, err
|
|
}
|
|
api.Logger.Debugln("RES", r.method, string(data))
|
|
if res.StatusCode != http.StatusOK {
|
|
return zero, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, string(data))
|
|
}
|
|
return parseBody[R](data)
|
|
|
|
}
|
|
func (r TelegramRequest[R, P]) Do(api *API) (R, error) {
|
|
return r.DoWithContext(context.Background(), api)
|
|
}
|
|
|
|
func readBody(body io.ReadCloser) ([]byte, error) {
|
|
reader := io.LimitReader(body, 10<<20)
|
|
return io.ReadAll(reader)
|
|
}
|
|
func parseBody[R any](data []byte) (R, error) {
|
|
var zero R
|
|
var resp ApiResponse[R]
|
|
err := json.Unmarshal(data, &resp)
|
|
if err != nil {
|
|
return zero, err
|
|
}
|
|
if !resp.Ok {
|
|
return zero, fmt.Errorf("[%d] %s", resp.ErrorCode, resp.Description)
|
|
}
|
|
return resp.Result, nil
|
|
}
|