diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-04-24 07:34:41 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-04-24 07:34:41 +0200 |
commit | 438319e63a17f3ae5927b274ef491b6e9514f934 (patch) | |
tree | a3df2c748d67d485d3a0080a9dd2ec710f620b60 | |
parent | 906ec60ea201f358993b52a6d73041e2a4fe4a3c (diff) |
Bump API to v1
-rwxr-xr-x | lib/Travelynx.pm | 1 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Api.pm | 109 | ||||
-rw-r--r-- | templates/account.html.ep | 34 |
3 files changed, 131 insertions, 13 deletions
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{ ^ (?<id> \d+ ) - (?<token> .* ) $ }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') ) { diff --git a/templates/account.html.ep b/templates/account.html.ep index 5a19c90..90c64fc 100644 --- a/templates/account.html.ep +++ b/templates/account.html.ep @@ -144,7 +144,7 @@ </div> </div> -% my $api_root = $self->url_for('/api/v0')->to_abs->scheme('https'); +% my $api_root = $self->url_for('/api/v1')->to_abs->scheme('https'); <h3>Status</h3> <div class="row"> <div class="col s12"> @@ -162,22 +162,32 @@ <p style="font-family: Monospace;"> {<br/> "deprecated" : true / false, (falls true: Diese API-Version wird irgendwann abgeschaltet, bitte auf eine neue umsteigen)<br/> - "checked_in" : true / false,<br/> - "station" : {<br/> - "name" : "Essen Hbf", (Name der letzten Station)<br/> - "ds100" : "EE", (DS100-Kürzel)<br/> - "uic" : 8000098, (Internationale Bahnhofsnummer)<br/> - "longitude" : 7.014793,<br/> + "checkedIn" : true / false,<br/> + "fromStation" : { (letzter Checkin)<br/> + "name" : "Essen Hbf",<br/> + "ds100" : "EE",<br/> + "uic" : 8000098,<br/> "latitude" : 51.451355,<br/> + "longitude" : 7.014793,<br/> + "scheduledTime": 1556083680,<br/> + "realTime": 1556083680,<br/> + },<br/> + "fromStation" : { (zugehöriger Checkout. Wenn noch nicht eingetragen, sind alle Felder null)<br/> + "name" : "Essen Stadtwald",<br/> + "ds100" : "EESA",<br/> + "uic" : 8001896,<br/> + "latitude" : 51.422853,<br/> + "longitude" : 7.023296,<br/> + "scheduledTime": 1556083980, (ggf. null)<br/> + "realTime": 1556083980, (ggf. null)<br/> },<br/> "train" : {<br/> - "type" : "ICE", (aktueller / letzter Zugtyp)<br/> - "line" : null, (Linie als String, nicht immer numerisch, ggf. null)<br/> - "no" : "1234", (Zugnummer als String)<br/> + "type" : "S", (aktueller / letzter Zugtyp)<br/> + "line" : "6", (Linie als String, nicht immer numerisch, ggf. null)<br/> + "no" : "30634", (Zugnummer als String)<br/> + "id" : "7512500863736016593", (IRIS-spezifische Zug-ID)<br/> },<br/> "actionTime" : 1234567, (UNIX-Timestamp des letzten Checkin/Checkout)<br/> - "scheduledTime" : 1234567, (UNIX-Timestamp der zugehörigen Ankunft/Abfahrt gemäß Fahrplan. Ggf. 0)<br/> - "realTime" : 1234567, (UNIX-Timestamp der zugehörigen Ankunft/Abfahrt laut Echtzeitdaten. Ggf. 0)<br/> } </p> <p> |