1
0
mirror of synced 2024-11-25 06:26:03 +03:00

Merge pull request #30 from gwinn/master

channels list method
This commit is contained in:
Alex Lushpai 2018-10-01 18:22:05 +03:00 committed by GitHub
commit 63a8948267
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 0 deletions

View File

@ -7,6 +7,8 @@ import (
"net/http"
"strconv"
"time"
"github.com/google/go-querystring/query"
)
// New initialize client
@ -18,6 +20,39 @@ func New(url string, token string) *MgClient {
}
}
// TransportChannels returns channels list
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
//
// data, status, err := client.TransportChannels{Channels{Active: true}}
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("Status: %v, Channels found: %v", status, len(data))
func (c *MgClient) TransportChannels(request Channels) ([]ChannelListItem, int, error) {
var resp []ChannelListItem
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/channels?%s", outgoing.Encode()))
if err != nil {
return resp, status, err
}
if err := json.Unmarshal(data, &resp); err != nil {
return resp, status, err
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// ActivateTransportChannel implement channel activation
//
// Example:

View File

@ -19,6 +19,18 @@ func client() *MgClient {
return New(mgURL, mgToken)
}
func TestMgClient_TransportChannels(t *testing.T) {
c := client()
data, status, err := c.TransportChannels(Channels{Active: true})
if err != nil {
t.Errorf("%d %v", status, err)
}
t.Logf("Channels found: %v", len(data))
}
func TestMgClient_ActivateTransportChannel(t *testing.T) {
c := client()
ch := Channel{

View File

@ -105,6 +105,31 @@ type DeleteResponse struct {
DectivatedAt time.Time `json:"deactivated_at"`
}
// ChannelListItem response struct
type ChannelListItem struct {
ID uint64 `json:"id"`
Type string `json:"type"`
Name *string `json:"name"`
Settings ChannelSettings `json:"settings"`
CreatedAt string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
ActivatedAt string `json:"activated_at"`
DeactivatedAt *string `json:"deactivated_at"`
IsActive bool `json:"is_active"`
}
// Channels request type
type Channels struct {
ID int `json:"id,omitempty"`
Types []string `json:"types,omitempty"`
Active bool `json:"active,omitempty"`
Since time.Time `json:"since,omitempty"`
Until time.Time `json:"until,omitempty"`
TransportID uint64 `json:"transport_id,omitempty"`
Sort string `json:"sort,omitempty"`
Limit int `json:"limit,omitempty"`
}
// User struct
type User struct {
ExternalID string `json:"external_id"`