Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
Jon Perritt | 90d31ce | 2016-03-10 02:48:46 -0600 | [diff] [blame] | 3 | import ( |
| 4 | "io" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | ) |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 8 | |
| 9 | // ServiceClient stores details required to interact with a specific service API implemented by a provider. |
| 10 | // Generally, you'll acquire these by calling the appropriate `New` method on a ProviderClient. |
| 11 | type ServiceClient struct { |
Ash Wilson | 55bbaaf | 2014-10-22 09:06:46 -0400 | [diff] [blame] | 12 | // ProviderClient is a reference to the provider that implements this service. |
| 13 | *ProviderClient |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 14 | |
| 15 | // Endpoint is the base URL of the service's API, acquired from a service catalog. |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame] | 16 | // It MUST end with a /. |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 17 | Endpoint string |
Ash Wilson | 4238032 | 2014-10-06 14:21:46 -0400 | [diff] [blame] | 18 | |
| 19 | // ResourceBase is the base URL shared by the resources within a service's API. It should include |
| 20 | // the API version and, like Endpoint, MUST end with a / if set. If not set, the Endpoint is used |
| 21 | // as-is, instead. |
| 22 | ResourceBase string |
Jon Perritt | c0dd8e5 | 2016-03-10 01:20:27 -0600 | [diff] [blame] | 23 | |
| 24 | Microversion string |
Ash Wilson | 4238032 | 2014-10-06 14:21:46 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // ResourceBaseURL returns the base URL of any resources used by this service. It MUST end with a /. |
| 28 | func (client *ServiceClient) ResourceBaseURL() string { |
| 29 | if client.ResourceBase != "" { |
| 30 | return client.ResourceBase |
| 31 | } |
| 32 | return client.Endpoint |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // ServiceURL constructs a URL for a resource belonging to this provider. |
| 36 | func (client *ServiceClient) ServiceURL(parts ...string) string { |
Ash Wilson | 4238032 | 2014-10-06 14:21:46 -0400 | [diff] [blame] | 37 | return client.ResourceBaseURL() + strings.Join(parts, "/") |
| 38 | } |
Jon Perritt | 90d31ce | 2016-03-10 02:48:46 -0600 | [diff] [blame] | 39 | |
| 40 | // Get calls `Request` with the "GET" HTTP verb. |
| 41 | func (client *ServiceClient) Get(url string, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
| 42 | if opts == nil { |
| 43 | opts = &RequestOpts{} |
| 44 | } |
| 45 | if JSONResponse != nil { |
| 46 | opts.JSONResponse = JSONResponse |
| 47 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 48 | |
| 49 | if opts.MoreHeaders == nil { |
| 50 | opts.MoreHeaders = make(map[string]string) |
| 51 | } |
| 52 | opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion |
| 53 | |
Jon Perritt | 90d31ce | 2016-03-10 02:48:46 -0600 | [diff] [blame] | 54 | return client.Request("GET", url, opts) |
| 55 | } |
| 56 | |
| 57 | // Post calls `Request` with the "POST" HTTP verb. |
| 58 | func (client *ServiceClient) Post(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
| 59 | if opts == nil { |
| 60 | opts = &RequestOpts{} |
| 61 | } |
| 62 | |
| 63 | if v, ok := (JSONBody).(io.Reader); ok { |
| 64 | opts.RawBody = v |
| 65 | } else if JSONBody != nil { |
| 66 | opts.JSONBody = JSONBody |
| 67 | } |
| 68 | |
| 69 | if JSONResponse != nil { |
| 70 | opts.JSONResponse = JSONResponse |
| 71 | } |
| 72 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 73 | if opts.MoreHeaders == nil { |
| 74 | opts.MoreHeaders = make(map[string]string) |
| 75 | } |
| 76 | opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion |
| 77 | |
Jon Perritt | 90d31ce | 2016-03-10 02:48:46 -0600 | [diff] [blame] | 78 | return client.Request("POST", url, opts) |
| 79 | } |
| 80 | |
| 81 | // Put calls `Request` with the "PUT" HTTP verb. |
| 82 | func (client *ServiceClient) Put(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
| 83 | if opts == nil { |
| 84 | opts = &RequestOpts{} |
| 85 | } |
| 86 | |
| 87 | if v, ok := (JSONBody).(io.Reader); ok { |
| 88 | opts.RawBody = v |
| 89 | } else if JSONBody != nil { |
| 90 | opts.JSONBody = JSONBody |
| 91 | } |
| 92 | |
| 93 | if JSONResponse != nil { |
| 94 | opts.JSONResponse = JSONResponse |
| 95 | } |
| 96 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 97 | if opts.MoreHeaders == nil { |
| 98 | opts.MoreHeaders = make(map[string]string) |
| 99 | } |
| 100 | opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion |
| 101 | |
Jon Perritt | 90d31ce | 2016-03-10 02:48:46 -0600 | [diff] [blame] | 102 | return client.Request("PUT", url, opts) |
| 103 | } |
| 104 | |
| 105 | // Patch calls `Request` with the "PATCH" HTTP verb. |
| 106 | func (client *ServiceClient) Patch(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
| 107 | if opts == nil { |
| 108 | opts = &RequestOpts{} |
| 109 | } |
| 110 | |
| 111 | if v, ok := (JSONBody).(io.Reader); ok { |
| 112 | opts.RawBody = v |
| 113 | } else if JSONBody != nil { |
| 114 | opts.JSONBody = JSONBody |
| 115 | } |
| 116 | |
| 117 | if JSONResponse != nil { |
| 118 | opts.JSONResponse = JSONResponse |
| 119 | } |
| 120 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 121 | if opts.MoreHeaders == nil { |
| 122 | opts.MoreHeaders = make(map[string]string) |
| 123 | } |
| 124 | opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion |
| 125 | |
Jon Perritt | 90d31ce | 2016-03-10 02:48:46 -0600 | [diff] [blame] | 126 | return client.Request("PATCH", url, opts) |
| 127 | } |
| 128 | |
| 129 | // Delete calls `Request` with the "DELETE" HTTP verb. |
| 130 | func (client *ServiceClient) Delete(url string, opts *RequestOpts) (*http.Response, error) { |
| 131 | if opts == nil { |
| 132 | opts = &RequestOpts{} |
| 133 | } |
| 134 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 135 | if opts.MoreHeaders == nil { |
| 136 | opts.MoreHeaders = make(map[string]string) |
| 137 | } |
| 138 | opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion |
| 139 | |
Jon Perritt | 90d31ce | 2016-03-10 02:48:46 -0600 | [diff] [blame] | 140 | return client.Request("DELETE", url, opts) |
| 141 | } |