new request syntax

This commit is contained in:
2026-01-29 10:50:43 +03:00
parent f1bb4b62c1
commit f0483564f2
2 changed files with 47 additions and 127 deletions

78
api.go
View File

@@ -3,20 +3,13 @@ package laniakea
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
type ApiResponse struct {
Ok bool `json:"ok"`
Description string `json:"description,omitempty"`
Result any `json:"result,omitempty"`
ErrorCode int `json:"error_code,omitempty"`
}
type ApiResponseG[R any] struct {
type ApiResponse[R any] struct {
Ok bool `json:"ok"`
Description string `json:"description,omitempty"`
Result R `json:"result,omitempty"`
@@ -27,13 +20,9 @@ type TelegramRequest[R, P any] struct {
method string
params P
}
type EmptyParams struct{}
func NewRequest[R, P any](method string, params P) TelegramRequest[R, P] {
return TelegramRequest[R, P]{
method: method,
params: params,
}
return TelegramRequest[R, P]{method: method, params: params}
}
func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) {
var buf bytes.Buffer
@@ -42,6 +31,13 @@ func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) {
return nil, err
}
if bot.requestLogger != nil {
bot.requestLogger.Debugln(strings.ReplaceAll(fmt.Sprintf(
"POST https://api.telegram.org/bot%s/%s %s",
"<TOKEN>", r.method, buf.String(),
), "\n", ""))
}
req, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/%s", bot.token, r.method), "application/json", &buf)
if err != nil {
return nil, err
@@ -52,7 +48,11 @@ func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) {
return nil, err
}
response := new(ApiResponseG[R])
if bot.requestLogger != nil {
bot.requestLogger.Debugln(fmt.Sprintf("RES %s %s", r.method, string(data)))
}
response := new(ApiResponse[R])
err = json.Unmarshal(data, &response)
if err != nil {
return nil, err
@@ -63,53 +63,3 @@ func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) {
}
return &response.Result, nil
}
// request is a low-level call to api.
func (b *Bot) request(methodName string, params any) (map[string]any, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(params)
if err != nil {
return nil, err
}
if b.debug && b.requestLogger != nil {
b.requestLogger.Debugln(strings.ReplaceAll(fmt.Sprintf(
"POST https://api.telegram.org/bot%s/%s %s",
"<TOKEN>",
methodName,
buf.String(),
), "\n", ""))
}
r, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/%s", b.token, methodName), "application/json", &buf)
if err != nil {
return nil, err
}
defer r.Body.Close()
data, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
b.requestLogger.Debugln(fmt.Sprintf("RES %s %s", methodName, string(data)))
response := new(ApiResponse)
err = json.Unmarshal(data, &response)
if err != nil {
return nil, err
}
if !response.Ok {
return nil, fmt.Errorf("[%d] %s", response.ErrorCode, response.Description)
}
if res, ok := response.Result.(bool); ok {
return map[string]any{
"data": res,
}, nil
} else if res, ok := response.Result.([]any); ok {
return map[string]any{
"data": res,
}, nil
} else if res, ok := response.Result.(map[string]any); ok {
return res, nil
}
return map[string]any{}, errors.New("can't parse response")
}