Skip to content

Commit

Permalink
[:GHPAGES:] Merge pull request #4 from navidys/doc_ghpage
Browse files Browse the repository at this point in the history
[:GHPAGES:] - example update
  • Loading branch information
navidys authored Nov 8, 2023
2 parents 7960e76 + ab18771 commit 7e4bafe
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 27 deletions.
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,69 @@ import "github.com/navidys/gopensky"

Here is an example program once you've added the library, visit [Golang OpenSky Network API](https://navidys.github.io/gopensky/) for more examples.


```
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/navidys/gopensky"
)
func main() {
conn, err := gopensky.NewConnection(context.Background(), "username", "password")
conn, err := gopensky.NewConnection(context.Background(), "", "")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// retrieve all states information
statesData, err := gopensky.GetStates(conn, 0, nil, nil)
// retrieve arrivals flights of:
// airpor: LFPG (Charles de Gaulle)
// being time: 1696755342 (Sunday October 08, 2023 08:55:42 UTC)
// end time: 1696928142 (Tuesday October 10, 2023 08:55:42 UTC)
flightsData, err := gopensky.GetArrivalsByAirport(conn, "LFPG", 1696755342, 1696928142)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
for _, state := range statesData.States {
fmt.Printf("ICAO24: %s, Origin Country: %s, Longitude: %v, Latitude: %v \n", state.Icao24, state.OriginCountry, state.Longitude, state.Latitude)
for _, flightData := range flightsData {
var depAirport string
if flightData.EstDepartureAirport != nil {
depAirport = *flightData.EstDepartureAirport
}
fmt.Printf("ICAO24: %s, Departure Airport: %4s, LastSeen: %s\n",
flightData.Icao24,
depAirport,
time.Unix(flightData.LastSeen, 0),
)
}
}
```

output:

```
ICAO24: 406544, Departure Airport: EGPH, LastSeen: 2023-10-10 07:33:07 +1100 AEDT
ICAO24: 896180, Departure Airport: , LastSeen: 2023-10-10 05:07:35 +1100 AEDT
ICAO24: 738065, Departure Airport: LLBG, LastSeen: 2023-10-10 03:14:58 +1100 AEDT
ICAO24: 4bc848, Departure Airport: LTFJ, LastSeen: 2023-10-10 01:31:15 +1100 AEDT
ICAO24: 4891b6, Departure Airport: , LastSeen: 2023-10-09 20:52:38 +1100 AEDT
ICAO24: 39856a, Departure Airport: LFBO, LastSeen: 2023-10-09 20:45:12 +1100 AEDT
ICAO24: 4ba9c9, Departure Airport: LTFM, LastSeen: 2023-10-09 18:52:45 +1100 AEDT
ICAO24: 738075, Departure Airport: LFPG, LastSeen: 2023-10-09 16:03:10 +1100 AEDT
ICAO24: 39e68b, Departure Airport: ESSA, LastSeen: 2023-10-09 07:23:04 +1100 AEDT
ICAO24: 01020a, Departure Airport: , LastSeen: 2023-10-09 05:46:24 +1100 AEDT
ICAO24: 39e698, Departure Airport: LOWW, LastSeen: 2023-10-09 04:51:45 +1100 AEDT
ICAO24: 398569, Departure Airport: LJLJ, LastSeen: 2023-10-09 02:03:00 +1100 AEDT
```

## License

Licensed under the [Apache 2.0](LICENSE) license.
Licensed under the [Apache 2.0](LICENSE) license.
12 changes: 0 additions & 12 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"net"
"net/http"
"net/url"

"github.com/rs/zerolog/log"
)

