From bba31eee24446126be8e6380097052b24972c24a Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Mon, 1 Oct 2018 18:17:03 +0300 Subject: [PATCH] channels list method --- v1/client.go | 35 +++++++++++++++++++++++++++++++++++ v1/client_test.go | 12 ++++++++++++ v1/types.go | 25 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/v1/client.go b/v1/client.go index 6e94b0d..746448b 100644 --- a/v1/client.go +++ b/v1/client.go @@ -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: diff --git a/v1/client_test.go b/v1/client_test.go index 0a52dc1..e74418f 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -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{ diff --git a/v1/types.go b/v1/types.go index 3062177..5558afa 100644 --- a/v1/types.go +++ b/v1/types.go @@ -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"`