transport.NewHTTPReadSeeker: return concrete type, deprecate ReadSeekCloser

General convention is to define interfaces on the receiver side, and
to return concrete types.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-10 17:23:11 +01:00
parent 019ead86f5
commit d71ad5b3a6
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -20,17 +20,17 @@ var (
) )
// ReadSeekCloser combines io.ReadSeeker with io.Closer. // ReadSeekCloser combines io.ReadSeeker with io.Closer.
type ReadSeekCloser interface { //
io.ReadSeeker // Deprecated: use [io.ReadSeekCloser].
io.Closer type ReadSeekCloser = io.ReadSeekCloser
}
// NewHTTPReadSeeker handles reading from an HTTP endpoint using a GET // NewHTTPReadSeeker handles reading from an HTTP endpoint using a GET
// request. When seeking and starting a read from a non-zero offset // request. When seeking and starting a read from a non-zero offset
// the a "Range" header will be added which sets the offset. // the a "Range" header will be added which sets the offset.
//
// TODO(dmcgowan): Move this into a separate utility package // TODO(dmcgowan): Move this into a separate utility package
func NewHTTPReadSeeker(ctx context.Context, client *http.Client, url string, errorHandler func(*http.Response) error) ReadSeekCloser { func NewHTTPReadSeeker(ctx context.Context, client *http.Client, url string, errorHandler func(*http.Response) error) *HTTPReadSeeker {
return &httpReadSeeker{ return &HTTPReadSeeker{
ctx: ctx, ctx: ctx,
client: client, client: client,
url: url, url: url,
@ -38,7 +38,8 @@ func NewHTTPReadSeeker(ctx context.Context, client *http.Client, url string, err
} }
} }
type httpReadSeeker struct { // HTTPReadSeeker implements an [io.ReadSeekCloser].
type HTTPReadSeeker struct {
ctx context.Context ctx context.Context
client *http.Client client *http.Client
url string url string
@ -63,7 +64,7 @@ type httpReadSeeker struct {
err error err error
} }
func (hrs *httpReadSeeker) Read(p []byte) (n int, err error) { func (hrs *HTTPReadSeeker) Read(p []byte) (n int, err error) {
if hrs.err != nil { if hrs.err != nil {
return 0, hrs.err return 0, hrs.err
} }
@ -92,7 +93,7 @@ func (hrs *httpReadSeeker) Read(p []byte) (n int, err error) {
return n, err return n, err
} }
func (hrs *httpReadSeeker) Seek(offset int64, whence int) (int64, error) { func (hrs *HTTPReadSeeker) Seek(offset int64, whence int) (int64, error) {
if hrs.err != nil { if hrs.err != nil {
return 0, hrs.err return 0, hrs.err
} }
@ -135,7 +136,7 @@ func (hrs *httpReadSeeker) Seek(offset int64, whence int) (int64, error) {
return hrs.seekOffset, err return hrs.seekOffset, err
} }
func (hrs *httpReadSeeker) Close() error { func (hrs *HTTPReadSeeker) Close() error {
if hrs.err != nil { if hrs.err != nil {
return hrs.err return hrs.err
} }
@ -152,7 +153,7 @@ func (hrs *httpReadSeeker) Close() error {
return nil return nil
} }
func (hrs *httpReadSeeker) reset() { func (hrs *HTTPReadSeeker) reset() {
if hrs.err != nil { if hrs.err != nil {
return return
} }
@ -162,7 +163,7 @@ func (hrs *httpReadSeeker) reset() {
} }
} }
func (hrs *httpReadSeeker) reader() (io.Reader, error) { func (hrs *HTTPReadSeeker) reader() (io.Reader, error) {
if hrs.err != nil { if hrs.err != nil {
return nil, hrs.err return nil, hrs.err
} }