Skip to content

Commit c2f9a85

Browse files
committed
wip: plugin work
1 parent e69b23d commit c2f9a85

4 files changed

Lines changed: 985 additions & 0 deletions

File tree

sdk/camera_http.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package sdk
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
"time"
9+
)
10+
11+
var errCameraHostRequired = errors.New("host is required")
12+
13+
// CameraHTTPClient wraps the shared HTTP request behavior for camera plugins.
14+
type CameraHTTPClient struct {
15+
BaseURL string
16+
Timeout time.Duration
17+
AuthHeader string
18+
}
19+
20+
// NewCameraHTTPClient builds a shared HTTP client from camera plugin config.
21+
func NewCameraHTTPClient(cfg CameraPluginConfig, fallbackTimeout time.Duration) (*CameraHTTPClient, error) {
22+
host := strings.TrimSpace(cfg.Host)
23+
if host == "" {
24+
return nil, errCameraHostRequired
25+
}
26+
27+
scheme, err := cfg.NormalizedScheme()
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
return &CameraHTTPClient{
33+
BaseURL: fmt.Sprintf("%s://%s", scheme, host),
34+
Timeout: cfg.ParsedTimeout(fallbackTimeout),
35+
AuthHeader: cfg.BasicAuthHeader(),
36+
}, nil
37+
}
38+
39+
// URL resolves a relative path against the camera base URL.
40+
func (c *CameraHTTPClient) URL(path string) string {
41+
if c == nil {
42+
return path
43+
}
44+
45+
return c.BaseURL + path
46+
}
47+
48+
// DoContext performs an HTTP request with the shared auth and timeout settings.
49+
func (c *CameraHTTPClient) DoContext(ctx context.Context, req HTTPRequest) (*HTTPResponse, error) {
50+
if c == nil {
51+
return nil, errCameraHostRequired
52+
}
53+
54+
if req.URL == "" {
55+
req.URL = c.BaseURL
56+
}
57+
if req.TimeoutMS == 0 {
58+
req.TimeoutMS = int(c.Timeout.Milliseconds())
59+
}
60+
if c.AuthHeader != "" {
61+
if req.Headers == nil {
62+
req.Headers = map[string]string{}
63+
}
64+
if _, ok := req.Headers["Authorization"]; !ok {
65+
req.Headers["Authorization"] = c.AuthHeader
66+
}
67+
}
68+
69+
return HTTP.DoContext(ctx, req)
70+
}
71+
72+
// GetContext performs a GET request against a relative path.
73+
func (c *CameraHTTPClient) GetContext(ctx context.Context, path string) (*HTTPResponse, error) {
74+
return c.DoContext(ctx, HTTPRequest{
75+
Method: "GET",
76+
URL: c.URL(path),
77+
})
78+
}

sdk/camera_http_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sdk
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestNewCameraHTTPClient(t *testing.T) {
9+
t.Parallel()
10+
11+
client, err := NewCameraHTTPClient(CameraPluginConfig{
12+
Host: " camera.local ",
13+
Scheme: "HTTPS",
14+
Timeout: "15s",
15+
Username: "root",
16+
Password: "secret",
17+
}, 3*time.Second)
18+
if err != nil {
19+
t.Fatalf("expected client, got error %v", err)
20+
}
21+
22+
if client.BaseURL != "https://camera.local" {
23+
t.Fatalf("unexpected base URL: %q", client.BaseURL)
24+
}
25+
if client.Timeout != 15*time.Second {
26+
t.Fatalf("unexpected timeout: %s", client.Timeout)
27+
}
28+
if client.AuthHeader != "Basic cm9vdDpzZWNyZXQ=" {
29+
t.Fatalf("unexpected auth header: %q", client.AuthHeader)
30+
}
31+
}
32+
33+
func TestCameraHTTPClientURL(t *testing.T) {
34+
t.Parallel()
35+
36+
client := &CameraHTTPClient{BaseURL: "https://camera.local"}
37+
if got := client.URL("/axis-cgi/basicdeviceinfo.cgi"); got != "https://camera.local/axis-cgi/basicdeviceinfo.cgi" {
38+
t.Fatalf("unexpected camera URL: %q", got)
39+
}
40+
}

0 commit comments

Comments
 (0)