Allow usage of custom hafas-rest-api instances
This commit is contained in:
parent
2695442199
commit
ed0054fa50
3 changed files with 18 additions and 6 deletions
|
@ -5,6 +5,12 @@
|
||||||
# 'localhost'.
|
# 'localhost'.
|
||||||
|
|
||||||
{
|
{
|
||||||
|
# travelynx relies on several backend projects. You may override the
|
||||||
|
# defaults to use other (e.g. self-hosted) instances.
|
||||||
|
backend => {
|
||||||
|
hafas_rest_api => 'https://v5.db.transport.rest',
|
||||||
|
},
|
||||||
|
|
||||||
# Cache directories for schedule and realtime data. Mandatory. The parent
|
# Cache directories for schedule and realtime data. Mandatory. The parent
|
||||||
# directory ('/var/cache/travelynx' in this case) must already exist.
|
# directory ('/var/cache/travelynx' in this case) must already exist.
|
||||||
cache => {
|
cache => {
|
||||||
|
|
|
@ -93,6 +93,8 @@ sub startup {
|
||||||
$self->secrets( $self->config->{secrets} );
|
$self->secrets( $self->config->{secrets} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$self->config->{backend}{hafas_rest_api} //= 'https://v5.db.transport.rest';
|
||||||
|
|
||||||
chomp $self->config->{version};
|
chomp $self->config->{version};
|
||||||
|
|
||||||
$self->plugin(
|
$self->plugin(
|
||||||
|
@ -279,6 +281,7 @@ sub startup {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
state $hafas = Travelynx::Helper::HAFAS->new(
|
state $hafas = Travelynx::Helper::HAFAS->new(
|
||||||
log => $self->app->log,
|
log => $self->app->log,
|
||||||
|
hafas_rest_api => $self->app->config->{backend}{hafas_rest_api},
|
||||||
main_cache => $self->app->cache_iris_main,
|
main_cache => $self->app->cache_iris_main,
|
||||||
realtime_cache => $self->app->cache_iris_rt,
|
realtime_cache => $self->app->cache_iris_rt,
|
||||||
root_url => $self->url_for('/')->to_abs,
|
root_url => $self->url_for('/')->to_abs,
|
||||||
|
|
|
@ -30,9 +30,9 @@ sub new {
|
||||||
sub get_polyline_p {
|
sub get_polyline_p {
|
||||||
my ( $self, $train, $trip_id ) = @_;
|
my ( $self, $train, $trip_id ) = @_;
|
||||||
|
|
||||||
my $line = $train->line // 0;
|
my $line = $train->line // 0;
|
||||||
my $url
|
my $backend = $self->{hafas_rest_api};
|
||||||
= "https://v5.db.transport.rest/trips/${trip_id}?lineName=${line}&polyline=true";
|
my $url = "${backend}/trips/${trip_id}?lineName=${line}&polyline=true";
|
||||||
my $cache = $self->{main_cache};
|
my $cache = $self->{main_cache};
|
||||||
my $promise = Mojo::Promise->new;
|
my $promise = Mojo::Promise->new;
|
||||||
my $version = $self->{version};
|
my $version = $self->{version};
|
||||||
|
@ -41,6 +41,9 @@ sub get_polyline_p {
|
||||||
return $promise->resolve($content);
|
return $promise->resolve($content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my $log_url = $url;
|
||||||
|
$log_url =~ s{://\K[^:]+:[^@]+\@}{***@};
|
||||||
|
|
||||||
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
|
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
|
||||||
->then(
|
->then(
|
||||||
sub {
|
sub {
|
||||||
|
@ -48,7 +51,7 @@ sub get_polyline_p {
|
||||||
|
|
||||||
if ( my $err = $tx->error ) {
|
if ( my $err = $tx->error ) {
|
||||||
$promise->reject(
|
$promise->reject(
|
||||||
"hafas->get_polyline_p($url) returned HTTP $err->{code} $err->{message}"
|
"hafas->get_polyline_p($log_url) returned HTTP $err->{code} $err->{message}"
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -97,7 +100,7 @@ sub get_polyline_p {
|
||||||
. ": IRIS route does not agree with HAFAS route: $iris_stations != $hafas_stations"
|
. ": IRIS route does not agree with HAFAS route: $iris_stations != $hafas_stations"
|
||||||
);
|
);
|
||||||
$promise->reject(
|
$promise->reject(
|
||||||
"hafas->get_polyline_p($url): polyline route mismatch");
|
"hafas->get_polyline_p($log_url): polyline route mismatch");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$promise->resolve($ret);
|
$promise->resolve($ret);
|
||||||
|
@ -107,7 +110,7 @@ sub get_polyline_p {
|
||||||
)->catch(
|
)->catch(
|
||||||
sub {
|
sub {
|
||||||
my ($err) = @_;
|
my ($err) = @_;
|
||||||
$promise->reject("hafas->get_polyline_p($url): $err");
|
$promise->reject("hafas->get_polyline_p($log_url): $err");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
)->wait;
|
)->wait;
|
||||||
|
|
Loading…
Reference in a new issue