sync: migrate ai-operator to Gitea (2026-08-10)
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package ari
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ai-operator/internal/config"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
baseURL string
|
||||
user string
|
||||
password string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewClient(cfg config.AsteriskConfig) *Client {
|
||||
return &Client{
|
||||
baseURL: strings.TrimRight(cfg.ARIURL, "/"),
|
||||
user: cfg.ARIUser,
|
||||
password: cfg.ARIPassword,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) AuthenticatedGET(ctx context.Context, path string) (*http.Response, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.endpoint(path), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.SetBasicAuth(c.user, c.password)
|
||||
return c.httpClient.Do(req)
|
||||
}
|
||||
|
||||
func (c *Client) UnauthenticatedGET(ctx context.Context, path string) (*http.Response, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.endpoint(path), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.httpClient.Do(req)
|
||||
}
|
||||
|
||||
func (c *Client) endpoint(path string) string {
|
||||
return c.baseURL + "/" + strings.TrimLeft(path, "/")
|
||||
}
|
||||
|
||||
func drainAndClose(resp *http.Response) {
|
||||
if resp == nil || resp.Body == nil {
|
||||
return
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
|
||||
func statusError(name string, got int, want int) string {
|
||||
return fmt.Sprintf("%s returned HTTP %d, want %d", name, got, want)
|
||||
}
|
||||
Reference in New Issue
Block a user