package http

import (
	
	
	

	smithy 
	
)

// ClientDo provides the interface for custom HTTP client implementations.
type ClientDo interface {
	Do(*http.Request) (*http.Response, error)
}

// ClientDoFunc provides a helper to wrap a function as an HTTP client for
// round tripping requests.
type ClientDoFunc func(*http.Request) (*http.Response, error)

// Do will invoke the underlying func, returning the result.
func ( ClientDoFunc) ( *http.Request) (*http.Response, error) {
	return ()
}

// ClientHandler wraps a client that implements the HTTP Do method. Standard
// implementation is http.Client.
type ClientHandler struct {
	client ClientDo
}

// NewClientHandler returns an initialized middleware handler for the client.
func ( ClientDo) ClientHandler {
	return ClientHandler{
		client: ,
	}
}

// Handle implements the middleware Handler interface, that will invoke the
// underlying HTTP client. Requires the input to be a Smithy *Request. Returns
// a smithy *Response, or error if the request failed.
func ( ClientHandler) ( context.Context,  interface{}) (
	 interface{},  middleware.Metadata,  error,
) {
	,  := .(*Request)
	if ! {
		return nil, , fmt.Errorf("expect Smithy http.Request value as input, got unsupported type %T", )
	}

	 := .Build()
	if  := ValidateEndpointHost(.Host);  != nil {
		return nil, , 
	}

	,  := .client.Do()
	if  == nil {
		// Ensure a http response value is always present to prevent unexpected
		// panics.
		 = &http.Response{
			Header: http.Header{},
			Body:   http.NoBody,
		}
	}
	if  != nil {
		 = &RequestSendError{Err: }

		// Override the error with a context canceled error, if that was canceled.
		select {
		case <-.Done():
			 = &smithy.CanceledError{Err: .Err()}
		default:
		}
	}

	// HTTP RoundTripper *should* close the request body. But this may not happen in a timely manner.
	// So instead Smithy *Request Build wraps the body to be sent in a safe closer that will clear the
	// stream reference so that it can be safely reused.
	if .Body != nil {
		_ = .Body.Close()
	}

	return &Response{Response: }, , 
}

// RequestSendError provides a generic request transport error. This error
// should wrap errors making HTTP client requests.
//
// The ClientHandler will wrap the HTTP client's error if the client request
// fails, and did not fail because of context canceled.
type RequestSendError struct {
	Err error
}

// ConnectionError returns that the error is related to not being able to send
// the request, or receive a response from the service.
func ( *RequestSendError) () bool {
	return true
}

// Unwrap returns the underlying error, if there was one.
func ( *RequestSendError) () error {
	return .Err
}

func ( *RequestSendError) () string {
	return fmt.Sprintf("request send failed, %v", .Err)
}

// NopClient provides a client that ignores the request, and returns an empty
// successful HTTP response value.
type NopClient struct{}

// Do ignores the request and returns a 200 status empty response.
func (NopClient) ( *http.Request) (*http.Response, error) {
	return &http.Response{
		StatusCode: 200,
		Header:     http.Header{},
		Body:       http.NoBody,
	}, nil
}