Skip to content

Commit

Permalink
Merge pull request #6
Browse files Browse the repository at this point in the history
Add NewWithOptions, fix error, add version
  • Loading branch information
badkaktus authored Sep 27, 2022
2 parents 521ed9a + 795b086 commit 3859dee
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)

Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/fkrasnovid/gorocket
module github.com/badkaktus/gorocket

go 1.13
47 changes: 47 additions & 0 deletions gorocket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gorocket

import (
"context"
"encoding/json"
"log"
"net/http"
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 3859dee

Please sign in to comment.