github.com/blend/go-sdk@v1.20220411.3/r2/ensure_http_transport.go (about)

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