github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/service_client.go (about) 1 package golangsdk 2 3 import ( 4 "io" 5 "net/http" 6 "strings" 7 ) 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 { 12 // ProviderClient is a reference to the provider that implements this service. 13 *ProviderClient 14 15 // Endpoint is the base URL of the service's API, acquired from a service catalog. 16 // It MUST end with a /. 17 Endpoint string 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 23 24 // This is the service client type (e.g. compute, sharev2). 25 // NOTE: FOR INTERNAL USE ONLY. DO NOT SET. GOPHERCLOUD WILL SET THIS. 26 // It is only exported because it gets set in a different package. 27 Type string 28 29 // The microversion of the service to use. Set this to use a particular microversion. 30 Microversion string 31 32 // MoreHeaders allows users to set service-wide headers on requests. Put another way, 33 // values set in this field will be set on all the HTTP requests the service client sends. 34 MoreHeaders map[string]string 35 } 36 37 // ResourceBaseURL returns the base URL of any resources used by this service. It MUST end with a /. 38 func (client *ServiceClient) ResourceBaseURL() string { 39 if client.ResourceBase != "" { 40 return client.ResourceBase 41 } 42 return client.Endpoint 43 } 44 45 // ServiceURL constructs a URL for a resource belonging to this provider. 46 func (client *ServiceClient) ServiceURL(parts ...string) string { 47 return client.ResourceBaseURL() + strings.Join(parts, "/") 48 } 49 50 func (client *ServiceClient) initReqOpts(_ string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) { 51 if v, ok := (JSONBody).(io.Reader); ok { 52 opts.RawBody = v 53 } else if JSONBody != nil { 54 opts.JSONBody = JSONBody 55 } 56 57 if JSONResponse != nil { 58 opts.JSONResponse = JSONResponse 59 } 60 61 if opts.MoreHeaders == nil { 62 opts.MoreHeaders = make(map[string]string) 63 } 64 65 if client.Microversion != "" { 66 client.setMicroversionHeader(opts) 67 } 68 } 69 70 // Get calls `Request` with the "GET" HTTP verb. Def 200 71 // JSONResponse Deprecated 72 func (client *ServiceClient) Get(url string, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { 73 if opts == nil { 74 opts = new(RequestOpts) 75 } 76 client.initReqOpts(url, nil, JSONResponse, opts) 77 return client.Request("GET", url, opts) 78 } 79 80 // Post calls `Request` with the "POST" HTTP verb. Def 201, 202 81 // JSONResponse Deprecated 82 func (client *ServiceClient) Post(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { 83 if opts == nil { 84 opts = new(RequestOpts) 85 } 86 client.initReqOpts(url, JSONBody, JSONResponse, opts) 87 return client.Request("POST", url, opts) 88 } 89 90 // Put calls `Request` with the "PUT" HTTP verb. Def 201, 202 91 // JSONResponse Deprecated 92 func (client *ServiceClient) Put(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { 93 if opts == nil { 94 opts = new(RequestOpts) 95 } 96 client.initReqOpts(url, JSONBody, JSONResponse, opts) 97 return client.Request("PUT", url, opts) 98 } 99 100 // Patch calls `Request` with the "PATCH" HTTP verb. Def 200, 204 101 // JSONResponse Deprecated 102 func (client *ServiceClient) Patch(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { 103 if opts == nil { 104 opts = new(RequestOpts) 105 } 106 client.initReqOpts(url, JSONBody, JSONResponse, opts) 107 return client.Request("PATCH", url, opts) 108 } 109 110 // Delete calls `Request` with the "DELETE" HTTP verb. Def 202, 204 111 func (client *ServiceClient) Delete(url string, opts *RequestOpts) (*http.Response, error) { 112 if opts == nil { 113 opts = new(RequestOpts) 114 } 115 client.initReqOpts(url, nil, nil, opts) 116 return client.Request("DELETE", url, opts) 117 } 118 119 // Head calls `Request` with the "HEAD" HTTP verb. Def 204, 206 120 func (client *ServiceClient) Head(url string, opts *RequestOpts) (*http.Response, error) { 121 if opts == nil { 122 opts = new(RequestOpts) 123 } 124 client.initReqOpts(url, nil, nil, opts) 125 return client.Request("HEAD", url, opts) 126 } 127 128 // DeleteWithBody calls `Request` with the "DELETE" HTTP verb. Def 202, 204 129 func (client *ServiceClient) DeleteWithBody(url string, JSONBody interface{}, opts *RequestOpts) (*http.Response, error) { 130 if opts == nil { 131 opts = new(RequestOpts) 132 } 133 client.initReqOpts(url, JSONBody, nil, opts) 134 return client.Request("DELETE", url, opts) 135 } 136 137 // DeleteWithResponse calls `Request` with the "DELETE" HTTP verb. Def 202, 204 138 // Deprecated 139 func (client *ServiceClient) DeleteWithResponse(url string, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { 140 if opts == nil { 141 opts = new(RequestOpts) 142 } 143 client.initReqOpts(url, nil, JSONResponse, opts) 144 return client.Request("DELETE", url, opts) 145 } 146 147 // DeleteWithBodyResp calls `Request` with the "DELETE" HTTP verb. Def 202, 204 148 // Deprecated 149 func (client *ServiceClient) DeleteWithBodyResp(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { 150 if opts == nil { 151 opts = new(RequestOpts) 152 } 153 client.initReqOpts(url, JSONBody, JSONResponse, opts) 154 return client.Request("DELETE", url, opts) 155 } 156 157 func (client *ServiceClient) setMicroversionHeader(opts *RequestOpts) { 158 switch client.Type { 159 case "compute": 160 opts.MoreHeaders["X-OpenStack-Nova-API-Version"] = client.Microversion 161 case "sharev2": 162 opts.MoreHeaders["X-OpenStack-Manila-API-Version"] = client.Microversion 163 case "volume": 164 opts.MoreHeaders["X-OpenStack-Volume-API-Version"] = client.Microversion 165 } 166 167 if client.Type != "" { 168 opts.MoreHeaders["OpenStack-API-Version"] = client.Type + " " + client.Microversion 169 } 170 } 171 172 // Request carries out the HTTP operation for the service client 173 func (client *ServiceClient) Request(method, url string, options *RequestOpts) (*http.Response, error) { 174 if len(client.MoreHeaders) > 0 { 175 if options == nil { 176 options = new(RequestOpts) 177 } 178 for k, v := range client.MoreHeaders { 179 options.MoreHeaders[k] = v 180 } 181 } 182 return client.ProviderClient.Request(method, url, options) 183 }