Moved common HTTP body fetch code into a shared helper function.
This commit is contained in:
@@ -5,13 +5,13 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/common"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
|
||||
)
|
||||
|
||||
@@ -133,30 +133,9 @@ type observationMeta struct {
|
||||
}
|
||||
|
||||
func (s *ObservationSource) fetchRaw(ctx context.Context) (json.RawMessage, observationMeta, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.url, nil)
|
||||
b, err := common.FetchBody(ctx, s.client, s.url, s.userAgent, "application/geo+json, application/json")
|
||||
if err != nil {
|
||||
return nil, observationMeta{}, err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", s.userAgent)
|
||||
req.Header.Set("Accept", "application/geo+json, application/json")
|
||||
|
||||
res, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, observationMeta{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
return nil, observationMeta{}, fmt.Errorf("nws_observation %q: HTTP %s", s.name, res.Status)
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, observationMeta{}, err
|
||||
}
|
||||
if len(b) == 0 {
|
||||
return nil, observationMeta{}, fmt.Errorf("nws_observation %q: empty response body", s.name)
|
||||
return nil, observationMeta{}, fmt.Errorf("nws_observation %q: %w", s.name, err)
|
||||
}
|
||||
|
||||
raw := json.RawMessage(b)
|
||||
|
||||
Reference in New Issue
Block a user