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.Msg.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.Msg.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.Msg.Error("Error carrying out HTTP Request", zap.Error(err), zap.Int("STATUS", resp.StatusCode)) return nil, err } if resp.StatusCode != http.StatusOK { err := fmt.Errorf("unexpected status code: %d", resp.StatusCode) log.Msg.Error("Non-successful status code", zap.Error(err)) return nil, err } // Run the data through the extractor function and return io.ReadCloser, error from // directly return NrodStreamExtract(resp) } func NrodStreamExtract(resp *http.Response) (io.ReadCloser, error) { log.Msg.Debug("Extracting NROD Download") log.Msg.Debug("Content Type", zap.String("Content-Encoding", resp.Header.Get("Content-Encoding"))) gzReader, err := gzip.NewReader(resp.Body) if err != nil { log.Msg.Warn("Unable to create GZIP Reader, data probably not gzipped") return resp.Body, err } return gzReader, nil }