api: add push stop command

This commit is contained in:
Geno 2021-08-22 15:26:13 +02:00
parent 14d35a9170
commit 81c0ed4793
4 changed files with 46 additions and 9 deletions

View File

@ -1,18 +1,33 @@
package api
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// 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{
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 {
return err
}

View File

@ -2,6 +2,7 @@ package api
import (
"fmt"
"net/http"
)
const (
@ -20,7 +21,7 @@ type ResponseList struct {
func (c *Client) RequestListVHosts() (*ResponseList, error) {
req := ResponseList{}
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 &req, nil
@ -30,7 +31,7 @@ func (c *Client) RequestListVHosts() (*ResponseList, error) {
func (c *Client) RequestListApps(vhost string) (*ResponseList, error) {
req := ResponseList{}
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 &req, nil
@ -45,7 +46,7 @@ func (c *Client) RequestDefaultListApps() (*ResponseList, error) {
func (c *Client) RequestListStreams(vhost, app string) (*ResponseList, error) {
req := ResponseList{}
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 &req, nil

View File

@ -2,11 +2,13 @@ package api
import (
"fmt"
"net/http"
)
// API URLS for Push
const (
URLRequestPushStatus = "/v1/vhosts/%s/apps/%s:pushes"
URLRequestPushStop = "/v1/vhosts/%s/apps/%s:stopPush"
)
// ResponsePushStatus JSON Message with Push status data
@ -49,7 +51,7 @@ type ResponsePushDataStream struct {
func (c *Client) RequestPushStatus(vhost, app string) (*ResponsePushStatus, error) {
req := ResponsePushStatus{}
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 &req, nil
@ -59,3 +61,21 @@ func (c *Client) RequestPushStatus(vhost, app string) (*ResponsePushStatus, erro
func (c *Client) RequestPushStatusDefault() (*ResponsePushStatus, error) {
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)
}

View File

@ -2,6 +2,7 @@ package api
import (
"fmt"
"net/http"
)
const (
@ -34,7 +35,7 @@ type ResponseStatsData struct {
func (c *Client) RequestStatsVHost(vhost string) (*ResponseStats, error) {
req := ResponseStats{}
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 &req, nil
@ -43,7 +44,7 @@ func (c *Client) RequestStatsVHost(vhost string) (*ResponseStats, error) {
func (c *Client) RequestStatsApp(vhost, app string) (*ResponseStats, error) {
req := ResponseStats{}
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 &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) {
req := ResponseStats{}
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 &req, nil