blob: 7484c67e57b01a748764358367550376cc1888bc [file] [log] [blame]
Ash Wilson6425a412014-08-29 12:30:35 -04001package gophercloud
2
Jon Perritt90d31ce2016-03-10 02:48:46 -06003import (
4 "io"
5 "net/http"
6 "strings"
7)
Ash Wilson6425a412014-08-29 12:30:35 -04008
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.
11type ServiceClient struct {
Ash Wilson55bbaaf2014-10-22 09:06:46 -040012 // ProviderClient is a reference to the provider that implements this service.
13 *ProviderClient
Ash Wilson6425a412014-08-29 12:30:35 -040014
15 // Endpoint is the base URL of the service's API, acquired from a service catalog.
Ash Wilsoned6a1d82014-09-03 12:01:00 -040016 // It MUST end with a /.
Ash Wilson6425a412014-08-29 12:30:35 -040017 Endpoint string
Ash Wilson42380322014-10-06 14:21:46 -040018
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 Perrittc0dd8e52016-03-10 01:20:27 -060023
24 Microversion string
Ash Wilson42380322014-10-06 14:21:46 -040025}
26
27// ResourceBaseURL returns the base URL of any resources used by this service. It MUST end with a /.
28func (client *ServiceClient) ResourceBaseURL() string {
29 if client.ResourceBase != "" {
30 return client.ResourceBase
31 }
32 return client.Endpoint
Ash Wilson6425a412014-08-29 12:30:35 -040033}
34
35// ServiceURL constructs a URL for a resource belonging to this provider.
36func (client *ServiceClient) ServiceURL(parts ...string) string {
Ash Wilson42380322014-10-06 14:21:46 -040037 return client.ResourceBaseURL() + strings.Join(parts, "/")
38}
Jon Perritt90d31ce2016-03-10 02:48:46 -060039
40// Get calls `Request` with the "GET" HTTP verb.
41func (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 Perrittdb0ae142016-03-13 00:33:41 -060048
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 Perritt90d31ce2016-03-10 02:48:46 -060054 return client.Request("GET", url, opts)
55}
56
57// Post calls `Request` with the "POST" HTTP verb.
58func (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 Perrittdb0ae142016-03-13 00:33:41 -060073 if opts.MoreHeaders == nil {
74 opts.MoreHeaders = make(map[string]string)
75 }
76 opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion
77
Jon Perritt90d31ce2016-03-10 02:48:46 -060078 return client.Request("POST", url, opts)
79}
80
81// Put calls `Request` with the "PUT" HTTP verb.
82func (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 Perrittdb0ae142016-03-13 00:33:41 -060097 if opts.MoreHeaders == nil {
98 opts.MoreHeaders = make(map[string]string)
99 }
100 opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion
101
Jon Perritt90d31ce2016-03-10 02:48:46 -0600102 return client.Request("PUT", url, opts)
103}
104
105// Patch calls `Request` with the "PATCH" HTTP verb.
106func (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 Perrittdb0ae142016-03-13 00:33:41 -0600121 if opts.MoreHeaders == nil {
122 opts.MoreHeaders = make(map[string]string)
123 }
124 opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion
125
Jon Perritt90d31ce2016-03-10 02:48:46 -0600126 return client.Request("PATCH", url, opts)
127}
128
129// Delete calls `Request` with the "DELETE" HTTP verb.
130func (client *ServiceClient) Delete(url string, opts *RequestOpts) (*http.Response, error) {
131 if opts == nil {
132 opts = &RequestOpts{}
133 }
134
Jon Perrittdb0ae142016-03-13 00:33:41 -0600135 if opts.MoreHeaders == nil {
136 opts.MoreHeaders = make(map[string]string)
137 }
138 opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion
139
Jon Perritt90d31ce2016-03-10 02:48:46 -0600140 return client.Request("DELETE", url, opts)
141}