const (
Expand Down Expand Up @@ -82,7 +80,6 @@ func (c *Connection) doGetRequest(ctx context.Context, httpBody io.Reader,
if len(queryParams) > 0 {
params := queryParams.Encode()
requestURL = fmt.Sprintf("%s?%s", requestURL, params)
log.Debug().Msgf("do request params: %s", params)
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, httpBody)
Expand All @@ -91,12 +88,9 @@ func (c *Connection) doGetRequest(ctx context.Context, httpBody io.Reader,
}

if c.auth != "" {
log.Debug().Msg("setting authorization")
req.Header.Add("Authorization", "Basic "+c.auth)
}

log.Debug().Msgf("do get request: %s", req.URL)

response, err := c.client.Do(req) //nolint:bodyclose

return &apiResponse{response, req}, err //nolint:wrapcheck
Expand Down Expand Up @@ -129,8 +123,6 @@ func (h apiResponse) processWithError(unmarshalInto interface{}) error {
}

if h.isSuccess() || h.isRedirection() {
log.Debug().Msg("process response (success or redirection)")

if unmarshalInto != nil {
if err := json.Unmarshal(data, unmarshalInto); err != nil {
return fmt.Errorf("unmarshalling into %#v, data %q: %w", unmarshalInto, string(data), err)
Expand All @@ -143,13 +135,9 @@ func (h apiResponse) processWithError(unmarshalInto interface{}) error {
}

if h.isInformational() {
log.Debug().Msg("process response (information)")

return nil
}

log.Debug().Msg("process response (error)")

return handleError(h.Response.StatusCode, data)
}

Expand Down
102 changes: 98 additions & 4 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Example for retrieving all states without authentication:

.. code-block:: go
package main
import (
"context"
"fmt"
Expand All @@ -23,18 +25,110 @@ Example for retrieving all states without authentication:
}
// retrieve all states information
statesData, err := gopensky.GetStates(conn, 0, nil, nil)
statesData, err := gopensky.GetStates(conn, 0, nil, nil, true)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
for _, state := range statesData.States {
fmt.Printf("ICAO24: %s, Origin Country: %s, Longitude: %v, Latitude: %v \n",
longitude := "<nil>"
latitude := "<nil>"
if state.Longitude != nil {
longitude = fmt.Sprintf("%f", *state.Longitude)
}
if state.Latitude != nil {
latitude = fmt.Sprintf("%f", *state.Latitude)
}
fmt.Printf("ICAO24: %s, Longitude: %s, Latitude: %s, Origin Country: %s \n",
state.Icao24,
longitude,
latitude,
state.OriginCountry,
state.Longitude,
state.Latitude,
)
}
}
output:

.. code-block:: bash
ICAO24: e49406, Longitude: -46.509700, Latitude: -23.144100, Origin Country: Brazil
ICAO24: ad4f1c, Longitude: -81.208700, Latitude: 29.430400, Origin Country: United States
ICAO24: 7c6b2d, Longitude: 171.803200, Latitude: -43.586000, Origin Country: Australia
ICAO24: 4b1812, Longitude: 8.498200, Latitude: 47.461400, Origin Country: Switzerland
ICAO24: 88044e, Longitude: 108.214900, Latitude: 18.868100, Origin Country: Thailand
ICAO24: 440da1, Longitude: 17.950300, Latitude: 59.645800, Origin Country: Austria
ICAO24: ab6fdd, Longitude: -96.840200, Latitude: 38.359400, Origin Country: United States
Arrivals by Airport
--------------------
Example of retrieving flights for a certain airport which arrived within a given time interval:

.. code-block:: go
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/navidys/gopensky"
)
func main() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// retrieve arrivals flights of:
// airpor: LFPG (Charles de Gaulle)
// being time: 1696755342 (Sunday October 08, 2023 08:55:42 UTC)
// end time: 1696928142 (Tuesday October 10, 2023 08:55:42 UTC)
flightsData, err := gopensky.GetArrivalsByAirport(conn, "LFPG", 1696755342, 1696928142)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
for _, flightData := range flightsData {
var depAirport string
if flightData.EstDepartureAirport != nil {
depAirport = *flightData.EstDepartureAirport
}
fmt.Printf("ICAO24: %s, Departure Airport: %4s, LastSeen: %s\n",
flightData.Icao24,
depAirport,
time.Unix(flightData.LastSeen, 0),
)
}
}
.. ::

output:

.. code-block:: bash
ICAO24: 406544, Departure Airport: EGPH, LastSeen: 2023-10-10 07:33:07 +1100 AEDT
ICAO24: 896180, Departure Airport: , LastSeen: 2023-10-10 05:07:35 +1100 AEDT
ICAO24: 738065, Departure Airport: LLBG, LastSeen: 2023-10-10 03:14:58 +1100 AEDT
ICAO24: 4bc848, Departure Airport: LTFJ, LastSeen: 2023-10-10 01:31:15 +1100 AEDT
ICAO24: 4891b6, Departure Airport: , LastSeen: 2023-10-09 20:52:38 +1100 AEDT
ICAO24: 39856a, Departure Airport: LFBO, LastSeen: 2023-10-09 20:45:12 +1100 AEDT
ICAO24: 4ba9c9, Departure Airport: LTFM, LastSeen: 2023-10-09 18:52:45 +1100 AEDT
ICAO24: 738075, Departure Airport: LFPG, LastSeen: 2023-10-09 16:03:10 +1100 AEDT
ICAO24: 39e68b, Departure Airport: ESSA, LastSeen: 2023-10-09 07:23:04 +1100 AEDT
ICAO24: 01020a, Departure Airport: , LastSeen: 2023-10-09 05:46:24 +1100 AEDT
ICAO24: 39e698, Departure Airport: LOWW, LastSeen: 2023-10-09 04:51:45 +1100 AEDT
ICAO24: 398569, Departure Airport: LJLJ, LastSeen: 2023-10-09 02:03:00 +1100 AEDT
44 changes: 44 additions & 0 deletions examples/all_states/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"fmt"
"os"

"github.com/navidys/gopensky"
)

func main() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// retrieve all states information
statesData, err := gopensky.GetStates(conn, 0, nil, nil, true)
if err != nil {
fmt.Println(err)
os.Exit(2)
}

for _, state := range statesData.States {
longitude := "<nil>"
latitude := "<nil>"

if state.Longitude != nil {
longitude = fmt.Sprintf("%f", *state.Longitude)
}

if state.Latitude != nil {
latitude = fmt.Sprintf("%f", *state.Latitude)
}

fmt.Printf("ICAO24: %s, Longitude: %s, Latitude: %s, Origin Country: %s \n",
state.Icao24,
longitude,
latitude,
state.OriginCountry,
)
}
}
42 changes: 42 additions & 0 deletions examples/arrivals_aiport/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"fmt"
"os"
"time"

