blob: 8546625bee34ca97ee320108d63d3a97ec073b88 [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 }
48 return client.Request("GET", url, opts)
49}
50
51// Post calls `Request` with the "POST" HTTP verb.
52func (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.
71func (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.
90func (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.
109func (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}