214 lines
5.5 KiB
Go
214 lines
5.5 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
|
|
attempts int
|
|
retryDelay time.Duration
|
|
}
|
|
|
|
func NewRemoteClient(client HTTPDoer) *RemoteClient {
|
|
if client == nil {
|
|
client = &http.Client{Timeout: 5 * time.Second}
|
|
}
|
|
return &RemoteClient{
|
|
httpClient: client,
|
|
attempts: 5,
|
|
retryDelay: 200 * time.Millisecond,
|
|
}
|
|
}
|
|
|
|
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) {
|
|
resp, err := c.doRequest(ctx, func() (*http.Request, 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")
|
|
return req, nil
|
|
})
|
|
if err != nil {
|
|
return nil, 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", responseURL(resp, service.APIBaseURL+"/api/manage/config"), 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)
|
|
|
|
resp, err := c.doRequest(ctx, func() (*http.Request, error) {
|
|
return c.newRequest(ctx, http.MethodGet, service, "/api/manage/files/download?"+query.Encode(), nil)
|
|
})
|
|
if err != nil {
|
|
return nil, 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 {
|
|
resp, err := c.doRequest(ctx, func() (*http.Request, error) {
|
|
return c.newRequest(ctx, http.MethodGet, service, endpoint, nil)
|
|
})
|
|
if err != nil {
|
|
return 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", responseURL(resp, strings.TrimRight(service.APIBaseURL, "/")+endpoint), err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func responseURL(resp *http.Response, fallback string) string {
|
|
if resp != nil && resp.Request != nil && resp.Request.URL != nil {
|
|
return resp.Request.URL.String()
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func (c *RemoteClient) doRequest(ctx context.Context, newReq func() (*http.Request, error)) (*http.Response, error) {
|
|
attempts := c.attempts
|
|
if attempts <= 0 {
|
|
attempts = 1
|
|
}
|
|
delay := c.retryDelay
|
|
if delay <= 0 {
|
|
delay = 100 * time.Millisecond
|
|
}
|
|
|
|
var lastReq *http.Request
|
|
var lastErr error
|
|
for attempt := 1; attempt <= attempts; attempt++ {
|
|
req, err := newReq()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
lastReq = req
|
|
resp, err := c.httpClient.Do(req)
|
|
if err == nil {
|
|
return resp, nil
|
|
}
|
|
lastErr = err
|
|
if attempt == attempts {
|
|
break
|
|
}
|
|
if err := sleepWithContext(ctx, delay); err != nil {
|
|
return nil, err
|
|
}
|
|
delay *= 2
|
|
}
|
|
|
|
if lastReq != nil {
|
|
return nil, fmt.Errorf("request %s %s: %w", lastReq.Method, lastReq.URL.String(), lastErr)
|
|
}
|
|
return nil, lastErr
|
|
}
|
|
|
|
func sleepWithContext(ctx context.Context, delay time.Duration) error {
|
|
timer := time.NewTimer(delay)
|
|
defer timer.Stop()
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
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)
|
|
}
|