App-bsky

 view release on metacpan or  search on metacpan

lib/App/bsky.pm  view on Meta::CPAN

package App::bsky 0.05 {
    use v5.38;
    use utf8;
    use Bluesky;
    use experimental 'class', 'try';
    no warnings 'experimental';
    use open qw[:std :encoding(UTF-8)];
    $|++;
    class App::bsky::CLI 0.05 {
        use JSON::Tiny qw[/code_json/];
        use Path::Tiny;
        use File::HomeDir;
        use Getopt::Long qw[GetOptionsFromArray];
        use Term::ANSIColor;
        #
        field $bsky = Bluesky->new();
        field $config;
        field $config_file : param //= path( File::HomeDir->my_data )->absolute->child('.bsky');
        #
        ADJUST {
            if ( $^O eq 'MSWin32' ) {
                try {
                    require Win32::Console;
                    Win32::Console::OutputCP(65001);
                }
                catch ($e) {

                    #~ warn $e;
                    #~ warn 'We may have issues with non-ASCII display';
                }
                binmode STDOUT, ':encoding(UTF-8)';
                binmode STDERR, ':encoding(UTF-8)';
            }
            $self->get_config;
            if ( defined $config->{resume}{accessJwt} && defined $config->{resume}{refreshJwt} ) {
                my $res = $bsky->resume(
                    $config->{resume}{accessJwt},
                    $config->{resume}{refreshJwt},
                    $config->{resume}{token_type} // 'Bearer',
                    $config->{resume}{dpop_key_jwk},
                    $config->{resume}{client_id},
                    $config->{resume}{handle},
                    $config->{resume}{pds},
                    $config->{resume}{scope}
                );

                # If resume automatically refreshed, update our config
                # Also, if the session is expired, try to refresh it manually
                if ( $bsky->session && builtin::blessed( $bsky->session ) && $bsky->session->isa('At::Protocol::Session') ) {
                    my $access = $bsky->at->_decode_token( $bsky->session->accessJwt );
                    if ( $access && time > $access->{exp} ) {
                        $bsky->at->oauth_refresh;
                    }
                    $config->{resume} = $bsky->session->_raw;
                    $self->put_config;
                }
            }
            elsif ( defined $config->{login}{identifier} && defined $config->{login}{password} ) {
                if ( $bsky->login( $config->{login}{identifier}, $config->{login}{password} ) &&
                    builtin::blessed( $bsky->session ) &&
                    $bsky->session->isa('At::Protocol::Session') ) {
                    $config->{resume} = $bsky->session->_raw;
                    $self->put_config;
                }
            }
            $config->{session}
                = ( $bsky->session && builtin::blessed( $bsky->session ) && $bsky->session->isa('At::Protocol::Session') ) ? $bsky->session->_raw :
                undef;
            $config->{settings} //= { wrap => 0 };
            $self->put_config;
        }

        method config() {
            $self->get_config if !$config && $config_file->is_file && $config_file->size;
            $config;
        }

        method DESTROY ( $global //= 0 ) {
            return unless $config;

            #~ $self->put_config;
        }
        #
        method get_config() {
            $config = ( $config_file->is_file && $config_file->size ) ? decode_json $config_file->slurp_utf8 : {};
        }
        method put_config() { $config_file->spew_utf8( JSON::Tiny::to_json $config ); }

        sub _wrap_and_indent {
            my ( $width, $indent, $string ) = @_;
            my $size        = $width - $indent;
            my $indentation = ' ' x $indent;



( run in 1.347 second using v1.01-cache-2.11-cpan-140bd7fdf52 )