mirror of
https://github.com/badkaktus/gorocket.git
synced 2024-12-12 11:15:05 +02:00
commit
be903cde13
@ -82,5 +82,14 @@ if err != nil {
|
||||
}
|
||||
fmt.Printf("Message was posted %t", msg.Success)
|
||||
```
|
||||
|
||||
## Pagination
|
||||
If endpoint support pagination, you can use that like this:
|
||||
```go
|
||||
// sort field in map. 1 - asc, -1 - desc
|
||||
srt := map[string]int{"_updatedAt": 1, "name": -1}
|
||||
|
||||
client.Count(10).Offset(10).Sort(srt).ChannelList()
|
||||
```
|
||||
## PS
|
||||
Feel free to create issue for add new endpoint to this client
|
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
||||
module github.com/badkaktus/gorocket
|
||||
|
||||
go 1.13
|
||||
|
||||
require github.com/google/go-querystring v1.1.0
|
||||
|
54
gorocket.go
54
gorocket.go
@ -3,6 +3,7 @@ package gorocket
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/google/go-querystring/query"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
@ -18,7 +19,15 @@ type Client struct {
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
// NewClient creates new Facest.io client with given API key
|
||||
type PaginationStruct struct {
|
||||
Count int `url:"count,omitempty"`
|
||||
Offset int `url:"offset,omitempty"`
|
||||
Sort string `url:"sort,omitempty"`
|
||||
}
|
||||
|
||||
var pagination PaginationStruct
|
||||
|
||||
// NewClient creates new rocket.chat client with given API key
|
||||
func NewClient(url string) *Client {
|
||||
return &Client{
|
||||
//userID: user,
|
||||
@ -31,7 +40,7 @@ func NewClient(url string) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
// NewClient creates new Facest.io client with given API key
|
||||
// NewWithOptions creates new rocket.chat client with options
|
||||
func NewWithOptions(url string, opts ...Option) *Client {
|
||||
c := &Client{
|
||||
HTTPClient: &http.Client{
|
||||
@ -81,12 +90,14 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
|
||||
req = req.WithContext(ctx)
|
||||
}
|
||||
|
||||
res, err := c.HTTPClient.Do(req)
|
||||
res, err := c.HTTPClient.Do(c.addQueryParams(req))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
c.cleanup()
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
resp := v
|
||||
@ -96,3 +107,40 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Count(val int) *Client {
|
||||
pagination.Count = val
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Offset(val int) *Client {
|
||||
pagination.Offset = val
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Sort(val map[string]int) *Client {
|
||||
byteJson, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
log.Printf("cant create sort. error: %s", err)
|
||||
return c
|
||||
}
|
||||
pagination.Sort = string(byteJson)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) addQueryParams(req *http.Request) *http.Request {
|
||||
v, err := query.Values(pagination)
|
||||
if err != nil {
|
||||
log.Printf("error create query string: %s", err)
|
||||
return req
|
||||
}
|
||||
req.URL.RawQuery = v.Encode()
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) cleanup() {
|
||||
pagination.Sort = ""
|
||||
pagination.Offset = 0
|
||||
pagination.Count = 0
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user