Traewelling: use the recently added login token expiry field

This commit is contained in:
Daniel Friesel 2020-10-17 11:03:47 +02:00
parent e61550f751
commit 8123797cf1

View file

@ -18,10 +18,30 @@ sub new {
"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', 'Accept' => 'application/json',
}; };
$opt{strp1} = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S.000000Z',
time_zone => 'UTC',
);
$opt{strp2} = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S',
time_zone => 'Europe/Berlin',
);
$opt{strp3} = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S%z',
time_zone => 'Europe/Berlin',
);
return bless( \%opt, $class ); return bless( \%opt, $class );
} }
sub parse_datetime {
my ( $self, $dt ) = @_;
return $self->{strp1}->parse_datetime($dt)
// $self->{strp2}->parse_datetime($dt)
// $self->{strp3}->parse_datetime($dt);
}
sub get_status_p { sub get_status_p {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
@ -47,29 +67,14 @@ sub get_status_p {
} }
else { else {
if ( my $status = $tx->result->json->{statuses}{data}[0] ) { if ( my $status = $tx->result->json->{statuses}{data}[0] ) {
my $strp1 = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S.000000Z',
time_zone => 'UTC',
);
my $strp2 = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S',
time_zone => 'Europe/Berlin',
);
my $status_id = $status->{id}; my $status_id = $status->{id};
my $message = $status->{body}; my $message = $status->{body};
my $checkin_at my $checkin_at
= $strp1->parse_datetime( $status->{created_at} ) = $self->parse_datetime( $status->{created_at} );
// $strp2->parse_datetime( $status->{created_at} );
my $dep_dt my $dep_dt = $self->parse_datetime(
= $strp1->parse_datetime(
$status->{train_checkin}{departure} )
// $strp2->parse_datetime(
$status->{train_checkin}{departure} ); $status->{train_checkin}{departure} );
my $arr_dt my $arr_dt = $self->parse_datetime(
= $strp1->parse_datetime(
$status->{train_checkin}{arrival} )
// $strp2->parse_datetime(
$status->{train_checkin}{arrival} ); $status->{train_checkin}{arrival} );
my $dep_eva my $dep_eva
@ -198,11 +203,12 @@ sub login_p {
return; return;
} }
else { else {
$token = $tx->result->json->{token}; my $res = $tx->result->json;
$token = $res->{token};
my $expiry_dt = $self->parse_datetime( $res->{expires_at} );
# As of 2020-10-04, Traewelling tokens expire one year after they # Fall back to one year expiry
# are generated $expiry_dt //= DateTime->now( time_zone => 'Europe/Berlin' )
my $expiry_dt = DateTime->now( time_zone => 'Europe/Berlin' )
->add( years => 1 ); ->add( years => 1 );
$self->{model}->link( $self->{model}->link(
uid => $uid, uid => $uid,