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 | } |
| 48 | return client.Request("GET", url, opts) |
| 49 | } |
| 50 | |
| 51 | // Post calls `Request` with the "POST" HTTP verb. |
| 52 | func (client *ServiceClient) Post(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
| 53 | if opts == nil { |
| 54 | opts = &RequestOpts{} |
| 55 | } |
| 56 | |
| 57 | if v, ok := (JSONBody).(io.Reader); ok { |
| 58 | opts.RawBody = v |
| 59 | } else if JSONBody != nil { |
| 60 | opts.JSONBody = JSONBody |
| 61 | } |
| 62 | |
| 63 | if JSONResponse != nil { |
| 64 | opts.JSONResponse = JSONResponse |
| 65 | } |
| 66 | |
| 67 | return client.Request("POST", url, opts) |
| 68 | } |
| 69 | |
| 70 | // Put calls `Request` with the "PUT" HTTP verb. |
| 71 | func (client *ServiceClient) Put(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
| 72 | if opts == nil { |
| 73 | opts = &RequestOpts{} |
| 74 | } |
| 75 | |
| 76 | if v, ok := (JSONBody).(io.Reader); ok { |
| 77 | opts.RawBody = v |
| 78 | } else if JSONBody != nil { |
| 79 | opts.JSONBody = JSONBody |
| 80 | } |
| 81 | |
| 82 | if JSONResponse != nil { |
| 83 | opts.JSONResponse = JSONResponse |
| 84 | } |
| 85 | |
| 86 | return client.Request("PUT", url, opts) |
| 87 | } |
| 88 | |
| 89 | // Patch calls `Request` with the "PATCH" HTTP verb. |
| 90 | func (client *ServiceClient) Patch(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
| 91 | if opts == nil { |
| 92 | opts = &RequestOpts{} |
| 93 | } |
| 94 | |
| 95 | if v, ok := (JSONBody).(io.Reader); ok { |
| 96 | opts.RawBody = v |
| 97 | } else if JSONBody != nil { |
| 98 | opts.JSONBody = JSONBody |
| 99 | } |
| 100 | |
| 101 | if JSONResponse != nil { |
| 102 | opts.JSONResponse = JSONResponse |
| 103 | } |
| 104 | |
| 105 | return client.Request("PATCH", url, opts) |
| 106 | } |
| 107 | |
| 108 | // Delete calls `Request` with the "DELETE" HTTP verb. |
| 109 | func (client *ServiceClient) Delete(url string, opts *RequestOpts) (*http.Response, error) { |
| 110 | if opts == nil { |
| 111 | opts = &RequestOpts{} |
| 112 | } |
| 113 | |
| 114 | return client.Request("DELETE", url, opts) |
| 115 | } |