api: move verbose logging out

pull/558/head
Demian 2 years ago
parent cee9ad2ddd
commit 4da4605bec

@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
@ -42,7 +41,7 @@ func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
}
}()
req, err := http.NewRequestWithContext(ctx, "POST", url, &buf)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &buf)
if err != nil {
return nil, wrapError(err)
}
@ -61,20 +60,7 @@ func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
}
if b.verbose {
body, _ := json.Marshal(payload)
body = bytes.ReplaceAll(body, []byte(`\"`), []byte(`"`))
body = bytes.ReplaceAll(body, []byte(`"{`), []byte(`{`))
body = bytes.ReplaceAll(body, []byte(`}"`), []byte(`}`))
indent := func(b []byte) string {
buf.Reset()
json.Indent(&buf, b, "", "\t")
return buf.String()
}
log.Printf("[verbose] telebot: sent request\n"+
"Method: %v\nParams: %v\nResponse: %v",
method, indent(body), indent(data))
verbose(method, payload, data)
}
// returning data as well

@ -23,6 +23,24 @@ func (b *Bot) debug(err error) {
}
}
func verbose(method string, payload interface{}, data []byte) {
body, _ := json.Marshal(payload)
body = bytes.ReplaceAll(body, []byte(`\"`), []byte(`"`))
body = bytes.ReplaceAll(body, []byte(`"{`), []byte(`{`))
body = bytes.ReplaceAll(body, []byte(`}"`), []byte(`}`))
indent := func(b []byte) string {
var buf bytes.Buffer
json.Indent(&buf, b, "", " ")
return buf.String()
}
log.Printf(
"[verbose] telebot: sent request\nMethod: %v\nParams: %v\nResponse: %v",
method, indent(body), indent(data),
)
}
func (b *Bot) runHandler(h HandlerFunc, c Context) {
f := func() {
if err := h(c); err != nil {
@ -172,6 +190,21 @@ func extractOptions(how []interface{}) *SendOptions {
return opts
}
// extractCommandsParams extracts parameters for commands-related methods from the given options.
func extractCommandsParams(opts ...interface{}) (params CommandParams) {
for _, opt := range opts {
switch value := opt.(type) {
case []Command:
params.Commands = value
case string:
params.LanguageCode = value
case CommandScope:
params.Scope = &value
}
}
return
}
func (b *Bot) embedSendOptions(params map[string]string, opt *SendOptions) {
if b.parseMode != ModeDefault {
params["parse_mode"] = b.parseMode
@ -271,18 +304,3 @@ func intsToStrs(ns []int) (s []string) {
}
return
}
// extractCommandsParams extracts parameters for commands-related methods from the given options.
func extractCommandsParams(opts ...interface{}) (params CommandParams) {
for _, opt := range opts {
switch value := opt.(type) {
case []Command:
params.Commands = value
case string:
params.LanguageCode = value
case CommandScope:
params.Scope = &value
}
}
return
}

Loading…
Cancel
Save