Debian Codegraph @

api.go

raw symbols SCIP
1 /*
2 * Copyright 2014 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
3 */
4
5 // Package govcloudair provides a simple binding for vCloud Air / vCloud Director REST APIs.
6 package govcd
7
8 import (
9 "encoding/xml"
10 "fmt"
11 "io"
12 "io/ioutil"
13 "net/http"
14 "net/url"
15
16 types "github.com/hmrc/vmware-govcd/types/v56"
17 )
18
19 // Client provides a client to vCloud Air, values can be populated automatically using the Authenticate method.
20 type Client struct {
21 APIVersion string // The API version required
22 VCDToken string // Access Token (authorization header)
23 VCDAuthHeader string // Authorization header
24 VCDVDCHREF url.URL // HREF of the backend VDC you're using
25 Http http.Client // HttpClient is the client to use. Default will be used if not provided.
26 }
27
28 // NewRequest creates a new HTTP request and applies necessary auth headers if
29 // set.
30 func (c *Client) NewRequest(params map[string]string, method string, u url.URL, body io.Reader) *http.Request {
31
32 p := url.Values{}
33
34 // Build up our request parameters
35 for k, v := range params {
36 p.Add(k, v)
37 }
38
39 // Add the params to our URL
40 u.RawQuery = p.Encode()
41
42 // Build the request, no point in checking for errors here as we're just
43 // passing a string version of an url.URL struct and http.NewRequest returns
44 // error only if can't process an url.ParseRequestURI().
45 req, _ := http.NewRequest(method, u.String(), body)
46
47 if c.VCDAuthHeader != "" && c.VCDToken != "" {
48 // Add the authorization header
49 req.Header.Add(c.VCDAuthHeader, c.VCDToken)
50 // Add the Accept header for VCD
51 req.Header.Add("Accept", "application/*+xml;version="+c.APIVersion)
52 }
53
54 return req
55
56 }
57
58 // parseErr takes an error XML resp and returns a single string for use in error messages.
59 func parseErr(resp *http.Response) error {
60
61 errBody := new(types.Error)
62
63 // if there was an error decoding the body, just return that
64 if err := decodeBody(resp, errBody); err != nil {
65 return fmt.Errorf("error parsing error body for non-200 request: %s", err)
66 }
67
68 return fmt.Errorf("API Error: %d: %s", errBody.MajorErrorCode, errBody.Message)
69 }
70
71 // decodeBody is used to XML decode a response body
72 func decodeBody(resp *http.Response, out interface{}) error {
73
74 body, err := ioutil.ReadAll(resp.Body)
75 if err != nil {
76 return err
77 }
78
79 // Unmarshal the XML.
80 if err = xml.Unmarshal(body, &out); err != nil {
81 return err
82 }
83
84 return nil
85 }
86
87 // checkResp wraps http.Client.Do() and verifies the request, if status code
88 // is 2XX it passes back the response, if it's a known invalid status code it
89 // parses the resultant XML error and returns a descriptive error, if the
90 // status code is not handled it returns a generic error with the status code.
91 func checkResp(resp *http.Response, err error) (*http.Response, error) {
92 if err != nil {
93 return resp, err
94 }
95
96 switch i := resp.StatusCode; {
97 // Valid request, return the response.
98 case i == 200 || i == 201 || i == 202 || i == 204:
99 return resp, nil
100 // Invalid request, parse the XML error returned and return it.
101 case i == 400 || i == 401 || i == 403 || i == 404 || i == 405 || i == 406 || i == 409 || i == 415 || i == 500 || i == 503 || i == 504:
102 return nil, parseErr(resp)
103 // Unhandled response.
104 default:
105 return nil, fmt.Errorf("unhandled API response, please report this issue, status code: %s", resp.Status)
106 }
107 }