go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/r2/ensure_http_transport.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package r2
     9  
    10  import "net/http"
    11  
    12  // EnsureHTTPTransport ensures the http client's transport
    13  // is set and that it is an *http.Transport.
    14  //
    15  // It will return an error `ErrInvalidTransport` if it
    16  // is set to something other than *http.Transport.
    17  func EnsureHTTPTransport(r *Request) (typed *http.Transport, err error) {
    18  	if r.client == nil {
    19  		r.client = &http.Client{}
    20  	}
    21  	if r.client.Transport == nil {
    22  		r.client.Transport = &http.Transport{}
    23  	}
    24  	var ok bool
    25  	typed, ok = r.client.Transport.(*http.Transport)
    26  	if r.client.Transport != nil && !ok {
    27  		err = ErrInvalidTransport
    28  		return
    29  	}
    30  	if typed == nil {
    31  		typed = &http.Transport{}
    32  		r.client.Transport = typed
    33  	}
    34  	return
    35  }