Skip to content

Commit

Permalink
chore: add basic-auth (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: truedrju <[email protected]>
  • Loading branch information
dedlockdave and dedlockdave authored Aug 30, 2024
1 parent 02bf9bd commit af580bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions flight-data-ingester/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions flight-data-ingester/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 27 additions & 2 deletions flight-data-ingester/ingester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit af580bc

Please sign in to comment.