155 lines
4.2 KiB
Go
155 lines
4.2 KiB
Go
package managed
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type HTTPDoer interface {
|
|
Do(req *http.Request) (*http.Response, error)
|
|
}
|
|
|
|
type RemoteClient struct {
|
|
httpClient HTTPDoer
|
|
}
|
|
|
|
func NewRemoteClient(client HTTPDoer) *RemoteClient {
|
|
if client == nil {
|
|
client = &http.Client{Timeout: 5 * time.Second}
|
|
}
|
|
return &RemoteClient{httpClient: client}
|
|
}
|
|
|
|
func (c *RemoteClient) GetConfig(ctx context.Context, service Service) (map[string]any, error) {
|
|
var payload map[string]any
|
|
if err := c.getJSON(ctx, service, "/api/manage/config", &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
return payload, nil
|
|
}
|
|
|
|
func (c *RemoteClient) UpdateRTSP(ctx context.Context, service Service, rtsp string) (map[string]any, error) {
|
|
body := strings.NewReader(fmt.Sprintf(`{"rtsp_url":%q}`, rtsp))
|
|
req, err := c.newRequest(ctx, http.MethodPut, service, "/api/manage/config", body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("request %s %s: %w", req.Method, req.URL.String(), err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 400 {
|
|
return nil, decodeAPIError(resp)
|
|
}
|
|
|
|
var payload map[string]any
|
|
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
|
return nil, fmt.Errorf("decode response %s: %w", req.URL.String(), err)
|
|
}
|
|
return payload, nil
|
|
}
|
|
|
|
func (c *RemoteClient) GetSummary(ctx context.Context, service Service) (*ResultSummary, error) {
|
|
var summary ResultSummary
|
|
if err := c.getJSON(ctx, service, "/api/manage/summary", &summary); err != nil {
|
|
return nil, err
|
|
}
|
|
return &summary, nil
|
|
}
|
|
|
|
func (c *RemoteClient) GetFiles(ctx context.Context, service Service) ([]ResultFile, error) {
|
|
var payload struct {
|
|
Files []ResultFile `json:"files"`
|
|
}
|
|
if err := c.getJSON(ctx, service, "/api/manage/files", &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
return payload.Files, nil
|
|
}
|
|
|
|
func (c *RemoteClient) PreviewFile(ctx context.Context, service Service, path string, lines int) (*FilePreview, error) {
|
|
query := url.Values{}
|
|
query.Set("path", path)
|
|
query.Set("lines", fmt.Sprintf("%d", lines))
|
|
|
|
var preview FilePreview
|
|
if err := c.getJSON(ctx, service, "/api/manage/files/preview?"+query.Encode(), &preview); err != nil {
|
|
return nil, err
|
|
}
|
|
return &preview, nil
|
|
}
|
|
|
|
func (c *RemoteClient) Download(ctx context.Context, service Service, path string) (*http.Response, error) {
|
|
query := url.Values{}
|
|
query.Set("path", path)
|
|
req, err := c.newRequest(ctx, http.MethodGet, service, "/api/manage/files/download?"+query.Encode(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("request %s %s: %w", req.Method, req.URL.String(), err)
|
|
}
|
|
if resp.StatusCode >= 400 {
|
|
defer resp.Body.Close()
|
|
return nil, decodeAPIError(resp)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (c *RemoteClient) getJSON(ctx context.Context, service Service, endpoint string, target any) error {
|
|
req, err := c.newRequest(ctx, http.MethodGet, service, endpoint, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("request %s %s: %w", req.Method, req.URL.String(), err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 400 {
|
|
return decodeAPIError(resp)
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(target); err != nil {
|
|
return fmt.Errorf("decode response %s: %w", req.URL.String(), err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *RemoteClient) newRequest(ctx context.Context, method string, service Service, endpoint string, body io.Reader) (*http.Request, error) {
|
|
base := strings.TrimRight(service.APIBaseURL, "/")
|
|
req, err := http.NewRequestWithContext(ctx, method, base+endpoint, body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("build request for %s%s: %w", base, endpoint, err)
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeAPIError(resp *http.Response) error {
|
|
data, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
|
var payload map[string]any
|
|
if err := json.Unmarshal(data, &payload); err == nil {
|
|
if message, ok := payload["error"].(string); ok && strings.TrimSpace(message) != "" {
|
|
return errors.New(message)
|
|
}
|
|
}
|
|
message := strings.TrimSpace(string(data))
|
|
if message == "" {
|
|
message = resp.Status
|
|
}
|
|
return errors.New(message)
|
|
}
|