From 438319e63a17f3ae5927b274ef491b6e9514f934 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 24 Apr 2019 07:34:41 +0200 Subject: Bump API to v1 --- lib/Travelynx.pm | 1 + lib/Travelynx/Controller/Api.pm | 109 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 276c958..3f8025a 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -1469,6 +1469,7 @@ sub startup { $r->get('/impressum')->to('static#imprint'); $r->get('/imprint')->to('static#imprint'); $r->get('/api/v0/:user_action/:token')->to('api#get_v0'); + $r->get('/api/v1/:user_action/:token')->to('api#get_v1'); $r->get('/login')->to('account#login_form'); $r->get('/register')->to('account#registration_form'); $r->get('/reg/:id/:token')->to('account#verify'); diff --git a/lib/Travelynx/Controller/Api.pm b/lib/Travelynx/Controller/Api.pm index 8e72374..a0b8e07 100755 --- a/lib/Travelynx/Controller/Api.pm +++ b/lib/Travelynx/Controller/Api.pm @@ -66,7 +66,7 @@ sub get_v0 { ) ? \1 : \0, station => { ds100 => $status->{arr_ds100} // $status->{dep_ds100}, - name => $status->{arr_ds100} // $status->{dep_ds100}, + name => $status->{arr_name} // $status->{dep_name}, uic => $station_eva, longitude => $station_lon, latitude => $station_lat, @@ -93,6 +93,113 @@ sub get_v0 { } } +sub get_v1 { + my ($self) = @_; + + my $api_action = $self->stash('user_action'); + my $api_token = $self->stash('token'); + if ( $api_action !~ qr{ ^ (?: status | history | action ) $ }x ) { + $self->render( + json => { + error => 'Invalid action', + }, + ); + return; + } + if ( $api_token !~ qr{ ^ (? \d+ ) - (? .* ) $ }x ) { + $self->render( + json => { + error => 'Malformed token', + }, + ); + return; + } + my $uid = $+{id}; + $api_token = $+{token}; + my $token = $self->get_api_token($uid); + if ( $api_token ne $token->{$api_action} ) { + $self->render( + json => { + error => 'Invalid token', + }, + ); + return; + } + if ( $api_action eq 'status' ) { + my $status = $self->get_user_status($uid); + + my $ret = { + deprecated => \0, + checkedIn => ( + $status->{checked_in} + or $status->{cancelled} + ) ? \1 : \0, + fromStation => { + ds100 => $status->{dep_ds100}, + name => $status->{dep_name}, + uic => undef, + longitude => undef, + latitude => undef, + scheduledTime => $status->{sched_departure}->epoch || undef, + realTime => $status->{real_departure}->epoch || undef, + }, + toStation => { + ds100 => $status->{arr_ds100}, + name => $status->{arr_name}, + uic => undef, + longitude => undef, + latitude => undef, + scheduledTime => $status->{sched_arrival}->epoch || undef, + realTime => $status->{real_arrival}->epoch || undef, + }, + train => { + type => $status->{train_type}, + line => $status->{train_line}, + no => $status->{train_no}, + id => $status->{train_id}, + }, + actionTime => $status->{timestamp}->epoch, + }; + + if ( $status->{dep_ds100} ) { + my @station_descriptions + = Travel::Status::DE::IRIS::Stations::get_station( + $status->{dep_ds100} ); + if ( @station_descriptions == 1 ) { + ( + undef, undef, + $ret->{fromStation}{uic}, + $ret->{fromStation}{longitude}, + $ret->{fromStation}{latitude} + ) = @{ $station_descriptions[0] }; + } + } + + if ( $status->{arr_ds100} ) { + my @station_descriptions + = Travel::Status::DE::IRIS::Stations::get_station( + $status->{arr_ds100} ); + if ( @station_descriptions == 1 ) { + ( + undef, undef, + $ret->{toStation}{uic}, + $ret->{toStation}{longitude}, + $ret->{toStation}{latitude} + ) = @{ $station_descriptions[0] }; + } + } + + $self->render( json => $ret ); + } + else { + $self->render( + json => { + error => 'not implemented', + }, + ); + } +} + sub set_token { my ($self) = @_; if ( $self->validation->csrf_protect->has_error('csrf_token') ) { -- cgit v1.2.3