github.com/polygon-io/client-go@v1.16.4/rest/models/request.go (about)

     1  package models
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  )
     7  
     8  // RequestOptions are used to configure client calls.
     9  type RequestOptions struct {
    10  	// APIKey to pass with the request
    11  	APIKey *string
    12  
    13  	// Headers to apply to the request
    14  	Headers http.Header
    15  
    16  	// Query params to apply to the request
    17  	QueryParams url.Values
    18  
    19  	// Trace enables request tracing
    20  	Trace bool
    21  }
    22  
    23  // RequestOption changes the configuration of RequestOptions.
    24  type RequestOption func(o *RequestOptions)
    25  
    26  // APIKey sets an APIKey as an option.
    27  func APIKey(id string) RequestOption {
    28  	return func(o *RequestOptions) {
    29  		o.APIKey = &id
    30  	}
    31  }
    32  
    33  // Header sets a header as an option.
    34  func Header(key, value string) RequestOption {
    35  	return func(o *RequestOptions) {
    36  		if o.Headers == nil {
    37  			o.Headers = make(http.Header)
    38  		}
    39  
    40  		o.Headers.Add(key, value)
    41  	}
    42  }
    43  
    44  // QueryParam sets a query param as an option.
    45  func QueryParam(key, value string) RequestOption {
    46  	return func(o *RequestOptions) {
    47  		if o.QueryParams == nil {
    48  			o.QueryParams = make(url.Values)
    49  		}
    50  
    51  		o.QueryParams.Add(key, value)
    52  	}
    53  }
    54  
    55  // Headers required to use the Launchpad product.
    56  const (
    57  	// HeaderEdgeID is a required Launchpad header. It identifies the Edge User requesting data.
    58  	HeaderEdgeID = "X-Polygon-Edge-ID"
    59  	// HeaderEdgeIPAddress is a required Launchpad header. It denotes the originating IP Address of the Edge User requesting data.
    60  	HeaderEdgeIPAddress = "X-Polygon-Edge-IP-Address"
    61  	// HeaderEdgeUserAgent is an optional Launchpad header. It denotes the originating UserAgent of the Edge User requesting data.
    62  	HeaderEdgeUserAgent = "X-Polygon-Edge-User-Agent"
    63  )
    64  
    65  // RequiredEdgeHeaders sets the required headers for the Launchpad product.
    66  func RequiredEdgeHeaders(edgeID, edgeIPAddress string) RequestOption {
    67  	return func(o *RequestOptions) {
    68  		if o.Headers == nil {
    69  			o.Headers = make(http.Header)
    70  		}
    71  
    72  		o.Headers.Add(HeaderEdgeID, edgeID)
    73  		o.Headers.Add(HeaderEdgeIPAddress, edgeIPAddress)
    74  	}
    75  }
    76  
    77  // EdgeUserAgent sets the Launchpad optional header denoting the Edge User's UserAgent.
    78  func EdgeUserAgent(userAgent string) RequestOption {
    79  	return Header(HeaderEdgeUserAgent, userAgent)
    80  }
    81  
    82  // WithTrace enables or disables request tracing.
    83  func WithTrace(trace bool) RequestOption {
    84  	return func(o *RequestOptions) {
    85  		o.Trace = trace
    86  	}
    87  }