timetable-mgr/nrod/streams.go

65 lines
1.5 KiB
Go
Raw Normal View History

package nrod
import (
"compress/gzip"
"errors"
"fmt"
"io"
"net/http"
"time"
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
// Downloads NROD Data and extracts if GZIP, returns a io.Reader
func NrodStream(url string, cfg *helpers.Configuration) (io.ReadCloser, error) {
2024-04-14 19:03:13 +01:00
log.Debug("Fetching NROD data stream", zap.String("Request URL", url))
client := http.Client{
Timeout: time.Second * 300,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Basic "+helpers.BasicAuth(cfg.NrodUser, cfg.NrodPass))
resp, err := client.Do(req)
if err != nil {
2024-04-28 08:38:53 +01:00
log.Error("Error carrying out HTTP Request", zap.Error(err))
return nil, err
}
if resp == nil {
err = errors.New("http response error - response = nil")
return nil, err
}
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("unexpected status code: %d", resp.StatusCode)
return nil, err
}
// Run the data through the extractor function and return io.ReadCloser, error from
2024-04-10 20:46:20 +01:00
// that function directly
return NrodStreamExtract(resp)
}
func NrodStreamExtract(resp *http.Response) (io.ReadCloser, error) {
2024-04-14 19:03:13 +01:00
log.Debug("Extracting NROD Download")
2024-04-14 19:03:13 +01:00
log.Debug("Content Type", zap.String("Content-Encoding", resp.Header.Get("Content-Encoding")))
gzReader, err := gzip.NewReader(resp.Body)
if err != nil {
2024-04-14 19:03:13 +01:00
log.Warn("Unable to create GZIP Reader, data probably not gzipped")
return resp.Body, err
}
return gzReader, nil
}