@@ -5,22 +5,21 @@ import (
5
5
"context"
6
6
"encoding/json"
7
7
"fmt"
8
- "github.com/harness/harness-mcp/client/ar"
9
8
"io"
10
9
"log/slog"
11
10
"net/http"
12
11
"net/url"
13
12
"strings"
14
13
"time"
15
14
15
+ "github.com/harness/harness-mcp/pkg/harness/auth"
16
+
16
17
"github.com/cenkalti/backoff/v4"
17
18
"github.com/harness/harness-mcp/client/dto"
18
19
"github.com/rs/zerolog/log"
19
20
)
20
21
21
22
var (
22
- defaultBaseURL = "https://app.harness.io/"
23
-
24
23
// these can be moved to a level above if we want to make this a generic
25
24
// client, keeping these here to ensure we don't end up returning too much info
26
25
// when different tools get added.
@@ -43,17 +42,8 @@ type Client struct {
43
42
// set to a domain endpoint to use with custom Harness installations
44
43
BaseURL * url.URL
45
44
46
- // API key for authentication
47
- // TODO: We can abstract out the auth provider
48
- APIKey string
49
-
50
- // Services used for talking to different Harness entities
51
- Connectors * ConnectorService
52
- PullRequests * PullRequestService
53
- Pipelines * PipelineService
54
- Repositories * RepositoryService
55
- Logs * LogService
56
- Registry * ar.ClientWithResponses
45
+ // AuthProvider used for authentication
46
+ AuthProvider auth.Provider
57
47
}
58
48
59
49
type service struct {
@@ -67,49 +57,19 @@ func defaultHTTPClient() *http.Client {
67
57
}
68
58
69
59
// NewWithToken creates a new client with the specified base URL and API token
70
- func NewWithToken (uri , apiKey string ) (* Client , error ) {
60
+ func NewWithAuthProvider (uri string , authProvider auth. Provider ) (* Client , error ) {
71
61
parsedURL , err := url .Parse (uri )
72
62
if err != nil {
73
63
return nil , err
74
64
}
75
65
c := & Client {
76
- client : defaultHTTPClient (),
77
- BaseURL : parsedURL ,
78
- APIKey : apiKey ,
66
+ client : defaultHTTPClient (),
67
+ BaseURL : parsedURL ,
68
+ AuthProvider : authProvider ,
79
69
}
80
- c .initialize ()
81
70
return c , nil
82
71
}
83
72
84
- func (c * Client ) initialize () error {
85
- if c .client == nil {
86
- c .client = defaultHTTPClient ()
87
- }
88
- if c .BaseURL == nil {
89
- baseURL , err := url .Parse (defaultBaseURL )
90
- if err != nil {
91
- return err
92
- }
93
- c .BaseURL = baseURL
94
- }
95
-
96
- c .Connectors = & ConnectorService {client : c }
97
- c .PullRequests = & PullRequestService {client : c }
98
- c .Pipelines = & PipelineService {client : c }
99
- c .Repositories = & RepositoryService {client : c }
100
- c .Logs = & LogService {client : c }
101
-
102
- // TODO: Replace it with harness-go-sdk
103
- arClient , err := ar .NewClientWithResponses (c .BaseURL .String ()+ "/har/api/v1" , ar .WithHTTPClient (c .client ),
104
- ar .WithRequestEditorFn (getEditor (c .APIKey )))
105
- if err != nil {
106
- return err
107
- }
108
- c .Registry = arClient
109
-
110
- return nil
111
- }
112
-
113
73
// Get is a simple helper that builds up the request URL, adding the path and parameters.
114
74
// The response from the request is unmarshalled into the data parameter.
115
75
func (c * Client ) Get (
@@ -262,7 +222,14 @@ func (c *Client) PostRaw(
262
222
// Do is a wrapper of http.Client.Do that injects the auth header in the request.
263
223
func (c * Client ) Do (r * http.Request ) (* http.Response , error ) {
264
224
slog .Debug ("Request" , "method" , r .Method , "url" , r .URL .String ())
265
- r .Header .Add (apiKeyHeader , c .APIKey )
225
+
226
+ // set the auth header
227
+ ctx := r .Context ()
228
+ k , v , err := c .AuthProvider .GetHeader (ctx )
229
+ if err != nil {
230
+ return nil , err
231
+ }
232
+ r .Header .Set (k , v )
266
233
267
234
return c .client .Do (r )
268
235
}
@@ -367,11 +334,3 @@ func setDefaultPagination(opts *dto.PaginationOptions) {
367
334
}
368
335
}
369
336
}
370
-
371
- // TODO: Remove once we have Client service or we integrate with go-sdk
372
- func getEditor (token string ) func (ctx context.Context , req * http.Request ) error {
373
- return func (ctx context.Context , req * http.Request ) error {
374
- req .Header .Set ("x-api-key" , token )
375
- return nil
376
- }
377
- }
0 commit comments