generic: return content length alongside body
This increases efficiency for bigger response bodies. Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
parent
00b50dab08
commit
21871a387c
@ -10,12 +10,12 @@ type netDirect struct {
|
||||
c http.Client
|
||||
}
|
||||
|
||||
func (n *netDirect) Get(ctx context.Context, url string) (io.ReadCloser, error) {
|
||||
func (n *netDirect) Get(ctx context.Context, url string) (io.ReadCloser, int64, error) {
|
||||
var resp *http.Response
|
||||
if req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil); err != nil {
|
||||
return nil, err
|
||||
return nil, -2, err
|
||||
} else if resp, err = n.c.Do(req); err != nil {
|
||||
return nil, err
|
||||
return nil, -2, err
|
||||
}
|
||||
return resp.Body, nil
|
||||
return resp.Body, resp.ContentLength, nil
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ const (
|
||||
type Net interface {
|
||||
// Get makes a get request to url and returns the response body.
|
||||
// The caller must close the body after it finishes reading from it.
|
||||
Get(ctx context.Context, url string) (body io.ReadCloser, err error)
|
||||
Get(ctx context.Context, url string) (body io.ReadCloser, contentLength int64, err error)
|
||||
}
|
||||
|
||||
// Response is a generic API response.
|
||||
|
2
song.go
2
song.go
@ -47,7 +47,7 @@ func (s *Song) Enrich(ctx context.Context, n Net) error {
|
||||
}
|
||||
|
||||
var v SongResponse
|
||||
if body, err := n.Get(ctx, APIPrefix+"/song/"+s.CID.String()); err != nil {
|
||||
if body, _, err := n.Get(ctx, APIPrefix+"/song/"+s.CID.String()); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err = json.NewDecoder(body).Decode(&v); err != nil {
|
||||
|
14
song_test.go
14
song_test.go
@ -168,22 +168,22 @@ func TestSongEnrich(t *testing.T) {
|
||||
|
||||
type stubNetSongEnrichErrorCloser stubNetSongEnrich
|
||||
|
||||
func (n stubNetSongEnrichErrorCloser) Get(ctx context.Context, url string) (io.ReadCloser, error) {
|
||||
r, err := stubNetSongEnrich(n).Get(ctx, url)
|
||||
func (n stubNetSongEnrichErrorCloser) Get(ctx context.Context, url string) (io.ReadCloser, int64, error) {
|
||||
r, l, err := stubNetSongEnrich(n).Get(ctx, url)
|
||||
if r != nil {
|
||||
r = errorCloser{r}
|
||||
}
|
||||
return r, err
|
||||
return r, l, err
|
||||
}
|
||||
|
||||
type stubNetSongEnrich map[StringInt][]byte
|
||||
|
||||
func (n stubNetSongEnrich) Get(_ context.Context, url string) (io.ReadCloser, error) {
|
||||
func (n stubNetSongEnrich) Get(_ context.Context, url string) (io.ReadCloser, int64, error) {
|
||||
if i, err := strconv.Atoi(strings.TrimPrefix(url, APIPrefix+"/song/")); err != nil {
|
||||
return nil, err
|
||||
return nil, -2, err
|
||||
} else if b, ok := n[StringInt(i)]; !ok {
|
||||
return nil, fmt.Errorf("song cid %d requested, but is not present", i)
|
||||
return nil, -2, fmt.Errorf("song cid %d requested, but is not present", i)
|
||||
} else {
|
||||
return nopCloser{bytes.NewReader(b)}, nil
|
||||
return nopCloser{bytes.NewReader(b)}, int64(len(b)), nil
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user