...

Source file src/github.com/sigstore/rekor/pkg/client/options.go

Documentation: github.com/sigstore/rekor/pkg/client

     1  // Copyright 2021 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import (
    18  	"net/http"
    19  
    20  	"github.com/hashicorp/go-retryablehttp"
    21  )
    22  
    23  // Option is a functional option for customizing static signatures.
    24  type Option func(*options)
    25  
    26  type options struct {
    27  	UserAgent   string
    28  	RetryCount  uint
    29  	InsecureTLS bool
    30  	Logger      interface{}
    31  }
    32  
    33  const (
    34  	// DefaultRetryCount is the default number of retries.
    35  	DefaultRetryCount = 3
    36  )
    37  
    38  func makeOptions(opts ...Option) *options {
    39  	o := &options{
    40  		UserAgent:  "",
    41  		RetryCount: DefaultRetryCount,
    42  	}
    43  
    44  	for _, opt := range opts {
    45  		opt(o)
    46  	}
    47  
    48  	return o
    49  }
    50  
    51  // WithUserAgent sets the media type of the signature.
    52  func WithUserAgent(userAgent string) Option {
    53  	return func(o *options) {
    54  		o.UserAgent = userAgent
    55  	}
    56  }
    57  
    58  // WithRetryCount sets the number of retries.
    59  func WithRetryCount(retryCount uint) Option {
    60  	return func(o *options) {
    61  		o.RetryCount = retryCount
    62  	}
    63  }
    64  
    65  // WithLogger sets the logger; it must implement either retryablehttp.Logger or retryablehttp.LeveledLogger; if not, this will not take effect.
    66  func WithLogger(logger interface{}) Option {
    67  	return func(o *options) {
    68  		switch logger.(type) {
    69  		case retryablehttp.Logger, retryablehttp.LeveledLogger:
    70  			o.Logger = logger
    71  		}
    72  	}
    73  }
    74  
    75  func WithInsecureTLS(enabled bool) Option {
    76  	return func(o *options) {
    77  		o.InsecureTLS = enabled
    78  	}
    79  }
    80  
    81  type roundTripper struct {
    82  	http.RoundTripper
    83  	UserAgent string
    84  }
    85  
    86  // RoundTrip implements `http.RoundTripper`
    87  func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    88  	req.Header.Set("User-Agent", rt.UserAgent)
    89  	return rt.RoundTripper.RoundTrip(req)
    90  }
    91  
    92  func createRoundTripper(inner http.RoundTripper, o *options) http.RoundTripper {
    93  	if inner == nil {
    94  		inner = http.DefaultTransport
    95  	}
    96  	if o.UserAgent == "" {
    97  		// There's nothing to do...
    98  		return inner
    99  	}
   100  	return &roundTripper{
   101  		RoundTripper: inner,
   102  		UserAgent:    o.UserAgent,
   103  	}
   104  }
   105  

View as plain text