@@ -3,7 +3,6 @@ package client
33import (
44 "bytes"
55 "encoding/json"
6- "errors"
76 "fmt"
87 "io"
98 "log"
@@ -15,11 +14,6 @@ import (
1514const (
1615 apiMeshObjectsRoot = "/api/meshobjects"
1716 loginEndpoint = "/api/login"
18-
19- ERROR_GENERIC_CLIENT_ERROR = "client error"
20- ERROR_GENERIC_API_ERROR = "api error"
21- ERROR_AUTHENTICATION_FAILURE = "Not authorized. Check api key and secret."
22- ERROR_ENDPOINT_LOOKUP = "Could not fetch endpoints for meshStack."
2317)
2418
2519type MeshStackProviderClient struct {
@@ -109,14 +103,16 @@ func (c *MeshStackProviderClient) login() error {
109103 req .Header .Add ("Content-Type" , "application/json" )
110104
111105 res , err := c .httpClient .Do (req )
112-
113106 if err != nil {
114107 return err
115- } else if res .StatusCode != 200 {
116- return fmt .Errorf ("Status %d: %s" , res .StatusCode , ERROR_AUTHENTICATION_FAILURE )
117108 }
109+ defer func () {
110+ _ = res .Body .Close ()
111+ }()
118112
119- defer res .Body .Close ()
113+ if res .StatusCode != 200 {
114+ return fmt .Errorf ("login failed with status %d, check api key and secret" , res .StatusCode )
115+ }
120116
121117 data , err := io .ReadAll (res .Body )
122118 if err != nil {
@@ -142,53 +138,6 @@ func (c *MeshStackProviderClient) ensureValidToken() error {
142138 return nil
143139}
144140
145- // nolint: unused
146- func (c * MeshStackProviderClient ) lookUpEndpoints () error {
147- if c .ensureValidToken () != nil {
148- return errors .New (ERROR_AUTHENTICATION_FAILURE )
149- }
150-
151- meshObjectsPath , err := url .JoinPath (c .url .String (), apiMeshObjectsRoot )
152- if err != nil {
153- return err
154- }
155- meshObjects , _ := url .Parse (meshObjectsPath )
156-
157- res , err := c .httpClient .Do (
158- & http.Request {
159- URL : meshObjects ,
160- Method : "GET" ,
161- Header : http.Header {
162- "Authorization" : {c .token },
163- },
164- },
165- )
166-
167- if err != nil {
168- return errors .New (ERROR_GENERIC_CLIENT_ERROR )
169- }
170-
171- defer res .Body .Close ()
172-
173- if res .StatusCode != 200 {
174- return errors .New (ERROR_AUTHENTICATION_FAILURE )
175- }
176-
177- data , err := io .ReadAll (res .Body )
178- if err != nil {
179- return err
180- }
181-
182- var endpoints endpoints
183- err = json .Unmarshal (data , & endpoints )
184- if err != nil {
185- return err
186- }
187-
188- c .endpoints = endpoints
189- return nil
190- }
191-
192141func (c * MeshStackProviderClient ) doAuthenticatedRequest (req * http.Request ) (* http.Response , error ) {
193142 // ensure that headeres are initialized
194143 if req .Header == nil {
@@ -224,18 +173,20 @@ func (c *MeshStackProviderClient) deleteMeshObject(targetUrl url.URL, expectedSt
224173 res , err := c .doAuthenticatedRequest (req )
225174
226175 if err != nil {
227- return errors . New ( ERROR_GENERIC_CLIENT_ERROR )
176+ return fmt . Errorf ( "cannot authenticate for delete request: %w " , err )
228177 }
229178
230- defer res .Body .Close ()
179+ defer func () {
180+ _ = res .Body .Close ()
181+ }()
231182
232183 data , err := io .ReadAll (res .Body )
233184 if err != nil {
234185 return err
235186 }
236187
237188 if res .StatusCode != expectedStatus {
238- return fmt .Errorf ("unexpected status code: %d, %s " , res .StatusCode , data )
189+ return fmt .Errorf ("expected status code %d, but got %d, body: '%s' " , expectedStatus , res .StatusCode , string ( data ) )
239190 }
240191
241192 return nil
0 commit comments