diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..67118c6 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# CHANGELOG + +## [v0.1.1] - 2022-09-20 + +- Add semver version `v0.1.1` (see semver.org) +- Add `NewWithOptions(url string, opts ...Option)` for custom client settings +- Rollback `go.mod` package name to `github.com/badkaktus/gorocket` \ No newline at end of file diff --git a/README.md b/README.md index 5bf8622..9c0212f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,16 @@ if err != nil { fmt.Printf("I'm %s", lg.Data.Me.Username) ``` +or + +```go +client := gorocket.NewWithOptions("https://your-rocket-chat.com", + gorocket.WithUserID("my-user-id"), + gorocket.WithToken("my-bot-token"), + gorocket.WithTimeout(1 * time.Second), +) +``` + ## Manage user ```go str := gorocket.NewUser{ @@ -49,7 +59,7 @@ fmt.Printf("User was created %t", me.Success) ``` ## Post a message -```php +```go // create a new channel str := gorocket.CreateChannelRequest{ Name: "newchannel", diff --git a/chat.go b/chat.go index 4d71b44..fc655db 100644 --- a/chat.go +++ b/chat.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" "time" ) @@ -247,10 +248,10 @@ func (c *Client) GetPinnedMessages(param *GetPinnedMsgRequest) (*GetPinnedMsgRes url.Add("roomId", param.RoomId) } if param.Offset != 0 { - url.Add("offset", string(param.Offset)) + url.Add("offset", strconv.Itoa(param.Offset)) } if param.Count != 0 { - url.Add("count", string(param.Count)) + url.Add("count", strconv.Itoa(param.Count)) } req.URL.RawQuery = url.Encode() diff --git a/go.mod b/go.mod index 2e39668..5a5baf1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/fkrasnovid/gorocket +module github.com/badkaktus/gorocket go 1.13 diff --git a/gorocket.go b/gorocket.go index a4f3720..11ff0c9 100644 --- a/gorocket.go +++ b/gorocket.go @@ -1,6 +1,7 @@ package gorocket import ( + "context" "encoding/json" "log" "net/http" @@ -13,6 +14,8 @@ type Client struct { xToken string apiVersion string HTTPClient *http.Client + + timeout time.Duration } // NewClient creates new Facest.io client with given API key @@ -28,12 +31,56 @@ func NewClient(url string) *Client { } } +// NewClient creates new Facest.io client with given API key +func NewWithOptions(url string, opts ...Option) *Client { + c := &Client{ + HTTPClient: &http.Client{ + Timeout: 5 * time.Minute, + }, + baseURL: url, + apiVersion: "api/v1", + } + + for _, o := range opts { + o(c) + } + + return c +} + +type Option func(*Client) + +func WithTimeout(d time.Duration) Option { + return func(c *Client) { + c.timeout = d + } +} + +func WithUserID(userID string) Option { + return func(c *Client) { + c.userID = userID + } +} + +func WithXToken(xtoken string) Option { + return func(c *Client) { + c.xToken = xtoken + } +} + func (c *Client) sendRequest(req *http.Request, v interface{}) error { req.Header.Set("Accept", "application/json; charset=utf-8") req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Add("X-Auth-Token", c.xToken) req.Header.Add("X-User-Id", c.userID) + if c.timeout > 0 { + ctx, cancel := context.WithTimeout(req.Context(), c.timeout) + defer cancel() + + req = req.WithContext(ctx) + } + res, err := c.HTTPClient.Do(req) if err != nil { log.Println(err)