mirror of
https://github.com/badkaktus/gorocket.git
synced 2025-11-25 22:21:37 +02:00
add paginations
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module github.com/badkaktus/gorocket
|
module github.com/badkaktus/gorocket
|
||||||
|
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
|
require github.com/google/go-querystring v1.1.0
|
||||||
|
|||||||
55
gorocket.go
55
gorocket.go
@@ -3,6 +3,8 @@ package gorocket
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/google/go-querystring/query"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,7 +20,15 @@ type Client struct {
|
|||||||
timeout time.Duration
|
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 {
|
func NewClient(url string) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
//userID: user,
|
//userID: user,
|
||||||
@@ -31,7 +41,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 {
|
func NewWithOptions(url string, opts ...Option) *Client {
|
||||||
c := &Client{
|
c := &Client{
|
||||||
HTTPClient: &http.Client{
|
HTTPClient: &http.Client{
|
||||||
@@ -81,12 +91,14 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
|
|||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := c.HTTPClient.Do(req)
|
res, err := c.HTTPClient.Do(c.addQueryParams(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.cleanup()
|
||||||
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
resp := v
|
resp := v
|
||||||
@@ -96,3 +108,40 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
|
|||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user