api: add push stop command
This commit is contained in:
parent
14d35a9170
commit
81c0ed4793
|
@ -1,18 +1,33 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Request to API and unmarshal result
|
// Request to API and unmarshal result
|
||||||
func (c *Client) Request(url string, value interface{}) error {
|
func (c *Client) Request(method, url string, body, value interface{}) error {
|
||||||
netClient := &http.Client{
|
netClient := &http.Client{
|
||||||
Timeout: time.Second * 20,
|
Timeout: time.Second * 20,
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(http.MethodGet, c.URL+url, nil)
|
var jsonBody io.Reader
|
||||||
|
if body != nil {
|
||||||
|
if strBody, ok := body.(string); ok {
|
||||||
|
jsonBody = strings.NewReader(strBody)
|
||||||
|
} else {
|
||||||
|
jsonBodyArray, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
jsonBody = bytes.NewBuffer(jsonBodyArray)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(method, c.URL+url, jsonBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -20,7 +21,7 @@ type ResponseList struct {
|
||||||
func (c *Client) RequestListVHosts() (*ResponseList, error) {
|
func (c *Client) RequestListVHosts() (*ResponseList, error) {
|
||||||
req := ResponseList{}
|
req := ResponseList{}
|
||||||
url := fmt.Sprintf(URLRequestListVHost)
|
url := fmt.Sprintf(URLRequestListVHost)
|
||||||
if err := c.Request(url, &req); err != nil {
|
if err := c.Request(http.MethodGet, url, nil, &req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &req, nil
|
return &req, nil
|
||||||
|
@ -30,7 +31,7 @@ func (c *Client) RequestListVHosts() (*ResponseList, error) {
|
||||||
func (c *Client) RequestListApps(vhost string) (*ResponseList, error) {
|
func (c *Client) RequestListApps(vhost string) (*ResponseList, error) {
|
||||||
req := ResponseList{}
|
req := ResponseList{}
|
||||||
url := fmt.Sprintf(URLRequestListApp, vhost)
|
url := fmt.Sprintf(URLRequestListApp, vhost)
|
||||||
if err := c.Request(url, &req); err != nil {
|
if err := c.Request(http.MethodGet, url, nil, &req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &req, nil
|
return &req, nil
|
||||||
|
@ -45,7 +46,7 @@ func (c *Client) RequestDefaultListApps() (*ResponseList, error) {
|
||||||
func (c *Client) RequestListStreams(vhost, app string) (*ResponseList, error) {
|
func (c *Client) RequestListStreams(vhost, app string) (*ResponseList, error) {
|
||||||
req := ResponseList{}
|
req := ResponseList{}
|
||||||
url := fmt.Sprintf(URLRequestListStream, vhost, app)
|
url := fmt.Sprintf(URLRequestListStream, vhost, app)
|
||||||
if err := c.Request(url, &req); err != nil {
|
if err := c.Request(http.MethodGet, url, nil, &req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &req, nil
|
return &req, nil
|
||||||
|
|
|
@ -2,11 +2,13 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// API URLS for Push
|
// API URLS for Push
|
||||||
const (
|
const (
|
||||||
URLRequestPushStatus = "/v1/vhosts/%s/apps/%s:pushes"
|
URLRequestPushStatus = "/v1/vhosts/%s/apps/%s:pushes"
|
||||||
|
URLRequestPushStop = "/v1/vhosts/%s/apps/%s:stopPush"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ResponsePushStatus JSON Message with Push status data
|
// ResponsePushStatus JSON Message with Push status data
|
||||||
|
@ -49,7 +51,7 @@ type ResponsePushDataStream struct {
|
||||||
func (c *Client) RequestPushStatus(vhost, app string) (*ResponsePushStatus, error) {
|
func (c *Client) RequestPushStatus(vhost, app string) (*ResponsePushStatus, error) {
|
||||||
req := ResponsePushStatus{}
|
req := ResponsePushStatus{}
|
||||||
url := fmt.Sprintf(URLRequestPushStatus, vhost, app)
|
url := fmt.Sprintf(URLRequestPushStatus, vhost, app)
|
||||||
if err := c.Request(url, &req); err != nil {
|
if err := c.Request(http.MethodGet, url, nil, &req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &req, nil
|
return &req, nil
|
||||||
|
@ -59,3 +61,21 @@ func (c *Client) RequestPushStatus(vhost, app string) (*ResponsePushStatus, erro
|
||||||
func (c *Client) RequestPushStatusDefault() (*ResponsePushStatus, error) {
|
func (c *Client) RequestPushStatusDefault() (*ResponsePushStatus, error) {
|
||||||
return c.RequestPushStatus(c.DefaultVHost, c.DefaultApp)
|
return c.RequestPushStatus(c.DefaultVHost, c.DefaultApp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeletePush to delete an push
|
||||||
|
func (c *Client) DeletePush(vhost, app, id string) error {
|
||||||
|
type idJSON struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(URLRequestPushStop, vhost, app)
|
||||||
|
data := idJSON{ID: id}
|
||||||
|
if err := c.Request(http.MethodPost, url, &data, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeletePushDefault to delete an push and on default vhost and app
|
||||||
|
func (c *Client) DeletePushDefault(id string) error {
|
||||||
|
return c.DeletePush(c.DefaultVHost, c.DefaultApp, id)
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -34,7 +35,7 @@ type ResponseStatsData struct {
|
||||||
func (c *Client) RequestStatsVHost(vhost string) (*ResponseStats, error) {
|
func (c *Client) RequestStatsVHost(vhost string) (*ResponseStats, error) {
|
||||||
req := ResponseStats{}
|
req := ResponseStats{}
|
||||||
url := fmt.Sprintf(URLRequestStatsVHost, vhost)
|
url := fmt.Sprintf(URLRequestStatsVHost, vhost)
|
||||||
if err := c.Request(url, &req); err != nil {
|
if err := c.Request(http.MethodGet, url, nil, &req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &req, nil
|
return &req, nil
|
||||||
|
@ -43,7 +44,7 @@ func (c *Client) RequestStatsVHost(vhost string) (*ResponseStats, error) {
|
||||||
func (c *Client) RequestStatsApp(vhost, app string) (*ResponseStats, error) {
|
func (c *Client) RequestStatsApp(vhost, app string) (*ResponseStats, error) {
|
||||||
req := ResponseStats{}
|
req := ResponseStats{}
|
||||||
url := fmt.Sprintf(URLRequestStatsApp, vhost, app)
|
url := fmt.Sprintf(URLRequestStatsApp, vhost, app)
|
||||||
if err := c.Request(url, &req); err != nil {
|
if err := c.Request(http.MethodGet, url, nil, &req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &req, nil
|
return &req, nil
|
||||||
|
@ -52,7 +53,7 @@ func (c *Client) RequestStatsApp(vhost, app string) (*ResponseStats, error) {
|
||||||
func (c *Client) RequestStatsStream(vhost, app, stream string) (*ResponseStats, error) {
|
func (c *Client) RequestStatsStream(vhost, app, stream string) (*ResponseStats, error) {
|
||||||
req := ResponseStats{}
|
req := ResponseStats{}
|
||||||
url := fmt.Sprintf(URLRequestStatsStream, vhost, app, stream)
|
url := fmt.Sprintf(URLRequestStatsStream, vhost, app, stream)
|
||||||
if err := c.Request(url, &req); err != nil {
|
if err := c.Request(http.MethodGet, url, nil, &req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &req, nil
|
return &req, nil
|
||||||
|
|
Loading…
Reference in New Issue