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

     1  package uaa
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"code.cloudfoundry.org/cli/api/uaa/internal"
     9  )
    10  
    11  // RequestOptions contains all the options to create an HTTP Request.
    12  type requestOptions struct {
    13  	// Header is the set of request headers
    14  	Header http.Header
    15  
    16  	// URIParams are the list URI route parameters
    17  	URIParams internal.Params
    18  
    19  	// Query is a list of HTTP query parameters
    20  	Query url.Values
    21  
    22  	// RequestName is the name of the request (see routes)
    23  	RequestName string
    24  
    25  	// Method is the HTTP method.
    26  	Method string
    27  	// URL is the request path.
    28  	URL string
    29  	// Body is the request body
    30  	Body io.Reader
    31  }
    32  
    33  // newRequest returns a constructed http.Request with some defaults. The
    34  // request will terminate the connection after it is sent (via a 'Connection:
    35  // close' header).
    36  func (client *Client) newRequest(passedRequest requestOptions) (*http.Request, error) {
    37  	var request *http.Request
    38  	var err error
    39  
    40  	if passedRequest.URL != "" {
    41  		request, err = http.NewRequest(
    42  			passedRequest.Method,
    43  			passedRequest.URL,
    44  			passedRequest.Body,
    45  		)
    46  	} else {
    47  		request, err = client.router.CreateRequest(
    48  			passedRequest.RequestName,
    49  			passedRequest.URIParams,
    50  			passedRequest.Body,
    51  		)
    52  	}
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	if passedRequest.Query != nil {
    58  		request.URL.RawQuery = passedRequest.Query.Encode()
    59  	}
    60  
    61  	if passedRequest.Header != nil {
    62  		request.Header = passedRequest.Header
    63  	} else {
    64  		request.Header = http.Header{}
    65  	}
    66  	request.Header.Set("Accept", "application/json")
    67  	request.Header.Set("Connection", "close")
    68  	request.Header.Set("User-Agent", client.userAgent)
    69  
    70  	return request, nil
    71  }