"github.com/navidys/gopensky"
)

func main() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// retrieve arrivals flights of:
// airpor: LFPG (Charles de Gaulle)
// being time: 1696755342 (Sunday October 08, 2023 08:55:42 UTC)
// end time: 1696928142 (Tuesday October 10, 2023 08:55:42 UTC)

flightsData, err := gopensky.GetArrivalsByAirport(conn, "LFPG", 1696755342, 1696928142)
if err != nil {
fmt.Println(err)
os.Exit(2)
}

for _, flightData := range flightsData {
var depAirport string
if flightData.EstDepartureAirport != nil {
depAirport = *flightData.EstDepartureAirport
}

fmt.Printf("ICAO24: %s, Departure Airport: %4s, LastSeen: %s\n",
flightData.Icao24,
depAirport,
time.Unix(flightData.LastSeen, 0),
)
}
}
6 changes: 1 addition & 5 deletions states.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"
"net/url"

"github.com/rs/zerolog/log"
)

// Retrieve state vectors for a given time. If time = 0 the most recent ones are taken.
Expand Down Expand Up @@ -41,9 +39,7 @@ func GetStates(ctx context.Context, time int64, icao24 []string,
for _, st := range statesRep.States {
stvec, err := decodeRawStateVector(st)
if err != nil {
log.Error().Msgf("cannot decode received data: %v", err)

continue
return nil, fmt.Errorf("decode state vector: %w", err)
}

if stvec != nil {
Expand Down

0 comments on commit 7e4bafe

Please sign in to comment.