1
0
mirror of https://github.com/badkaktus/gorocket.git synced 2024-12-12 11:15:05 +02:00

Merge pull request #6

Add NewWithOptions, fix error, add version
This commit is contained in:
Nickolay 2022-09-27 13:07:42 +03:00 committed by GitHub
commit 3859deed80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 4 deletions

7
CHANGELOG.md Normal file
View File

@ -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`

View File

@ -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",

View File

@ -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()

2
go.mod
View File

@ -1,3 +1,3 @@
module github.com/fkrasnovid/gorocket
module github.com/badkaktus/gorocket
go 1.13

View File

@ -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)