abon_client.pm 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use Modern::Perl;
  2. use utf8;
  3. use Mojo::Base -strict, -signatures, -async_await;
  4. our $redis;
  5. our $client;
  6. our $config;
  7. package abon_client;
  8. use darsan_client;
  9. my $auth = abon_auth->new;
  10. sub new($class)
  11. {
  12. my $client = darsan_client->new(undef, $config->{darsan}->{servers});
  13. $client->map($config->{darsan_map}) if $config->{darsan_map};
  14. return bless {client => $client}, $class;
  15. }
  16. our $AUTOLOAD;
  17. sub AUTOLOAD($self, $info, @rest)
  18. {
  19. # Remove qualifier from original method name...
  20. my $called = $AUTOLOAD =~ s/.*:://r;
  21. return $self->{client}->auth($auth->telegram($info->{id}))->$called(@rest);
  22. }
  23. package abon_auth;
  24. sub new($class)
  25. {
  26. return bless {telegram=>undef}, $class;
  27. }
  28. sub telegram($self, $tel_id)
  29. {
  30. $self->{telegram} = $tel_id;
  31. return $self;
  32. }
  33. async sub token_p
  34. {
  35. my $self = shift;
  36. my $key = "ab-$self->{telegram}-token";
  37. my $tok = $redis->get($key);
  38. return $tok if $tok;
  39. my $res = await $client->post_p("client", "/telegram/$self->{telegram}/token");
  40. $redis->set($key, $res->{token});
  41. $redis->expireat($key, darsan_auth::parse_time($res->{expires})-5);
  42. return $res->{token};
  43. }
  44. 1;