Traewelling: Add missing "Accept: application/json" header to API requests

This commit is contained in:
Daniel Friesel 2020-10-04 12:59:08 +02:00
parent 0b6df3859c
commit c56d126d89

View file

@ -13,10 +13,11 @@ sub new {
my $version = $opt{version}; my $version = $opt{version};
$opt{header} $opt{header} = {
= { 'User-Agent' => 'User-Agent' =>
"travelynx/${version} on $opt{root_url} +https://finalrewind.org/projects/travelynx" "travelynx/${version} on $opt{root_url} +https://finalrewind.org/projects/travelynx",
}; 'Accept' => 'application/json',
};
return bless( \%opt, $class ); return bless( \%opt, $class );
} }
@ -30,6 +31,7 @@ sub get_status_p {
my $header = { my $header = {
'User-Agent' => $self->{header}{'User-Agent'}, 'User-Agent' => $self->{header}{'User-Agent'},
'Accept' => 'application/json',
'Authorization' => "Bearer $token", 'Authorization' => "Bearer $token",
}; };
@ -119,6 +121,7 @@ sub get_user_p {
my $header = { my $header = {
'User-Agent' => $self->{header}{'User-Agent'}, 'User-Agent' => $self->{header}{'User-Agent'},
'Accept' => 'application/json',
'Authorization' => "Bearer $token", 'Authorization' => "Bearer $token",
}; };
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
@ -173,8 +176,9 @@ sub login_p {
my $token; my $token;
$ua->post_p( $ua->post_p(
"https://traewelling.de/api/v0/auth/login" => $self->{header} => "https://traewelling.de/api/v0/auth/login" => $self->{header},
json => $request )->then( json => $request
)->then(
sub { sub {
my ($tx) = @_; my ($tx) = @_;
if ( my $err = $tx->error ) { if ( my $err = $tx->error ) {
@ -240,6 +244,7 @@ sub logout_p {
my $header = { my $header = {
'User-Agent' => $self->{header}{'User-Agent'}, 'User-Agent' => $self->{header}{'User-Agent'},
'Accept' => 'application/json',
'Authorization' => "Bearer $token", 'Authorization' => "Bearer $token",
}; };
my $request = {}; my $request = {};
@ -279,6 +284,7 @@ sub checkin {
my $header = { my $header = {
'User-Agent' => $self->{header}{'User-Agent'}, 'User-Agent' => $self->{header}{'User-Agent'},
'Accept' => 'application/json',
'Authorization' => "Bearer $opt{token}", 'Authorization' => "Bearer $opt{token}",
}; };
@ -323,19 +329,6 @@ sub checkin {
); );
return; return;
} }
# Work around https://github.com/Traewelling/traewelling/issues/128
if ( $tx->res->code == 302 ) {
my $err_msg = "Der Login-Token wurde nicht akzeptiert";
$self->{log}->debug("... error: $err_msg");
$self->{model}->log(
uid => $opt{uid},
message =>
"Fehler bei $opt{train_type} $opt{train_no}: $err_msg",
is_error => 1
);
return;
}
$self->{log}->debug( "... success! " . $tx->res->body ); $self->{log}->debug( "... success! " . $tx->res->body );
# As of 2020-10-04, traewelling.de checkins do not yet return # As of 2020-10-04, traewelling.de checkins do not yet return