Skip to content

Commit

Permalink
api: move verbose logging out
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Oct 4, 2022
1 parent cee9ad2 commit 4da4605
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
18 changes: 2 additions & 16 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
48 changes: 33 additions & 15 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 4da4605

Please sign in to comment.