From d23b4052c0866698b14ac13ac98581d9f5440a9b Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sun, 11 Mar 2018 19:38:27 +0100 Subject: 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. --- iterator.go | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'iterator.go') diff --git a/iterator.go b/iterator.go index e460edd..04fb492 100644 --- a/iterator.go +++ b/iterator.go @@ -31,21 +31,19 @@ type iteratorInterface interface { getAccount() *Account getContainerName() string getPrefix() string - getHeaders() map[string]string getOptions() *RequestOptions //putHeader initializes the AccountHeaders/ContainerHeaders field of the //Account/Container using the response headers from the GET request. putHeader(http.Header) error } -func (i ContainerIterator) getAccount() *Account { return i.Account } -func (i ContainerIterator) getContainerName() string { return "" } -func (i ContainerIterator) getPrefix() string { return i.Prefix } -func (i ContainerIterator) getHeaders() map[string]string { return i.Headers } -func (i ContainerIterator) getOptions() *RequestOptions { return i.Options } +func (i ContainerIterator) getAccount() *Account { return i.Account } +func (i ContainerIterator) getContainerName() string { return "" } +func (i ContainerIterator) getPrefix() string { return i.Prefix } +func (i ContainerIterator) getOptions() *RequestOptions { return i.Options } func (i ContainerIterator) putHeader(hdr http.Header) error { - headers := AccountHeaders(headersFromHTTP(hdr)) + headers := AccountHeaders{headersFromHTTP(hdr)} if err := headers.Validate(); err != nil { return err } @@ -53,14 +51,13 @@ func (i ContainerIterator) putHeader(hdr http.Header) error { return nil } -func (i ObjectIterator) getAccount() *Account { return i.Container.Account() } -func (i ObjectIterator) getContainerName() string { return i.Container.Name() } -func (i ObjectIterator) getPrefix() string { return i.Prefix } -func (i ObjectIterator) getHeaders() map[string]string { return i.Headers } -func (i ObjectIterator) getOptions() *RequestOptions { return i.Options } +func (i ObjectIterator) getAccount() *Account { return i.Container.Account() } +func (i ObjectIterator) getContainerName() string { return i.Container.Name() } +func (i ObjectIterator) getPrefix() string { return i.Prefix } +func (i ObjectIterator) getOptions() *RequestOptions { return i.Options } func (i ObjectIterator) putHeader(hdr http.Header) error { - headers := ContainerHeaders(headersFromHTTP(hdr)) + headers := ContainerHeaders{headersFromHTTP(hdr)} if err := headers.Validate(); err != nil { return err } @@ -79,8 +76,7 @@ func (b *iteratorBase) request(limit int, detailed bool) Request { r := Request{ Method: "GET", ContainerName: b.i.getContainerName(), - Headers: headersToHTTP(b.i.getHeaders()), - Options: cloneRequestOptions(b.i.getOptions()), + Options: cloneRequestOptions(b.i.getOptions(), nil), } if prefix := b.i.getPrefix(); prefix != "" { @@ -100,11 +96,11 @@ func (b *iteratorBase) request(limit int, detailed bool) Request { } if detailed { - r.Headers.Set("Accept", "application/json") + r.Options.Headers.Set("Accept", "application/json") r.Options.Values.Set("format", "json") r.ExpectStatusCodes = []int{200} } else { - r.Headers.Set("Accept", "text/plain") + r.Options.Headers.Set("Accept", "text/plain") r.Options.Values.Set("format", "plain") r.ExpectStatusCodes = []int{200, 204} } -- cgit v1.2.3