61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package nrod
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"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) {
|
|
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 {
|
|
log.Error("Error creating HTTP Request", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Add("Authorization", "Basic "+helpers.BasicAuth(cfg.NrodUser, cfg.NrodPass))
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Error("Error carrying out HTTP Request", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
err := fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
log.Error("Non-successful status code", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
// Run the data through the extractor function and return io.ReadCloser, error from
|
|
// that function directly
|
|
return NrodStreamExtract(resp)
|
|
}
|
|
|
|
func NrodStreamExtract(resp *http.Response) (io.ReadCloser, error) {
|
|
log.Debug("Extracting NROD Download")
|
|
|
|
log.Debug("Content Type", zap.String("Content-Encoding", resp.Header.Get("Content-Encoding")))
|
|
|
|
gzReader, err := gzip.NewReader(resp.Body)
|
|
if err != nil {
|
|
log.Warn("Unable to create GZIP Reader, data probably not gzipped")
|
|
return resp.Body, err
|
|
}
|
|
|
|
return gzReader, nil
|
|
}
|