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 +++++++++++++++++++++++++++++++++++++++- 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{ ^ (? \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') ) { 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 @@ -% my $api_root = $self->url_for('/api/v0')->to_abs->scheme('https'); +% my $api_root = $self->url_for('/api/v1')->to_abs->scheme('https');

Status

@@ -162,22 +162,32 @@

{
"deprecated" : true / false, (falls true: Diese API-Version wird irgendwann abgeschaltet, bitte auf eine neue umsteigen)
- "checked_in" : true / false,
- "station" : {
- "name" : "Essen Hbf", (Name der letzten Station)
- "ds100" : "EE", (DS100-Kürzel)
- "uic" : 8000098, (Internationale Bahnhofsnummer)
- "longitude" : 7.014793,
+ "checkedIn" : true / false,
+ "fromStation" : { (letzter Checkin)
+ "name" : "Essen Hbf",
+ "ds100" : "EE",
+ "uic" : 8000098,
"latitude" : 51.451355,
+ "longitude" : 7.014793,
+ "scheduledTime": 1556083680,
+ "realTime": 1556083680,
+ },
+ "fromStation" : { (zugehöriger Checkout. Wenn noch nicht eingetragen, sind alle Felder null)
+ "name" : "Essen Stadtwald",
+ "ds100" : "EESA",
+ "uic" : 8001896,
+ "latitude" : 51.422853,
+ "longitude" : 7.023296,
+ "scheduledTime": 1556083980, (ggf. null)
+ "realTime": 1556083980, (ggf. null)
},
"train" : {
- "type" : "ICE", (aktueller / letzter Zugtyp)
- "line" : null, (Linie als String, nicht immer numerisch, ggf. null)
- "no" : "1234", (Zugnummer als String)
+ "type" : "S", (aktueller / letzter Zugtyp)
+ "line" : "6", (Linie als String, nicht immer numerisch, ggf. null)
+ "no" : "30634", (Zugnummer als String)
+ "id" : "7512500863736016593", (IRIS-spezifische Zug-ID)
},
"actionTime" : 1234567, (UNIX-Timestamp des letzten Checkin/Checkout)
- "scheduledTime" : 1234567, (UNIX-Timestamp der zugehörigen Ankunft/Abfahrt gemäß Fahrplan. Ggf. 0)
- "realTime" : 1234567, (UNIX-Timestamp der zugehörigen Ankunft/Abfahrt laut Echtzeitdaten. Ggf. 0)
}

-- cgit v1.2.3