Skip to content

Commit

Permalink
Merge pull request #24 from artemMur/add_ctx_support_to_get_updates
Browse files Browse the repository at this point in the history
Add context to client request
  • Loading branch information
DmitryDorofeev authored Nov 8, 2021
2 parents 52c21bd + 92519da commit 949689d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 11 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package botgolang

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -23,6 +24,10 @@ type Client struct {
}

func (c *Client) Do(path string, params url.Values, file *os.File) ([]byte, error) {
return c.DoWithContext(context.Background(), path, params, file)
}

func (c *Client) DoWithContext(ctx context.Context, path string, params url.Values, file *os.File) ([]byte, error) {
apiURL, err := url.Parse(c.baseURL + path)
params.Set("token", c.token)

Expand All @@ -31,7 +36,7 @@ func (c *Client) Do(path string, params url.Values, file *os.File) ([]byte, erro
}

apiURL.RawQuery = params.Encode()
req, err := http.NewRequest(http.MethodGet, apiURL.String(), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL.String(), nil)
if err != nil || req == nil {
return nil, fmt.Errorf("cannot init http request: %s", err)
}
Expand Down Expand Up @@ -558,13 +563,17 @@ func (c *Client) UploadVoice(message *Message) error {
}

func (c *Client) GetEvents(lastEventID int, pollTime int) ([]*Event, error) {
return c.GetEventsWithContext(context.Background(), lastEventID, pollTime)
}

func (c *Client) GetEventsWithContext(ctx context.Context, lastEventID int, pollTime int) ([]*Event, error) {
params := url.Values{
"lastEventId": {strconv.Itoa(lastEventID)},
"pollTime": {strconv.Itoa(pollTime)},
}
events := &eventsResponse{}

response, err := c.Do("/events/get", params, nil)
response, err := c.DoWithContext(ctx, "/events/get", params, nil)
if err != nil {
return events.Events, fmt.Errorf("error while making request: %s", err)
}
Expand Down
10 changes: 7 additions & 3 deletions updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (u *Updater) NewMessageFromPayload(message EventPayload) *Message {
}

func (u *Updater) RunUpdatesCheck(ctx context.Context, ch chan<- Event) {
_, err := u.GetLastEvents(0)
_, err := u.GetLastEventsWithContext(ctx, 0)
if err != nil {
u.logger.WithFields(logrus.Fields{
"err": err,
Expand All @@ -49,7 +49,7 @@ func (u *Updater) RunUpdatesCheck(ctx context.Context, ch chan<- Event) {
close(ch)
return
default:
events, err := u.GetLastEvents(u.PollTime)
events, err := u.GetLastEventsWithContext(ctx, u.PollTime)
if err != nil {
u.logger.WithFields(logrus.Fields{
"err": err,
Expand All @@ -71,7 +71,11 @@ func (u *Updater) RunUpdatesCheck(ctx context.Context, ch chan<- Event) {
}

func (u *Updater) GetLastEvents(pollTime int) ([]*Event, error) {
events, err := u.client.GetEvents(u.lastEventID, pollTime)
return u.GetLastEventsWithContext(context.Background(), pollTime)
}

func (u *Updater) GetLastEventsWithContext(ctx context.Context, pollTime int) ([]*Event, error) {
events, err := u.client.GetEventsWithContext(ctx, u.lastEventID, pollTime)
if err != nil {
u.logger.WithFields(logrus.Fields{
"err": err,
Expand Down

0 comments on commit 949689d

Please sign in to comment.