Skip to content

Commit d7a7dbb

Browse files
committed
Reuse existing client creation logic
1 parent 1b571a6 commit d7a7dbb

2 files changed

Lines changed: 23 additions & 24 deletions

File tree

pkg/cmd/api/api.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package api
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io"
87
"net/http"
98
"os"
109

1110
"github.com/MakeNowJust/heredoc/v2"
11+
"github.com/OctopusDeploy/cli/pkg/apiclient"
1212
"github.com/OctopusDeploy/cli/pkg/constants"
1313
"github.com/OctopusDeploy/cli/pkg/constants/annotations"
1414
"github.com/OctopusDeploy/cli/pkg/factory"
@@ -21,7 +21,7 @@ var OsExit = os.Exit
2121
func NewCmdAPI(f factory.Factory) *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "api <url>",
24-
Short: "Execute a raw API request",
24+
Short: "Execute a raw API GET request",
2525
Long: "Execute an authenticated GET request against the Octopus Server API and print the JSON response.",
2626
Example: heredoc.Docf(`
2727
$ %[1]s api /api
@@ -41,36 +41,17 @@ func NewCmdAPI(f factory.Factory) *cobra.Command {
4141
}
4242

4343
func apiRun(cmd *cobra.Command, f factory.Factory, path string) error {
44-
host := f.GetCurrentHost()
45-
fullURL := host + path
46-
47-
httpClient, err := f.GetHttpClient()
48-
if err != nil {
49-
return err
50-
}
51-
if httpClient == nil {
52-
httpClient = &http.Client{}
53-
}
54-
55-
req, err := http.NewRequest("GET", fullURL, nil)
44+
client, err := f.GetSystemClient(apiclient.NewRequester(cmd))
5645
if err != nil {
5746
return err
5847
}
5948

60-
configProvider, err := f.GetConfigProvider()
49+
req, err := http.NewRequest("GET", path, nil)
6150
if err != nil {
6251
return err
6352
}
6453

65-
apiKey := configProvider.Get(constants.ConfigApiKey)
66-
accessToken := configProvider.Get(constants.ConfigAccessToken)
67-
if apiKey != "" {
68-
req.Header.Set("X-Octopus-ApiKey", apiKey)
69-
} else if accessToken != "" {
70-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
71-
}
72-
73-
resp, err := httpClient.Do(req)
54+
resp, err := client.HttpSession().DoRawRequest(req)
7455
if err != nil {
7556
return err
7657
}

pkg/cmd/api/api_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
)
1515

16+
// respondToSdkInit handles the two HTTP requests that the Octopus SDK makes
17+
// when initialising the system client: fetching the root resource and listing
18+
// spaces to find the default space.
19+
func respondToSdkInit(t *testing.T, api *testutil.MockHttpServer) {
20+
api.ExpectRequest(t, "GET", "/api/").RespondWith(testutil.NewRootResource())
21+
api.ExpectRequest(t, "GET", "/api/spaces").RespondWith(map[string]any{
22+
"Items": []any{},
23+
"ItemsPerPage": 30,
24+
"TotalResults": 0,
25+
})
26+
}
27+
1628
func TestApiCommand(t *testing.T) {
1729
tests := []struct {
1830
name string
@@ -25,6 +37,8 @@ func TestApiCommand(t *testing.T) {
2537
return rootCmd.ExecuteC()
2638
})
2739

40+
respondToSdkInit(t, api)
41+
2842
api.ExpectRequest(t, "GET", "/api").RespondWithStatus(http.StatusOK, "200 OK", map[string]string{
2943
"Application": "Octopus Deploy",
3044
"Version": "2024.1.0",
@@ -49,6 +63,8 @@ func TestApiCommand(t *testing.T) {
4963
return rootCmd.ExecuteC()
5064
})
5165

66+
respondToSdkInit(t, api)
67+
5268
api.ExpectRequest(t, "GET", "/api/nonexistent").RespondWithStatus(http.StatusNotFound, "404 Not Found", map[string]string{
5369
"ErrorMessage": "Not found",
5470
})
@@ -66,6 +82,8 @@ func TestApiCommand(t *testing.T) {
6682
return rootCmd.ExecuteC()
6783
})
6884

85+
respondToSdkInit(t, api)
86+
6987
r, _ := api.ReceiveRequest()
7088
assert.Equal(t, "GET", r.Method)
7189
assert.Equal(t, "/api/health", r.URL.Path)

0 commit comments

Comments
 (0)