Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add basic-auth #40

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}