From af580bce921520266813642e656c7df9db5b5f8c Mon Sep 17 00:00:00 2001 From: david <76677784+dedlockdave@users.noreply.github.com> Date: Fri, 30 Aug 2024 11:10:26 -0700 Subject: [PATCH] chore: add basic-auth (#40) Co-authored-by: truedrju <76677784+truedrju@users.noreply.github.com> --- flight-data-ingester/README.md | 4 ++++ flight-data-ingester/docker-compose.yml | 2 ++ flight-data-ingester/ingester/main.go | 29 +++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/flight-data-ingester/README.md b/flight-data-ingester/README.md index 331da37..532215f 100644 --- a/flight-data-ingester/README.md +++ b/flight-data-ingester/README.md @@ -36,6 +36,10 @@ running the demo in to ensure there is data in the set. This can be done by setting the ``ICAO_AIRPORT_CODE` environment variable when running `docker compose up`. +The anonymous user can only make 400 requests per day. +Set the OPENSKY_USERNAME and OPENSKY_PASSWORD env variables with +your credentials to use basic authentication in the open sky requests for higher limits. + If you are going to restart this demo, press `Ctrl-C` and remember to call `docker compose down` to clean up the data before you run `docker compose up` again. diff --git a/flight-data-ingester/docker-compose.yml b/flight-data-ingester/docker-compose.yml index 4dc5e0c..bbd259a 100644 --- a/flight-data-ingester/docker-compose.yml +++ b/flight-data-ingester/docker-compose.yml @@ -23,6 +23,8 @@ services: - demo-network environment: - ICAO_AIRPORT_CODE=${ICAO_AIRPORT_CODE:-KSFO} + - OPENSKY_USERNAME=${OPENSKY_USERNAME} + - OPENSKY_PASSWORD=${OPENSKY_PASSWORD} depends_on: greptimedb: condition: service_started diff --git a/flight-data-ingester/ingester/main.go b/flight-data-ingester/ingester/main.go index aa0770e..fc2c177 100644 --- a/flight-data-ingester/ingester/main.go +++ b/flight-data-ingester/ingester/main.go @@ -92,8 +92,13 @@ func GetFlightState(flight []Flight) (*StateResponse, error) { icaoCsv = strings.Join(icaos, ",") url := fmt.Sprintf("https://opensky-network.org/api/states/all?icao24=%s", icaoCsv) + req, err := OpenSkyReq(url) + if err != nil { + return nil, fmt.Errorf("error creating request: %v", err) + } + client := &http.Client{Timeout: 10 * time.Second} - resp, err := client.Get(url) + resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error making request: %v", err) } @@ -133,8 +138,13 @@ func SelectLiveFlights(airportCode string, num int) ([]Flight, error) { url := fmt.Sprintf("https://opensky-network.org/api/flights/departure?begin=%d&end=%d&airport=%s", startTime, endTime, airportCode) + req, err := OpenSkyReq(url) + if err != nil { + return nil, fmt.Errorf("error creating request: %v", err) + } + client := &http.Client{Timeout: 10 * time.Second} - resp, err := client.Get(url) + resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error making request: %v", err) } @@ -166,3 +176,18 @@ func SelectLiveFlights(airportCode string, num int) ([]Flight, error) { return flights[:num], nil } + +func OpenSkyReq(url string) (*http.Request, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + + // Add basic auth if credentials are present + username := os.Getenv("OPENSKY_USERNAME") + password := os.Getenv("OPENSKY_PASSWORD") + if username != "" && password != "" { + req.SetBasicAuth(username, password) + } + return req, nil +}