mirror of
https://github.com/docker/compose.git
synced 2025-07-25 06:34:35 +02:00
refactor: improve Desktop client structure
Signed-off-by: Adel Sevastianov <102406080+Saracomethstein@users.noreply.github.com>
This commit is contained in:
parent
482b622282
commit
0bcc629fbe
@ -53,11 +53,10 @@ func NewClient(apiEndpoint string) *Client {
|
||||
}
|
||||
transport = otelhttp.NewTransport(transport)
|
||||
|
||||
c := &Client{
|
||||
return &Client{
|
||||
apiEndpoint: apiEndpoint,
|
||||
client: &http.Client{Transport: transport},
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Endpoint() string {
|
||||
@ -76,18 +75,17 @@ type PingResponse struct {
|
||||
|
||||
// Ping is a minimal API used to ensure that the server is available.
|
||||
func (c *Client) Ping(ctx context.Context) (*PingResponse, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/ping"), http.NoBody)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, "/ping", http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
@ -106,18 +104,17 @@ type FeatureFlagValue struct {
|
||||
}
|
||||
|
||||
func (c *Client) FeatureFlags(ctx context.Context) (FeatureFlagResponse, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/features"), http.NoBody)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, "/features", http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
@ -137,18 +134,16 @@ type GetFileSharesConfigResponse struct {
|
||||
}
|
||||
|
||||
func (c *Client) GetFileSharesConfig(ctx context.Context) (*GetFileSharesConfigResponse, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/mutagen/file-shares/config"), http.NoBody)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, "/mutagen/file-shares/config", http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, newHTTPStatusCodeError(resp)
|
||||
@ -171,25 +166,28 @@ type CreateFileShareResponse struct {
|
||||
}
|
||||
|
||||
func (c *Client) CreateFileShare(ctx context.Context, r CreateFileShareRequest) (*CreateFileShareResponse, error) {
|
||||
rawBody, _ := json.Marshal(r)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, backendURL("/mutagen/file-shares"), bytes.NewReader(rawBody))
|
||||
rawBody, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
||||
}
|
||||
|
||||
req, err := c.newRequest(ctx, http.MethodPost, "/mutagen/file-shares", bytes.NewReader(rawBody))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
errBody, _ := io.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(errBody))
|
||||
}
|
||||
|
||||
var ret CreateFileShareResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
|
||||
return nil, err
|
||||
@ -216,18 +214,16 @@ type FileShareSession struct {
|
||||
}
|
||||
|
||||
func (c *Client) ListFileShares(ctx context.Context) ([]FileShareSession, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/mutagen/file-shares"), http.NoBody)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, "/mutagen/file-shares", http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, newHTTPStatusCodeError(resp)
|
||||
@ -241,18 +237,17 @@ func (c *Client) ListFileShares(ctx context.Context) ([]FileShareSession, error)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteFileShare(ctx context.Context, id string) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, backendURL("/mutagen/file-shares/"+id), http.NoBody)
|
||||
req, err := c.newRequest(ctx, http.MethodDelete, "/mutagen/file-shares/"+id, http.NoBody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return newHTTPStatusCodeError(resp)
|
||||
}
|
||||
@ -274,20 +269,18 @@ func newHTTPStatusCodeError(resp *http.Response) error {
|
||||
}
|
||||
|
||||
func (c *Client) StreamFileShares(ctx context.Context) (<-chan EventMessage[[]FileShareSession], error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/mutagen/file-shares/stream"), http.NoBody)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, "/mutagen/file-shares/stream", http.NoBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
_ = resp.Body.Close()
|
||||
return nil, newHTTPStatusCodeError(resp)
|
||||
}
|
||||
|
||||
@ -295,9 +288,6 @@ func (c *Client) StreamFileShares(ctx context.Context) (<-chan EventMessage[[]Fi
|
||||
go func(ctx context.Context) {
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
for range events {
|
||||
// drain the channel
|
||||
}
|
||||
close(events)
|
||||
}()
|
||||
if err := readEvents(ctx, resp.Body, events); err != nil {
|
||||
@ -334,6 +324,15 @@ func readEvents[T any](ctx context.Context, r io.Reader, events chan<- EventMess
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, method, backendURL(path), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// backendURL generates a URL for the given API path.
|
||||
//
|
||||
// NOTE: Custom transport handles communication. The host is to create a valid
|
||||
|
Loading…
x
Reference in New Issue
Block a user