generic: net return full response struct
This is still quite easy to stub, and the extra information is very useful to the caller. Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"slices"
|
||||
@@ -97,19 +98,21 @@ func mustFetch(ctx context.Context) {
|
||||
defer wg.Done()
|
||||
for u := range uc {
|
||||
buf := new(bytes.Buffer)
|
||||
if r, l, err := n.Get(ctx, u); err != nil {
|
||||
if resp, err := n.Get(ctx, u); err != nil {
|
||||
log.Fatal(err)
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
log.Fatal(&monstersirenfetch.ResponseStatusError{URL: u, StatusCode: resp.StatusCode, CloseErr: resp.Body.Close()})
|
||||
} else {
|
||||
if v := int(l); v > 0 {
|
||||
if v := int(resp.ContentLength); v > 0 {
|
||||
buf.Grow(v)
|
||||
}
|
||||
if _, err = io.Copy(buf, r); err != nil {
|
||||
if closeErr := r.Close(); closeErr != nil {
|
||||
if _, err = io.Copy(buf, resp.Body); err != nil {
|
||||
if closeErr := resp.Body.Close(); closeErr != nil {
|
||||
log.Print(closeErr)
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = r.Close(); err != nil {
|
||||
if err = resp.Body.Close(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -10,12 +9,12 @@ type netDirect struct {
|
||||
c http.Client
|
||||
}
|
||||
|
||||
func (n *netDirect) Get(ctx context.Context, url string) (io.ReadCloser, int64, error) {
|
||||
func (n *netDirect) Get(ctx context.Context, url string) (*http.Response, error) {
|
||||
var resp *http.Response
|
||||
if req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil); err != nil {
|
||||
return nil, -2, err
|
||||
return nil, err
|
||||
} else if resp, err = n.c.Do(req); err != nil {
|
||||
return nil, -2, err
|
||||
return nil, err
|
||||
}
|
||||
return resp.Body, resp.ContentLength, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user