Bump API to v1

This commit is contained in:
Daniel Friesel 2019-04-24 07:34:41 +02:00
parent 906ec60ea2
commit 438319e63a
3 changed files with 131 additions and 13 deletions

View file

@ -1469,6 +1469,7 @@ sub startup {
$r->get('/impressum')->to('static#imprint'); $r->get('/impressum')->to('static#imprint');
$r->get('/imprint')->to('static#imprint'); $r->get('/imprint')->to('static#imprint');
$r->get('/api/v0/:user_action/:token')->to('api#get_v0'); $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('/login')->to('account#login_form');
$r->get('/register')->to('account#registration_form'); $r->get('/register')->to('account#registration_form');
$r->get('/reg/:id/:token')->to('account#verify'); $r->get('/reg/:id/:token')->to('account#verify');

View file

@ -66,7 +66,7 @@ sub get_v0 {
) ? \1 : \0, ) ? \1 : \0,
station => { station => {
ds100 => $status->{arr_ds100} // $status->{dep_ds100}, ds100 => $status->{arr_ds100} // $status->{dep_ds100},
name => $status->{arr_ds100} // $status->{dep_ds100}, name => $status->{arr_name} // $status->{dep_name},
uic => $station_eva, uic => $station_eva,
longitude => $station_lon, longitude => $station_lon,
latitude => $station_lat, 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 { sub set_token {
my ($self) = @_; my ($self) = @_;
if ( $self->validation->csrf_protect->has_error('csrf_token') ) { if ( $self->validation->csrf_protect->has_error('csrf_token') ) {

View file

@ -144,7 +144,7 @@
</div> </div>
</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> <h3>Status</h3>
<div class="row"> <div class="row">
<div class="col s12"> <div class="col s12">
@ -162,22 +162,32 @@
<p style="font-family: Monospace;"> <p style="font-family: Monospace;">
{<br/> {<br/>
"deprecated" : true / false, (falls true: Diese API-Version wird irgendwann abgeschaltet, bitte auf eine neue umsteigen)<br/> "deprecated" : true / false, (falls true: Diese API-Version wird irgendwann abgeschaltet, bitte auf eine neue umsteigen)<br/>
"checked_in" : true / false,<br/> "checkedIn" : true / false,<br/>
"station" : {<br/> "fromStation" : { (letzter Checkin)<br/>
"name" : "Essen Hbf", (Name der letzten Station)<br/> "name" : "Essen Hbf",<br/>
"ds100" : "EE", (DS100-Kürzel)<br/> "ds100" : "EE",<br/>
"uic" : 8000098, (Internationale Bahnhofsnummer)<br/> "uic" : 8000098,<br/>
"longitude" : 7.014793,<br/>
"latitude" : 51.451355,<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/> },<br/>
"train" : {<br/> "train" : {<br/>
"type" : "ICE", (aktueller / letzter Zugtyp)<br/> "type" : "S", (aktueller / letzter Zugtyp)<br/>
"line" : null, (Linie als String, nicht immer numerisch, ggf. null)<br/> "line" : "6", (Linie als String, nicht immer numerisch, ggf. null)<br/>
"no" : "1234", (Zugnummer als String)<br/> "no" : "30634", (Zugnummer als String)<br/>
"id" : "7512500863736016593", (IRIS-spezifische Zug-ID)<br/>
},<br/> },<br/>
"actionTime" : 1234567, (UNIX-Timestamp des letzten Checkin/Checkout)<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>
<p> <p>