API.pm

filter: Statementstmt 31.0% 36 / 116 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 54.5% 12 / 22 Podpod n/a totaltotal 22.4% 48 / 214 CC 35 SCAR 33.1
CC
35
Cov
20%
_get_response
56.0
get_states
42.0
_url
42.0
CRAP 27.4
↑ Summary API/Core/StateVector.pm »
1 # see also https://github.com/openskynetwork/opensky-api
2  
3 package OpenSky::API;
4  
5 # ABSTRACT: Perl interface to the OpenSky Network API
6  
7 our $VERSION = '0.005';
8 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use OpenSky::API::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 ArrayRef
11 Bool
12 Dict
13 HashRef
14 InstanceOf
15 Int
16 Latitude
17 Longitude
18 NonEmptyStr
19 Num
20 Optional
21 5 );
22 5 ▶use OpenSky::API::States;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use OpenSky::API::Flights;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Config::INI::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use Mojo::JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use Type::Params -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 warnings::warnif( 'deprecated', 'OpenSky::API is deprecated and should no longer be used. Please use WebService::OpenSky instead.' );
35  
36 has config => (
37 is => 'ro',
38 isa => NonEmptyStr,
39 default => sub ($self) { $ENV{HOME} . '/.openskyrc' },
40 );
41  
42 has [qw/debug raw testing/] => (
43 is => 'ro',
44 isa => Bool,
45 default => 0,
46 );
47  
48 has _config_data => (
49 is => 'ro',
50 isa => HashRef,
51 init_arg => undef,
52 lazy => 1,
53 default => sub ($self) {
54 Config::INI::Reader->read_file( $self->config );
55 },
56 );
57  
58 has _ua => (
59 is => 'ro',
60 isa => InstanceOf ['Mojo::UserAgent'],
61 init_arg => undef,
62 default => sub { Mojo::UserAgent->new },
63 );
64  
65 has _username => (
66 is => 'ro',
67 isa => NonEmptyStr,
68 lazy => 1,
69 init_arg => 'username',
70 default => sub ($self) { $ENV{OPENSKY_USERNAME} // $self->_config_data->{opensky}{username} },
71 );
72  
73 has _password => (
74 is => 'ro',
75 isa => NonEmptyStr,
76 lazy => 1,
77 init_arg => 'password',
78 default => sub ($self) { $ENV{OPENSKY_PASSWORD} // $self->_config_data->{opensky}{password} },
79 );
80  
81 has _base_url => (
82 is => 'ro',
83 init_arg => 'base_url',
84 isa => NonEmptyStr,
85 default => sub ($self) {'https://opensky-network.org/api'},
86 );
87  
88 has limit_remaining => (
89 is => 'ro',
90 isa => Int,
91 lazy => 1,
92 writer => '_set_limit_remaining',
93 default => sub ($self) {
94 return 4000 if $self->testing;
95  
96 # per their documentation,
97 # https://openskynetwork.github.io/opensky-api/rest.html#api-credit-usage,
98 # this request should only cost one credit. However, it appears to
99 # cost three.
100 my %params = (
101 lamin => 49.7,
102 lomin => 3.2,
103 lamax => 50.5,
104 lomax => 4.6,
105 );
106 my $route = '/states/all';
107 return $self->_get_response( route => $route, params => \%params, credits => 1 );
108 },
109 );
110  
111 signature_for get_states => (
112 method => 1,
113 named => [
114 time => Optional [Num], { default => 0 },
115 icao24 => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ],
116 bbox => Optional [
117 Dict [
118 lamin => Latitude,
119 lomin => Longitude,
120 lamax => Latitude,
121 lomax => Longitude,
122 ],
123 { default => {} },
124 ],
125 extended => Optional [Bool],
126 ],
127 named_to_list => 1,
128 );
129  
130 0 ▶sub get_states ( $self, $seconds, $icao24, $bbox, $extended ) {
Subroutine get_states: not called CC=6 SCAR=37.4
131 0 my %params = (
132 maybe time => $seconds,
133 maybe icao24 => $icao24,
134 maybe extended => $extended,
135 );
136 0 ▶ if ( keys $bbox->%* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$bbox) 0 0 0
137 0 $params{$_} = $bbox->{$_} for qw( lamin lomin lamax lomax );
138 }
139  
140 0 my $route = '/states/all';
141 0 ▶ my $response = $self->_get_response( route => $route, params => \%params, no_auth_required => 1 ) // {
Condition0%0/4
$self->_get_response("route", $route, "params", \%params, "no_auth_required", 1) // {"time", time - ($seconds // 0), "states", []}
l!l
00
$seconds // 0
l!l
00
MC/DC0%0/1
$self->_get_response("route", $route, "params", \%params, "no_auth_required", 1) // {"time", time - ($seconds // 0), "states", []}
$self->_get_response("route", $route, "params", \%params, "no_auth_required", 1)
MC/DC0%0/1
$seconds // 0
$seconds
Truth table0%0/2
A
A= $self->_get_response("route", $route, "params", \%params, "no_auth_required", 1)
Aresult
11
00
Truth table0%0/2
A
A= $seconds
Aresult
11
00
142 time => time - ( $seconds // 0 ),
143 states => [],
144 };
145 0 ▶ if ( $self->raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 0 0
146 0 return $response;
147 }
148 0 return OpenSky::API::States->new($response);
149 }
150  
151 signature_for get_my_states => (
152 method => 1,
153 named => [
154 time => Optional [Num], { default => 0 },
155 icao24 => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ],
156 serials => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ],
157 ],
158 named_to_list => 1,
159 );
160  
161 0 ▶sub get_my_states ( $self, $seconds, $icao24, $serials ) {
Subroutine get_my_states: not called CC=2 SCAR=17.9
162 0 my %params = (
163 extended => 1,
164 maybe time => $seconds,
165 maybe icao24 => $icao24,
166 maybe serials => $serials,
167 );
168  
169 0 my $route = '/states/own';
170 0 my $response = $self->_get_response( route => $route, params => \%params );
171 0 ▶ if ( $self->raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 0 0
172 0 return $response;
173 }
174 0 return OpenSky::API::States->new($response);
175 }
176  
177 0 ▶sub get_flights_from_interval ( $self, $begin, $end ) {
Subroutine get_flights_from_interval: not called CC=5 SCAR=34.0
178 0 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 0 0 0
179 0 croak 'The end time must be greater than or equal to the start time.';
180 }
181 0 ▶ if ( ( $end - $begin ) > 7200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 7200) 0 0 0
182 0 croak 'The time interval must be smaller than two hours.';
183 }
184  
185 0 my %params = ( begin => $begin, end => $end );
186 0 my $route = '/flights/all';
187 0 ▶ my $response = $self->_get_response( route => $route, params => \%params ) // [];
Condition0%0/2
$self->_get_response("route", $route, "params", \%params) // []
l!l
00
MC/DC0%0/1
$self->_get_response("route", $route, "params", \%params) // []
$self->_get_response("route", $route, "params", \%params)
Truth table0%0/2
A
A= $self->_get_response("route", $route, "params", \%params)
Aresult
11
00
188  
189 0 ▶ if ( $self->raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 0 0
190 0 return $response;
191 }
192 0 return OpenSky::API::Flights->new($response);
193 }
194  
195 0 ▶sub get_flights_by_aircraft ( $self, $icao24, $begin, $end ) {
Subroutine get_flights_by_aircraft: not called CC=5 SCAR=34.0
196 0 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 0 0 0
197 0 croak 'The end time must be greater than or equal to the start time.';
198 }
199 0 ▶ if ( ( $end - $begin ) > 2592 * 1e3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 2592000) 0 0 0
200 0 croak 'The time interval must be smaller than 30 days.';
201 }
202  
203 0 my %params = ( icao24 => $icao24, begin => $begin, end => $end );
204 0 my $route = '/flights/aircraft';
205 0 ▶ my $response = $self->_get_response( route => $route, params => \%params ) // [];
Condition0%0/2
$self->_get_response("route", $route, "params", \%params) // []
l!l
00
MC/DC0%0/1
$self->_get_response("route", $route, "params", \%params) // []
$self->_get_response("route", $route, "params", \%params)
Truth table0%0/2
A
A= $self->_get_response("route", $route, "params", \%params)
Aresult
11
00
206  
207 0 ▶ if ( $self->raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 0 0
208 0 return $response;
209 }
210 0 return OpenSky::API::Flights->new($response);
211 }
212  
213 0 ▶sub get_arrivals_by_airport ( $self, $airport, $begin, $end ) {
Subroutine get_arrivals_by_airport: not called CC=5 SCAR=34.0
214 0 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 0 0 0
215 0 croak 'The end time must be greater than or equal to the start time.';
216 }
217 0 ▶ if ( ( $end - $begin ) > 604800 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 604800) 0 0 0
218 0 croak 'The time interval must be smaller than 7 days.';
219 }
220  
221 0 my %params = ( airport => $airport, begin => $begin, end => $end );
222 0 my $route = '/flights/arrival';
223 0 ▶ my $response = $self->_get_response( route => $route, params => \%params ) // [];
Condition0%0/2
$self->_get_response("route", $route, "params", \%params) // []
l!l
00
MC/DC0%0/1
$self->_get_response("route", $route, "params", \%params) // []
$self->_get_response("route", $route, "params", \%params)
Truth table0%0/2
A
A= $self->_get_response("route", $route, "params", \%params)
Aresult
11
00
224  
225 0 ▶ if ( $self->raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 0 0
226 0 return $response;
227 }
228 0 return OpenSky::API::Flights->new($response);
229 }
230  
231 0 ▶sub get_departures_by_airport ( $self, $airport, $begin, $end ) {
Subroutine get_departures_by_airport: not called CC=5 SCAR=34.0
232 0 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 0 0 0
233 0 croak 'The end time must be greater than or equal to the start time.';
234 }
235 0 ▶ if ( ( $end - $begin ) > 604800 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 604800) 0 0 0
236 0 croak 'The time interval must be smaller than 7 days.';
237 }
238  
239 0 my %params = ( airport => $airport, begin => $begin, end => $end );
240 0 my $route = '/flights/departure';
241 0 ▶ my $response = $self->_get_response( route => $route, params => \%params ) // [];
Condition0%0/2
$self->_get_response("route", $route, "params", \%params) // []
l!l
00
MC/DC0%0/1
$self->_get_response("route", $route, "params", \%params) // []
$self->_get_response("route", $route, "params", \%params)
Truth table0%0/2
A
A= $self->_get_response("route", $route, "params", \%params)
Aresult
11
00
242  
243 0 ▶ if ( $self->raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 0 0
244 0 return $response;
245 }
246 0 return OpenSky::API::Flights->new($response);
247 }
248  
249 signature_for _get_response => (
250 method => 1,
251 named => [
252 route => NonEmptyStr,
253 params => Optional [HashRef],
254 credits => Optional [Bool],
255 no_auth_required => Optional [Bool],
256 ],
257 named_to_list => 1,
258 );
259  
260 0 ▶sub _get_response ( $self, $route, $params, $credits, $no_auth_required ) {
Subroutine _get_response: not called CC=7 SCAR=40.3
261 0 my $url = $self->_url( $route, $params, $no_auth_required );
262  
263 0 my $response = $self->_GET($url);
264 0 my $remaining = $response->headers->header('X-Rate-Limit-Remaining');
265  
266 0 $self->_debug( $response->headers->to_string . "\n" );
267  
268 # not all requests cost credits, so we only want to set the limit if
269 # $remaining is defined
270 0 ▶ $self->_set_limit_remaining($remaining) if !$credits && defined $remaining;
Branch: 1 branch
BranchTrueFalseTotal
if not $credits and defined $remaining 0 0 0
Condition0%0/3
not $credits and defined $remaining
!ll&&!rl&&r
000
MC/DC0%0/2
not $credits and defined $remaining
not $credits defined $remaining
Truth table0%0/3
not(A) and B
A= not $creditsB= defined $remaining
ABresult
0X0
100
111
271 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
272 0 ▶ if ( $response->code == 404 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 404) 0 0 0
273  
274 # this is annoying. If the didn't match any criteria, return a 200
275 # and and empty element. Instead, we get a 404.
276 0 return;
277 }
278 0 croak $response->to_string;
279 }
280 0 ▶ return $remaining if $credits;
Branch: 1 branch
BranchTrueFalseTotal
if $credits 0 0 0
281 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
282 0 $self->_debug( $response->body );
283 }
284 0 return decode_json( $response->body );
285 }
286  
287 # an easy target to override for testing
288 0 ▶sub _GET ( $self, $url ) {
Subroutine _GET: not called CC=1 SCAR=6.9
289 0 $self->_debug("GET $url\n");
290 0 return $self->_ua->get($url)->res;
291 }
292  
293 0 ▶sub _debug ( $self, $msg ) {
Subroutine _debug: not called CC=2 SCAR=17.9
294 0 ▶ return if !$self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 0 0 0
295 0 say STDERR $msg;
296 }
297  
298 0 ▶sub _url ( $self, $url, $params = {}, $no_auth_required = 0 ) {
Subroutine _url: not called CC=6 SCAR=37.4
299 0 my $username = $self->_username;
300 0 my $password = $self->_password;
301 0 ▶ if ( ( !$username || !$password ) && $no_auth_required ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $username && $password and $no_auth_required) 0 0 0
Condition0%0/6
not $username && $password and $no_auth_required
!ll&&!rl&&r
000
$username && $password
!ll&&!rl&&r
000
MC/DC0%0/3
not $username && $password and $no_auth_required
$username $password $no_auth_required
Truth table0%0/5
not(A && B) and C
A= $usernameB= $passwordC= $no_auth_required
ABCresult
11X0
0X00
1000
0X11
1011
302 0 return Mojo::URL->new( $self->_base_url . $url )->query($params);
303 }
304 0 $url = Mojo::URL->new( $self->_base_url . $url )->userinfo( $self->_username . ':' . $self->_password );
305 0 $url->query($params);
306 0 return $url;
307 }
308  
309 __PACKAGE__->meta->make_immutable;
310  
311 __END__
↑ Summary API/Core/StateVector.pm »