mirror of
https://github.com/badkaktus/gorocket.git
synced 2024-12-12 11:15:05 +02:00
add paginations
This commit is contained in:
parent
3859deed80
commit
0e7082dccc
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
|
||||
|
55
gorocket.go
55
gorocket.go
@ -3,6 +3,8 @@ package gorocket
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/go-querystring/query"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
@ -18,7 +20,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 +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 {
|
||||
c := &Client{
|
||||
HTTPClient: &http.Client{
|
||||
@ -81,12 +91,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 +108,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