Skip to content

Commit

Permalink
move HTTP action methods to ServiceClient (for microversions)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Perritt committed Mar 10, 2016
1 parent c0dd8e5 commit 90d31ce
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 93 deletions.
94 changes: 2 additions & 92 deletions provider_client.go
Expand Up @@ -104,27 +104,14 @@ type RequestOpts struct {
ErrorContext error
}

func (r *RequestOpts) ToRequestOpts() (*RequestOpts, error) {
return r, nil
}

type RequestOptsBuilder interface {
ToRequestOpts() (*RequestOpts, error)
}

var applicationJSON = "application/json"

// Request performs an HTTP request using the ProviderClient's current HTTPClient. An authentication
// header will automatically be provided.
func (client *ProviderClient) Request(method, url string, optsBuilder RequestOptsBuilder) (*http.Response, error) {
func (client *ProviderClient) Request(method, url string, options *RequestOpts) (*http.Response, error) {
var body io.Reader
var contentType *string

options, err := optsBuilder.ToRequestOpts()
if err != nil {
return nil, err
}

// Derive the content body by either encoding an arbitrary object as JSON, or by taking a provided
// io.ReadSeeker as-is. Default the content-type to application/json.
if options.JSONBody != nil {
Expand Down Expand Up @@ -211,7 +198,7 @@ func (client *ProviderClient) Request(method, url string, optsBuilder RequestOpt
Actual: resp.StatusCode,
Body: body,
}
respErr.Function = "gophercloud.ProviderClient.Request"
//respErr.Function = "gophercloud.ProviderClient.Request"

errType := options.ErrorContext
switch resp.StatusCode {
Expand Down Expand Up @@ -318,80 +305,3 @@ func defaultOkCodes(method string) []int {

return []int{}
}

// Get calls `Request` with the "GET" HTTP verb.
func (client *ProviderClient) Get(url string, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}
if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}
return client.Request("GET", url, opts)
}

// Post calls `Request` with the "POST" HTTP verb.
func (client *ProviderClient) Post(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

if v, ok := (JSONBody).(io.ReadSeeker); ok {
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}

if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}

return client.Request("POST", url, opts)
}

// Put calls `Request` with the "PUT" HTTP verb.
func (client *ProviderClient) Put(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

if v, ok := (JSONBody).(io.ReadSeeker); ok {
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}

if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}

return client.Request("PUT", url, opts)
}

// Patch calls `Request` with the "PATCH" HTTP verb.
func (client *ProviderClient) Patch(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

if v, ok := (JSONBody).(io.ReadSeeker); ok {
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}

if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}

return client.Request("PATCH", url, opts)
}

// Delete calls `Request` with the "DELETE" HTTP verb.
func (client *ProviderClient) Delete(url string, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

return client.Request("DELETE", url, opts)
}
83 changes: 82 additions & 1 deletion service_client.go
@@ -1,6 +1,10 @@
package gophercloud

import "strings"
import (
"io"
"net/http"
"strings"
)

// ServiceClient stores details required to interact with a specific service API implemented by a provider.
// Generally, you'll acquire these by calling the appropriate `New` method on a ProviderClient.
Expand Down Expand Up @@ -32,3 +36,80 @@ func (client *ServiceClient) ResourceBaseURL() string {
func (client *ServiceClient) ServiceURL(parts ...string) string {
return client.ResourceBaseURL() + strings.Join(parts, "/")
}

// Get calls `Request` with the "GET" HTTP verb.
func (client *ServiceClient) Get(url string, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}
if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}
return client.Request("GET", url, opts)
}

// Post calls `Request` with the "POST" HTTP verb.
func (client *ServiceClient) Post(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

if v, ok := (JSONBody).(io.Reader); ok {
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}

if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}

return client.Request("POST", url, opts)
}

// Put calls `Request` with the "PUT" HTTP verb.
func (client *ServiceClient) Put(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

if v, ok := (JSONBody).(io.Reader); ok {
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}

if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}

return client.Request("PUT", url, opts)
}

// Patch calls `Request` with the "PATCH" HTTP verb.
func (client *ServiceClient) Patch(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

if v, ok := (JSONBody).(io.Reader); ok {
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}

if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}

return client.Request("PATCH", url, opts)
}

// Delete calls `Request` with the "DELETE" HTTP verb.
func (client *ServiceClient) Delete(url string, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}

return client.Request("DELETE", url, opts)
}

0 comments on commit 90d31ce

Please sign in to comment.