@@ -17,11 +17,11 @@ import (
1717)
1818
1919const (
20- libraryVersion = "v3"
20+ // libraryVersion = "v3"
2121 defaultBaseURL = "https://%s/"
22- absolutePath = "api/nutanix/" + libraryVersion
23- userAgent = "nutanix/" + libraryVersion
24- mediaType = "application/json"
22+ // absolutePath = "api/nutanix/" + libraryVersion
23+ // userAgent = "nutanix/" + libraryVersion
24+ mediaType = "application/json"
2525)
2626
2727// Client Config Configuration of the client
@@ -41,6 +41,9 @@ type Client struct {
4141
4242 // Optional function called after every successful request made.
4343 onRequestCompleted RequestCompletionCallback
44+
45+ // absolutePath: for example api/nutanix/v3
46+ AbsolutePath string
4447}
4548
4649// RequestCompletionCallback defines the type of the request callback function
@@ -59,7 +62,14 @@ type Credentials struct {
5962}
6063
6164// NewClient returns a new Nutanix API client.
62- func NewClient (credentials * Credentials ) (* Client , error ) {
65+ func NewClient (credentials * Credentials , userAgent string , absolutePath string ) (* Client , error ) {
66+ if userAgent == "" {
67+ return nil , fmt .Errorf ("userAgent argument must be passed" )
68+ }
69+ if absolutePath == "" {
70+ return nil , fmt .Errorf ("absolutePath argument must be passed" )
71+ }
72+
6373 transCfg := & http.Transport {
6474 // nolint:gas
6575 TLSClientConfig : & tls.Config {InsecureSkipVerify : credentials .Insecure }, // ignore expired SSL certificates
@@ -85,7 +95,7 @@ func NewClient(credentials *Credentials) (*Client, error) {
8595 return nil , err
8696 }
8797
88- c := & Client {credentials , httpClient , baseURL , userAgent , nil , nil }
98+ c := & Client {credentials , httpClient , baseURL , userAgent , nil , nil , absolutePath }
8999
90100 if credentials .SessionAuth {
91101 log .Printf ("[DEBUG] Using session_auth\n " )
@@ -117,7 +127,7 @@ func NewClient(credentials *Credentials) (*Client, error) {
117127
118128// NewRequest creates a request
119129func (c * Client ) NewRequest (ctx context.Context , method , urlStr string , body interface {}) (* http.Request , error ) {
120- rel , errp := url .Parse (absolutePath + urlStr )
130+ rel , errp := url .Parse (c . AbsolutePath + urlStr )
121131 if errp != nil {
122132 return nil , errp
123133 }
@@ -156,7 +166,7 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int
156166
157167// NewUploadRequest Handles image uploads for image service
158168func (c * Client ) NewUploadRequest (ctx context.Context , method , urlStr string , body []byte ) (* http.Request , error ) {
159- rel , errp := url .Parse (absolutePath + urlStr )
169+ rel , errp := url .Parse (c . AbsolutePath + urlStr )
160170 if errp != nil {
161171 return nil , errp
162172 }
@@ -211,6 +221,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) error
211221 _ , err = io .Copy (w , resp .Body )
212222 if err != nil {
213223 fmt .Printf ("Error io.Copy %s" , err )
224+
214225 return err
215226 }
216227 } else {
@@ -251,7 +262,6 @@ func CheckResponse(r *http.Response) error {
251262 rdr2 := ioutil .NopCloser (bytes .NewBuffer (buf ))
252263
253264 r .Body = rdr2
254-
255265 // if has entities -> return nil
256266 // if has message_list -> check_error["state"]
257267 // if has status -> check_error["status.state"]
@@ -264,9 +274,9 @@ func CheckResponse(r *http.Response) error {
264274 if err != nil {
265275 return fmt .Errorf ("unmarshalling error response %s" , err )
266276 }
277+ log .Print ("[DEBUG] after json.Unmarshal" )
267278
268279 errRes := & ErrorResponse {}
269-
270280 if status , ok := res ["status" ]; ok {
271281 _ , sok := status .(string )
272282 if sok {
@@ -280,14 +290,25 @@ func CheckResponse(r *http.Response) error {
280290 return nil
281291 }
282292
293+ log .Print ("[DEBUG] after bunch of switch cases" )
283294 if err != nil {
284295 return err
285296 }
297+ log .Print ("[DEBUG] first nil check" )
286298
299+ // karbon error check
300+ if messageInfo , ok := res ["message_info" ]; ok {
301+ return fmt .Errorf ("error: %s" , messageInfo )
302+ }
303+ if message , ok := res ["message" ]; ok {
304+ log .Print (message )
305+ return fmt .Errorf ("error: %s" , message )
306+ }
287307 if errRes .State != "ERROR" {
288308 return nil
289309 }
290310
311+ log .Print ("[DEBUG] after errRes.State" )
291312 pretty , _ := json .MarshalIndent (errRes , "" , " " )
292313 return fmt .Errorf ("error: %s" , string (pretty ))
293314}
@@ -319,6 +340,7 @@ func (r *ErrorResponse) Error() string {
319340 for key , value := range r .MessageList {
320341 err = fmt .Sprintf ("%d: {message:%s, reason:%s }" , key , value .Message , value .Reason )
321342 }
343+
322344 return err
323345}
324346
@@ -327,5 +349,6 @@ func fillStruct(data map[string]interface{}, result interface{}) error {
327349 if err != nil {
328350 return err
329351 }
352+
330353 return json .Unmarshal (j , result )
331354}
0 commit comments