-
Notifications
You must be signed in to change notification settings - Fork 0
/
flights.go
161 lines (120 loc) · 3.91 KB
/
flights.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package gopensky
import (
"context"
"fmt"
"net/url"
)
// GetArrivalsByAirport retrieves flights for a certain airport which arrived within a given time interval [being, end].
// The given time interval must not be larger than seven days!
func GetArrivalsByAirport(ctx context.Context, airport string, begin int64, end int64) ([]FlighData, error) {
var flighDataList []FlighData
conn, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("client: %w", err)
}
if airport == "" {
return nil, ErrInvalidAirportName
}
if begin <= 0 || end <= 0 {
return nil, ErrInvalidUnixTime
}
requestParams := getFlightsRequestParams(airport, "", begin, end)
response, err := conn.doGetRequest(ctx, "/flights/arrival", requestParams)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer response.Body.Close()
if err := response.process(&flighDataList); err != nil {
return nil, err
}
return flighDataList, nil
}
// GetDeparturesByAirport retrieves flights for a certain airport which departed
// The given time interval must not be larger than seven days!
func GetDeparturesByAirport(ctx context.Context, airport string, begin int64, end int64) ([]FlighData, error) {
var flighDataList []FlighData
conn, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("client: %w", err)
}
if airport == "" {
return nil, ErrInvalidAirportName
}
if begin <= 0 || end <= 0 {
return nil, ErrInvalidUnixTime
}
requestParams := getFlightsRequestParams(airport, "", begin, end)
response, err := conn.doGetRequest(ctx, "/flights/departure", requestParams)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer response.Body.Close()
if err := response.process(&flighDataList); err != nil {
return nil, err
}
return flighDataList, nil
}
// GetFlightsByInterval retrieves flights for a certain time interval [begin, end].
// The given time interval must not be larger than two hours!
func GetFlightsByInterval(ctx context.Context, begin int64, end int64) ([]FlighData, error) {
var flighDataList []FlighData
conn, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("client: %w", err)
}
if begin <= 0 || end <= 0 {
return nil, ErrInvalidUnixTime
}
requestParams := getFlightsRequestParams("", "", begin, end)
response, err := conn.doGetRequest(ctx, "/flights/all", requestParams)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer response.Body.Close()
if err := response.process(&flighDataList); err != nil {
return nil, err
}
return flighDataList, nil
}
// GetFlightsByAircraft retrieves flights for a particular aircraft within a certain time interval.
// Resulting flights departed and arrived within [begin, end].
// The given time interval must not be larger than 30 days!
func GetFlightsByAircraft(ctx context.Context, icao24 string, begin int64, end int64) ([]FlighData, error) {
var flighDataList []FlighData
conn, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("client: %w", err)
}
if icao24 == "" {
return nil, ErrInvalidAircraftName
}
if begin <= 0 || end <= 0 {
return nil, ErrInvalidUnixTime
}
requestParams := getFlightsRequestParams("", icao24, begin, end)
response, err := conn.doGetRequest(ctx, "/flights/aircraft", requestParams)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer response.Body.Close()
if err := response.process(&flighDataList); err != nil {
return nil, err
}
return flighDataList, nil
}
func getFlightsRequestParams(airport string, aircraft string, begin int64, end int64) url.Values {
requestParams := make(url.Values)
if begin >= 0 {
requestParams.Add("begin", fmt.Sprintf("%d", begin)) //nolint:perfsprint
}
if end >= 0 {
requestParams.Add("end", fmt.Sprintf("%d", end)) //nolint:perfsprint
}
if aircraft != "" {
requestParams.Add("icao24", aircraft)
}
if airport != "" {
requestParams.Add("airport", airport)
}
return requestParams
}