🏗️ Use fasthttp.URI instead url.URL
This commit is contained in:
parent
3623237602
commit
0ed078f083
12
request.go
12
request.go
|
@ -2,21 +2,25 @@ package telegram
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
json "github.com/pquerna/ffjson/ffjson"
|
||||
http "github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var defaultClient = new(http.Client)
|
||||
|
||||
func (bot *Bot) request(dst []byte, method string) (response *Response, err error) {
|
||||
if bot.Client == nil {
|
||||
bot.SetClient(defaultClient)
|
||||
}
|
||||
|
||||
requestURI := defaultURI
|
||||
requestURI.Path = path.Join("bot"+bot.AccessToken, method)
|
||||
requestURI := http.AcquireURI()
|
||||
requestURI.SetScheme("https")
|
||||
requestURI.SetHost("api.telegram.org")
|
||||
requestURI.SetPath(path.Join("bot"+bot.AccessToken, method))
|
||||
|
||||
req := http.AcquireRequest()
|
||||
defer http.ReleaseRequest(req)
|
||||
|
@ -27,7 +31,7 @@ func (bot *Bot) request(dst []byte, method string) (response *Response, err erro
|
|||
}
|
||||
req.Header.SetRequestURI(requestURI.String())
|
||||
req.Header.SetUserAgent(path.Join("telegram", strconv.FormatInt(Version, 10)))
|
||||
req.Header.SetHost(requestURI.Hostname())
|
||||
req.Header.SetHostBytes(requestURI.Host())
|
||||
req.SetBody(dst)
|
||||
|
||||
resp := http.AcquireResponse()
|
||||
|
|
|
@ -60,8 +60,10 @@ func (bot *Bot) Upload(method, key, name string, file InputFile, args fmt.String
|
|||
buffer := bytes.NewBuffer(nil)
|
||||
multi := multipart.NewWriter(buffer)
|
||||
|
||||
requestURI := defaultURI
|
||||
requestURI.Path = path.Join("bot"+bot.AccessToken, method)
|
||||
requestURI := http.AcquireURI()
|
||||
requestURI.SetScheme("https")
|
||||
requestURI.SetHost("api.telegram.org")
|
||||
requestURI.SetPath(path.Join("bot"+bot.AccessToken, method))
|
||||
|
||||
query, err := url.ParseQuery(args.String())
|
||||
if err != nil {
|
||||
|
@ -89,7 +91,7 @@ func (bot *Bot) Upload(method, key, name string, file InputFile, args fmt.String
|
|||
req.Header.SetMethod("POST")
|
||||
req.Header.SetRequestURI(requestURI.String())
|
||||
req.Header.SetUserAgent(path.Join("telegram", strconv.FormatInt(Version, 10)))
|
||||
req.Header.SetHost(requestURI.Hostname())
|
||||
req.Header.SetHostBytes(requestURI.Host())
|
||||
|
||||
log.Ln("Request:")
|
||||
log.D(req)
|
||||
|
|
28
utils.go
28
utils.go
|
@ -1,8 +1,7 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
http "github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// NewForceReply calls the response interface to the message.
|
||||
|
@ -11,15 +10,14 @@ func NewForceReply() *ForceReply {
|
|||
}
|
||||
|
||||
// NewInlineMentionURL creates a url.URL for the mention user without username.
|
||||
func NewInlineMentionURL(userID int) *url.URL {
|
||||
link := &url.URL{
|
||||
Scheme: SchemeTelegram,
|
||||
Path: "user",
|
||||
}
|
||||
func NewInlineMentionURL(userID int) *http.URI {
|
||||
link := http.AcquireURI()
|
||||
link.SetScheme(SchemeTelegram)
|
||||
link.SetPath("user")
|
||||
|
||||
q := link.Query()
|
||||
q.Add("id", strconv.Itoa(userID))
|
||||
link.RawQuery = q.Encode()
|
||||
q := link.QueryArgs()
|
||||
q.SetUint("id", userID)
|
||||
link.SetQueryStringBytes(q.QueryString())
|
||||
|
||||
return link
|
||||
}
|
||||
|
@ -32,13 +30,12 @@ func NewMarkdownItalic(text string) string {
|
|||
return "_" + text + "_"
|
||||
}
|
||||
|
||||
func NewMarkdownURL(text string, link *url.URL) string {
|
||||
func NewMarkdownURL(text string, link *http.URI) string {
|
||||
return "[" + text + "](" + link.String() + ")"
|
||||
}
|
||||
|
||||
func NewMarkdownMention(text string, id int) string {
|
||||
link := NewInlineMentionURL(id)
|
||||
return NewMarkdownURL(text, link)
|
||||
return NewMarkdownURL(text, NewInlineMentionURL(id))
|
||||
}
|
||||
|
||||
func NewMarkdownCode(text string) string {
|
||||
|
@ -57,13 +54,12 @@ func NewHtmlItalic(text string) string {
|
|||
return "<i>" + text + "</i>"
|
||||
}
|
||||
|
||||
func NewHtmlURL(text string, link *url.URL) string {
|
||||
func NewHtmlURL(text string, link *http.URI) string {
|
||||
return `<a href="` + link.String() + `">` + text + `</a>`
|
||||
}
|
||||
|
||||
func NewHtmlMention(text string, id int) string {
|
||||
link := NewInlineMentionURL(id)
|
||||
return NewHtmlURL(text, link)
|
||||
return NewHtmlURL(text, NewInlineMentionURL(id))
|
||||
}
|
||||
|
||||
func NewHtmlCode(text string) string {
|
||||
|
|
|
@ -126,14 +126,16 @@ func (b *Bot) IsMessageToMe(m *Message) bool {
|
|||
}
|
||||
|
||||
// NewFileURL creates a url.URL to file with path getted from GetFile method.
|
||||
func (b *Bot) NewFileURL(filePath string) *url.URL {
|
||||
func (b *Bot) NewFileURL(filePath string) *http.URI {
|
||||
if b == nil || b.AccessToken == "" ||
|
||||
filePath == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := defaultURI
|
||||
result.Path = path.Join("file", "bot"+b.AccessToken, filePath)
|
||||
result := http.AcquireURI()
|
||||
result.SetScheme("https")
|
||||
result.SetHost("api.telegram.org")
|
||||
result.SetPath(path.Join("file", "bot"+b.AccessToken, filePath))
|
||||
|
||||
return result
|
||||
}
|
||||
|
|
15
variables.go
15
variables.go
|
@ -1,15 +0,0 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
http "github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultClient = new(http.Client)
|
||||
defaultURI = &url.URL{
|
||||
Scheme: "https",
|
||||
Host: "api.telegram.org",
|
||||
}
|
||||
)
|
Loading…
Reference in New Issue