diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2018-03-11 19:38:27 +0100 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2018-03-11 19:41:33 +0100 |
| commit | d23b4052c0866698b14ac13ac98581d9f5440a9b (patch) | |
| tree | 51c93559594f8fd5136fd51b7397a30415ea93aa /request.go | |
| parent | aaf61ac55e18a04fd68b9b6ee4fd4fce49659eeb (diff) | |
| download | go-schwift-d23b4052c0866698b14ac13ac98581d9f5440a9b.tar.gz | |
revamp the Headers API
1. Move common methods of AccountHeaders, ContainerHeaders,
ObjectHeaders into a base type Headers.
2. Fold Headers into RequestOptions to remove one of the two optional
arguments on request methods. The new Headers.ToOpts() method
offers a nice experience for users passing Headers to request
methods.
The Update() methods keep the explicit Headers argument since the
Headers argument is not optional there.
The only downside is that we lose a bit of type-safety because
RequestOptions takes any Headers instance, so e.g. ContainerHeaders
could be passed to Object.Upload(). I believe the benefits outweigh
this problem.
Diffstat (limited to 'request.go')
| -rw-r--r-- | request.go | 34 |
1 files changed, 27 insertions, 7 deletions
@@ -26,20 +26,39 @@ import ( "strings" ) -//RequestOptions contains additional headers and values for a request. +//RequestOptions is used to pass additional headers and values to aa request. +// +//When preparing a RequestOptions instance with additional headers, the +//preferred way is to create an AccountHeaders, ContainerHeaders and +//ObjectHeaders instance and use the type-safe API on these types. Then use the +//ToOpts() method on that instance. For example: +// +// hdr := NewObjectHeaders() +// hdr.ContentType().Set("image/png") +// hdr.Metadata().Set("color", "blue") +// opts := hdr.ToOpts() //type *schwift.RequestOptions +// type RequestOptions struct { - Values url.Values + Headers Headers + Values url.Values } -func cloneRequestOptions(orig *RequestOptions) *RequestOptions { +func cloneRequestOptions(orig *RequestOptions, additional Headers) *RequestOptions { result := RequestOptions{ - Values: make(url.Values), + Headers: make(Headers), + Values: make(url.Values), } if orig != nil { + for k, v := range orig.Headers { + result.Headers[k] = v + } for k, v := range orig.Values { result.Values[k] = v } } + for k, v := range additional { + result.Headers[k] = v + } return &result } @@ -48,7 +67,6 @@ type Request struct { Method string //"GET", "HEAD", "PUT", "POST" or "DELETE" ContainerName string //empty for requests on accounts ObjectName string //empty for requests on accounts/containers - Headers http.Header Options *RequestOptions Body io.Reader //ExpectStatusCodes can be left empty to disable this check, otherwise @@ -102,8 +120,10 @@ func (r Request) Do(backend Backend) (*http.Response, error) { return nil, err } - for k, v := range r.Headers { - req.Header[k] = v + if r.Options != nil { + for k, v := range r.Options.Headers { + req.Header[k] = []string{v} + } } if r.Body != nil { req.Header.Set("Expect", "100-continue") |
