github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/request.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     9  )
    10  
    11  // requestOptions contains all the options to create an HTTP request.
    12  type requestOptions struct {
    13  	// URIParams are the list URI route parameters
    14  	URIParams internal.Params
    15  
    16  	// Query is a list of HTTP query parameters. Query will overwrite any
    17  	// existing query string in the URI. If you want to preserve the query
    18  	// string in URI make sure Query is nil.
    19  	Query []Query
    20  
    21  	// RequestName is the name of the request (see routes)
    22  	RequestName string
    23  
    24  	// Method is the HTTP method.
    25  	Method string
    26  	// URL is the request path.
    27  	URL string
    28  	// Body is the content of the request.
    29  	Body io.ReadSeeker
    30  }
    31  
    32  // newHTTPRequest returns a constructed HTTP.Request with some defaults.
    33  // Defaults are applied when Request options are not filled in.
    34  func (client *Client) newHTTPRequest(passedRequest requestOptions) (*cloudcontroller.Request, error) {
    35  	var request *http.Request
    36  	var err error
    37  
    38  	if passedRequest.URL != "" {
    39  		request, err = http.NewRequest(
    40  			passedRequest.Method,
    41  			passedRequest.URL,
    42  			passedRequest.Body,
    43  		)
    44  	} else {
    45  		request, err = client.router.CreateRequest(
    46  			passedRequest.RequestName,
    47  			map[string]string(passedRequest.URIParams),
    48  			passedRequest.Body,
    49  		)
    50  	}
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	if passedRequest.Query != nil {
    56  		request.URL.RawQuery = FormatQueryParameters(passedRequest.Query).Encode()
    57  	}
    58  
    59  	request.Header = http.Header{}
    60  	request.Header.Set("Accept", "application/json")
    61  	request.Header.Set("User-Agent", client.userAgent)
    62  
    63  	if passedRequest.Body != nil {
    64  		request.Header.Set("Content-Type", "application/json")
    65  	}
    66  
    67  	return cloudcontroller.NewRequest(request, passedRequest.Body), nil
    68  }