use strict;
use warnings;    ## required because I can't work out how to get percritic to use my modern config

package WebService::Google::Client::Credentials;
$WebService::Google::Client::Credentials::VERSION = '0.06';

# ABSTRACT: Credentials for particular Client instance. You can use this module as singleton also if you need to share
#           credentials between two or more modules


use Carp;
use Moo;
with 'MooX::Singleton';


has 'access_token' => ( is => 'rw' );
has 'user'         => ( is => 'rw', trigger => \&get_access_token_for_user );                             # full gmail, like peter@shotgundriver.com
has 'auth_storage' => ( is => 'rw', default => sub { WebService::Google::Client::AuthStorage->new } );    # dont delete to able to configure


sub get_access_token_for_user
{
  my ( $self ) = @_;
  if ( $self->auth_storage->is_set )
  {                                                                                                       # chech that auth_storage initialized fine
    $self->access_token( $self->auth_storage->get_access_token_from_storage( $self->user ) );
  }
  else
  {
    croak q/Can get access token for specified user because storage isn't set/;
  }
  return $self;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

WebService::Google::Client::Credentials - Credentials for particular Client instance. You can use this module as singleton also if you need to share

=head1 VERSION

version 0.06

=head1 METHODS

=head2 get_access_token_for_user

Automatically get access_token for current user if auth_storage is set

=head1 AUTHORS

=over 4

=item *

Pavel Serikov <pavelsr@cpan.org>

=item *

Peter Scott <peter@localshop.com.au>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017-2018 by Pavel Serikov, Peter Scott and others.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
