github.com/puellanivis/breton@v0.2.16/lib/files/httpfiles/option.go (about)

     1  package httpfiles
     2  
     3  import (
     4  	"net/url"
     5  
     6  	"github.com/puellanivis/breton/lib/files"
     7  )
     8  
     9  // WithForm returns a files.Option that that will add to the underlying HTTP
    10  // request the url.Values given as a POST request. (A GET request can always
    11  // be composed through the URL string itself.
    12  func WithForm(vals url.Values) files.Option {
    13  	body := []byte(vals.Encode())
    14  	return WithContent("POST", "application/x-www-form-urlencoded", body)
    15  }
    16  
    17  // WithContent returns a files.Option that will set the Method, Body and
    18  // Content-Type of the underlying HTTP request to the given values.
    19  func WithContent(method, contentType string, data []byte) files.Option {
    20  	type methodSetter interface {
    21  		SetMethod(string) string
    22  	}
    23  
    24  	type ctypeSetter interface {
    25  		SetContentType(string) string
    26  	}
    27  
    28  	type bodySetter interface {
    29  		SetBody([]byte) []byte
    30  	}
    31  
    32  	return func(f files.File) (files.Option, error) {
    33  		var methodSave, ctypeSave string
    34  		var dataSave []byte
    35  
    36  		if r, ok := f.(methodSetter); ok {
    37  			methodSave = r.SetMethod(method)
    38  		}
    39  
    40  		if r, ok := f.(ctypeSetter); ok {
    41  			ctypeSave = r.SetContentType(contentType)
    42  		}
    43  
    44  		if r, ok := f.(bodySetter); ok {
    45  			dataSave = r.SetBody(data)
    46  		}
    47  
    48  		// option is not reversible
    49  		return WithContent(methodSave, ctypeSave, dataSave), nil
    50  	}
    51  }
    52  
    53  // WithMethod returns a files.Option that sets the Method of the
    54  // underlying HTTP request to be the given value.
    55  func WithMethod(method string) files.Option {
    56  	type methodSetter interface {
    57  		SetMethod(string) string
    58  	}
    59  
    60  	return func(f files.File) (files.Option, error) {
    61  		var save string
    62  
    63  		if r, ok := f.(methodSetter); ok {
    64  			save = r.SetMethod(method)
    65  		}
    66  
    67  		return WithMethod(save), nil
    68  	}
    69  }
    70  
    71  // WithContentType returns a files.Option that sets the Content-Type of the
    72  // underlying HTTP request to be the given value. (This is intended to allow
    73  // setting a specific type during a files.Create() and not have it auto-detect
    74  // during the eventual commit of the request at Sync() or Close().)
    75  func WithContentType(contentType string) files.Option {
    76  	type ctypeSetter interface {
    77  		SetContentType(string) string
    78  	}
    79  
    80  	return func(f files.File) (files.Option, error) {
    81  		var save string
    82  
    83  		if r, ok := f.(ctypeSetter); ok {
    84  			save = r.SetContentType(contentType)
    85  		}
    86  
    87  		return WithContentType(save), nil
    88  	}
    89  }