@@ -3,14 +3,13 @@ package console
33
44import (
55 "bytes"
6- "context"
76 "encoding/json"
87 "fmt"
98 "github.com/philips-software/go-hsdp-api/internal"
9+ "golang.org/x/oauth2"
1010 "io"
1111 "io/ioutil"
1212 "net/http"
13- "net/http/httputil"
1413 "net/url"
1514 "os"
1615 "strings"
@@ -19,7 +18,6 @@ import (
1918
2019 validator "github.com/go-playground/validator/v10"
2120 "github.com/google/go-querystring/query"
22- "github.com/google/uuid"
2321 autoconf "github.com/philips-software/go-hsdp-api/config"
2422)
2523
@@ -128,7 +126,8 @@ type Client struct {
128126
129127 Metrics * MetricsService
130128
131- debugFile * os.File
129+ debugFile * os.File
130+ consoleErr error
132131
133132 sync.Mutex
134133}
@@ -154,25 +153,24 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) {
154153 if config .UAAURL == "" {
155154 return nil , ErrUAAURLCannotBeEmpty
156155 }
157- if config .BaseConsoleURL == "" {
158- return nil , ErrConsoleURLCannotBeEmpty
159- }
160156 c := & Client {client : httpClient , config : config , UserAgent : userAgent }
161157 if err := c .SetBaseUAAURL (c .config .UAAURL ); err != nil {
162158 return nil , err
163159 }
164160 if err := c .SetBaseConsoleURL (c .config .BaseConsoleURL ); err != nil {
165- return nil , err
161+ c . consoleErr = err
166162 }
167-
168163 if config .DebugLog != "" {
169- debugFile , err := os .OpenFile (config .DebugLog , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0600 )
170- if err != nil {
171- c .debugFile = nil
172- } else {
173- c .debugFile = debugFile
164+ var err error
165+ c .debugFile , err = os .OpenFile (config .DebugLog , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0600 )
166+ if err == nil {
167+ httpClient .Transport = internal .NewLoggingRoundTripper (httpClient .Transport , c .debugFile )
174168 }
175169 }
170+ header := make (http.Header )
171+ header .Set ("User-Agent" , userAgent )
172+ httpClient .Transport = internal .NewHeaderRoundTripper (httpClient .Transport , header )
173+
176174 c .Metrics = & MetricsService {client : c }
177175 c .validate = validator .New ()
178176 return c , nil
@@ -208,8 +206,8 @@ func (c *Client) HttpClient() *http.Client {
208206 return c .client
209207}
210208
211- // Token returns the current token
212- func (c * Client ) Token () string {
209+ // Token returns the current token. It also confirms to TokenSource
210+ func (c * Client ) Token () ( * oauth2. Token , error ) {
213211 c .Lock ()
214212 defer c .Unlock ()
215213
@@ -218,10 +216,14 @@ func (c *Client) Token() string {
218216
219217 if expires - now < 60 {
220218 if c .TokenRefresh () != nil {
221- return ""
219+ return nil , fmt . Errorf ( "failed to refresh console token" )
222220 }
223221 }
224- return c .token
222+ return & oauth2.Token {
223+ AccessToken : c .token ,
224+ RefreshToken : c .refreshToken ,
225+ Expiry : c .expiresAt ,
226+ }, nil
225227}
226228
227229// TokenRefresh refreshes the accessToken
@@ -320,6 +322,9 @@ func (c *Client) newRequest(endpoint, method, path string, opt interface{}, opti
320322 u = * c .baseUAAURL
321323 u .Opaque = c .baseUAAURL .Path + path
322324 case CONSOLE :
325+ if c .consoleErr != nil {
326+ return nil , c .consoleErr
327+ }
323328 u = * c .baseConsoleURL
324329 u .Opaque = c .baseConsoleURL .Path + path
325330 default :
@@ -371,14 +376,10 @@ func (c *Client) newRequest(endpoint, method, path string, opt interface{}, opti
371376
372377 switch c .tokenType {
373378 case oAuthToken :
374- if token := c .Token (); token != "" {
375- req .Header .Set ("Authorization" , "Bearer " + c . token )
379+ if token , err := c .Token (); err == nil {
380+ req .Header .Set ("Authorization" , "Bearer " + token . AccessToken )
376381 }
377382 }
378-
379- if c .UserAgent != "" {
380- req .Header .Set ("User-Agent" , c .UserAgent )
381- }
382383 return req , nil
383384}
384385
@@ -401,27 +402,7 @@ func newResponse(r *http.Response) *Response {
401402// interface, the raw response body will be written to v, without attempting to
402403// first decode it.
403404func (c * Client ) do (req * http.Request , v interface {}) (* Response , error ) {
404- id := uuid .New ()
405-
406- if c .debugFile != nil {
407- dumped , _ := httputil .DumpRequest (req , true )
408- out := fmt .Sprintf ("[go-hsdp-api] --- Request [%s] start ---\n %s\n [go-hsdp-api] --- Request [%s] end ---\n " , id , string (dumped ), id )
409- if c .debugFile != nil {
410- _ , _ = c .debugFile .WriteString (out )
411- } else {
412- fmt .Println (out )
413- }
414- }
415405 resp , err := c .client .Do (req )
416- if c .debugFile != nil && resp != nil {
417- dumped , _ := httputil .DumpResponse (resp , true )
418- out := fmt .Sprintf ("[go-hsdp-api] --- Response [%s] start ---\n %s\n [go-hsdp-api] --- Response [%s] end ---\n " , id , string (dumped ), id )
419- if c .debugFile != nil {
420- _ , _ = c .debugFile .WriteString (out )
421- } else {
422- fmt .Println (out )
423- }
424- }
425406 if err != nil {
426407 return nil , err
427408 }
@@ -443,14 +424,6 @@ func (c *Client) do(req *http.Request, v interface{}) (*Response, error) {
443424 return response , err
444425}
445426
446- // WithContext runs the request with the provided context
447- func WithContext (ctx context.Context ) OptionFunc {
448- return func (req * http.Request ) error {
449- * req = * req .WithContext (ctx )
450- return nil
451- }
452- }
453-
454427// CheckResponse checks the API response for errors, and returns them if present.
455428func checkResponse (r * http.Response ) error {
456429 switch r .StatusCode {
0 commit comments