137 lines
3.6 KiB
Go
137 lines
3.6 KiB
Go
package push_client
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"git.bit5.ru/backend/errors"
|
|
"git.bit5.ru/backend/push_common"
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
)
|
|
|
|
const tracerName = "git.bit5.ru/backend/push_client"
|
|
|
|
var tracer = otel.Tracer(tracerName)
|
|
|
|
type Client struct {
|
|
addr string
|
|
}
|
|
|
|
func NewClient(addr string) *Client {
|
|
return &Client{
|
|
addr: addr,
|
|
}
|
|
}
|
|
|
|
func (c *Client) ServerInfo() (*push_common.ServerInfo, error) {
|
|
var info push_common.ServerInfo
|
|
err := httpGet(c.addr+"/api/server_info", &info)
|
|
return &info, err
|
|
}
|
|
|
|
func (c *Client) UserPushPreviews(ctx context.Context) ([]*push_common.PushPreview, error) {
|
|
var previews []*push_common.PushPreview
|
|
if err := httpGet(c.addr+"/api/push/previews/user", &previews); err != nil {
|
|
return nil, err
|
|
}
|
|
return previews, nil
|
|
}
|
|
|
|
func (c *Client) AutoPushPreviews() ([]*push_common.PushPreview, error) {
|
|
var previews []*push_common.PushPreview
|
|
if err := httpGet(c.addr+"/api/push/previews/auto", &previews); err != nil {
|
|
return nil, err
|
|
}
|
|
return previews, nil
|
|
}
|
|
|
|
func (c *Client) NewPush(req push_common.PushRequest) (int, error) {
|
|
resp, err := httpPostDataDeprecated(c.addr+"/api/push/add", req)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return strconv.Atoi(resp)
|
|
}
|
|
|
|
func (c *Client) SchedulePush(ctx context.Context, def *push_common.PushDef) (int, error) {
|
|
_, span := tracer.Start(ctx, "Client.SchedulePush")
|
|
defer span.End()
|
|
|
|
resp, err := httpPostDataDeprecated(c.addr+"/api/push/schedule", def)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return strconv.Atoi(resp)
|
|
}
|
|
|
|
func (c *Client) StartPush(id int) error {
|
|
return httpPost(c.addr + "/api/push/start/" + strconv.Itoa(id))
|
|
}
|
|
|
|
func (c *Client) PausePush(id int) error {
|
|
return httpPost(c.addr + "/api/push/pause/" + strconv.Itoa(id))
|
|
}
|
|
|
|
/*
|
|
func (c *Client) ArchivePush(ctx context.Context, id int) error {
|
|
return httpPost(c.Addr + "/api/push/archive/" + strconv.Itoa(id))
|
|
}
|
|
*/
|
|
|
|
func (c *Client) DelPush(ctx context.Context, id int) error {
|
|
return httpPost(c.addr + "/api/push/del/" + strconv.Itoa(id))
|
|
}
|
|
|
|
func (c *Client) GetPush(id int) (*push_common.Push, error) {
|
|
var push push_common.Push
|
|
err := httpGet(c.addr+"/api/push/get/"+strconv.Itoa(id), &push)
|
|
return &push, err
|
|
}
|
|
|
|
func (c *Client) GetPushPayload(id int) (*push_common.PushPayload, error) {
|
|
var payload *push_common.PushPayload
|
|
if err := httpGet(c.addr+"/api/push/get_payload/"+strconv.Itoa(id), &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
return payload, nil
|
|
}
|
|
|
|
func (c *Client) EditPushRequest(id int, payload push_common.PushPayload) error {
|
|
_, err := httpPostDataDeprecated(c.addr+"/api/push/edit/"+strconv.Itoa(id), payload)
|
|
return err
|
|
}
|
|
|
|
func (c *Client) GetIncomingPushes(ctx context.Context) ([]*push_common.IncomingPushInfo, error) {
|
|
_, span := tracer.Start(ctx, "Client.GetIncomingPushes")
|
|
defer span.End()
|
|
|
|
var pushes []*push_common.IncomingPushInfo
|
|
if err := httpGet(c.addr+"/api/push/incoming_pushes", &pushes); err != nil {
|
|
return nil, errors.WithMessage(err, "Can not load incoming pushes.")
|
|
}
|
|
|
|
return pushes, nil
|
|
}
|
|
|
|
func (c *Client) GetPushesToClean(ctx context.Context) ([]*push_common.Push, error) {
|
|
_, span := tracer.Start(ctx, "Client.GetPushesToClean")
|
|
defer span.End()
|
|
|
|
var lst []*push_common.Push
|
|
if err := httpGet(c.addr+"/api/push/list_to_clean", &lst); err != nil {
|
|
return nil, errors.WithMessage(err, "Can not load list_to_clean.")
|
|
}
|
|
return lst, nil
|
|
}
|
|
|
|
func (c *Client) LaunchPush(ctx context.Context, logger logr.Logger, launchRequest *push_common.PushLaunchRequest) error {
|
|
_, span := tracer.Start(ctx, "Client.LaunchPush")
|
|
defer span.End()
|
|
|
|
_, err := httpPostData(logger, c.addr+"/api/push/launch", launchRequest)
|
|
return err
|
|
}
|