Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit e806cc4

Browse files
committed
refactor: combine the requests endpoints
Differentiate regular and streaming based on the Accept Header. Signed-off-by: Dorin Geman <[email protected]>
1 parent 3fa1028 commit e806cc4

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

commands/requests.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"bufio"
55
"fmt"
6+
"io"
67
"strings"
78

89
"github.com/docker/model-cli/commands/completion"
@@ -27,16 +28,19 @@ func newRequestsCmd() *cobra.Command {
2728
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), cmd); err != nil {
2829
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
2930
}
30-
if follow {
31-
responseBody, err := desktopClient.RequestsStream(model, includeExisting)
32-
if err != nil {
33-
errMsg := "Failed to get requests stream"
34-
if model != "" {
35-
errMsg = errMsg + " for" + model
36-
}
37-
err = handleClientError(err, errMsg)
38-
return handleNotRunningError(err)
31+
32+
responseBody, cancel, err := desktopClient.Requests(model, follow, includeExisting)
33+
if err != nil {
34+
errMsg := "Failed to get requests"
35+
if model != "" {
36+
errMsg = errMsg + " for " + model
3937
}
38+
err = handleClientError(err, errMsg)
39+
return handleNotRunningError(err)
40+
}
41+
defer cancel()
42+
43+
if follow {
4044
scanner := bufio.NewScanner(responseBody)
4145
cmd.Println("Connected to request stream. Press Ctrl+C to stop.")
4246
var currentEvent string
@@ -56,18 +60,14 @@ func newRequestsCmd() *cobra.Command {
5660
}
5761
}
5862
cmd.Println("Stream closed by server.")
59-
return nil
60-
}
61-
requests, err := desktopClient.RequestsList(model)
62-
if err != nil {
63-
errMsg := "Failed to get requests"
64-
if model != "" {
65-
errMsg = errMsg + " for" + model
63+
} else {
64+
body, err := io.ReadAll(responseBody)
65+
if err != nil {
66+
return fmt.Errorf("failed to read response body: %w", err)
6667
}
67-
err = handleClientError(err, errMsg)
68-
return handleNotRunningError(err)
68+
cmd.Print(string(body))
6969
}
70-
cmd.Print(requests)
70+
7171
return nil
7272
},
7373
ValidArgsFunction: completion.NoComplete,

desktop/desktop.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -657,61 +657,61 @@ func (c *Client) ConfigureBackend(request scheduling.ConfigureRequest) error {
657657
return nil
658658
}
659659

660-
func (c *Client) RequestsStream(modelFilter string, includeExisting bool) (io.ReadCloser, error) {
661-
path := c.modelRunner.URL(inference.InferencePrefix + "/requests/stream")
660+
// Requests returns a response body and a cancel function to ensure proper cleanup.
661+
func (c *Client) Requests(modelFilter string, streaming bool, includeExisting bool) (io.ReadCloser, func(), error) {
662+
path := c.modelRunner.URL(inference.InferencePrefix + "/requests")
662663
var queryParams []string
663664
if modelFilter != "" {
664665
queryParams = append(queryParams, "model="+modelFilter)
665666
}
666-
if includeExisting {
667+
if includeExisting && streaming {
667668
queryParams = append(queryParams, "include_existing=true")
668669
}
669670
if len(queryParams) > 0 {
670671
path += "?" + strings.Join(queryParams, "&")
671672
}
673+
672674
req, err := http.NewRequest(http.MethodGet, path, nil)
673675
if err != nil {
674-
return nil, fmt.Errorf("failed to create request: %w", err)
675-
}
676-
req.Header.Set("Accept", "text/event-stream")
677-
req.Header.Set("Cache-Control", "no-cache")
678-
req.Header.Set("User-Agent", "docker-model-cli/"+Version)
679-
resp, err := c.modelRunner.Client().Do(req)
680-
if err != nil {
681-
return nil, fmt.Errorf("failed to connect to stream: %w", err)
682-
}
683-
if resp.StatusCode != http.StatusOK {
684-
resp.Body.Close()
685-
return nil, fmt.Errorf("stream request failed with status: %d", resp.StatusCode)
676+
return nil, nil, fmt.Errorf("failed to create request: %w", err)
686677
}
687-
return resp.Body, nil
688-
}
689678

690-
func (c *Client) RequestsList(modelFilter string) (string, error) {
691-
path := inference.InferencePrefix + "/requests"
692-
if modelFilter != "" {
693-
path += "?model=" + modelFilter
679+
if streaming {
680+
req.Header.Set("Accept", "text/event-stream")
681+
req.Header.Set("Cache-Control", "no-cache")
682+
} else {
683+
req.Header.Set("Accept", "application/json")
694684
}
695-
resp, err := c.doRequest(http.MethodGet, path, nil)
685+
req.Header.Set("User-Agent", "docker-model-cli/"+Version)
686+
687+
resp, err := c.modelRunner.Client().Do(req)
696688
if err != nil {
697-
return "", c.handleQueryError(err, path)
689+
if streaming {
690+
return nil, nil, c.handleQueryError(fmt.Errorf("failed to connect to stream: %w", err), path)
691+
}
692+
return nil, nil, c.handleQueryError(err, path)
698693
}
699-
defer resp.Body.Close()
700694

701-
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
702-
return "", fmt.Errorf("failed to list requests: %s", resp.Status)
703-
}
695+
if resp.StatusCode != http.StatusOK {
696+
if resp.StatusCode == http.StatusNotFound {
697+
body, _ := io.ReadAll(resp.Body)
698+
resp.Body.Close()
699+
return nil, nil, fmt.Errorf("%s", strings.TrimSpace(string(body)))
700+
}
704701

705-
body, err := io.ReadAll(resp.Body)
706-
if err != nil {
707-
return "", fmt.Errorf("failed to read response body: %w", err)
702+
resp.Body.Close()
703+
if streaming {
704+
return nil, nil, fmt.Errorf("stream request failed with status: %d", resp.StatusCode)
705+
}
706+
return nil, nil, fmt.Errorf("failed to list requests: %s", resp.Status)
708707
}
709708

710-
if resp.StatusCode == http.StatusNotFound {
711-
return "", fmt.Errorf("%s", strings.TrimSpace(string(body)))
709+
// Return the response body and a cancel function that closes it.
710+
cancel := func() {
711+
resp.Body.Close()
712712
}
713713

714-
return string(body), nil
714+
return resp.Body, cancel, nil
715715
}
716716

717717
// doRequest is a helper function that performs HTTP requests and handles 503 responses

0 commit comments

Comments
 (0)