blib/lib/App/MatrixTool/Command/client.pm

filter: Statementstmt 53.1% 17 / 32 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 2 totaltotal 34.3% 22 / 64 SCAR 34.5
CC
9
Cov
35%
do_json
56.0
run
12.0
BEGIN
1.0
CRAP 31.6
« blib/lib/App/MatrixTool/Command/notary.pm ↑ Summary blib/lib/App/MatrixTool/Command/client/json.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2016 -- leonerd@leonerd.org.uk
5  
6 package App::MatrixTool::Command::client;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base qw( App::MatrixTool::SubCommands );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.06';
13  
14 1 ▶use constant DESCRIPTION => "Commandline client utilities";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 use constant OPTIONS => (
16 's|server=s' => "Server",
17 'u|user-id=s' => "User ID",
18 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 matrixtool client - Commandline client utilities for a Matrix homeserver
23  
24 =head1 SYNOPSIS
25  
26 $ matrixtool client login @username:example.com
27  
28 =head1 DESCRIPTION
29  
30 This command provides a commandline interface to various client APIs on a
31 Matrix homeserver. It has various sub-commands that provide particular
32 functionality, along with some common options between them.
33  
34 These sub-commands offers a convenience over using simpler tools like F<curl>
35 directly because they will automatically fill in details like access tokens
36 and server hostnames, when given a C<user-id> argument. This works by storing
37 a cache of access tokens in the user's home directory, under
38 F<.matrix/client-tokens>.
39  
40 By using the C<matrixtool client login> command you can add an access token
41 for your user account into this cache:
42  
43 $ matrixtool client login @me:example.com
44 Password:
45 [OK] Obtained access token
46  
47 Having logged in, you can now use the C<-u> option to other
48 C<matrixtool client> subcommands, causing it to automatically supply this
49 access token on every request:
50  
51 $ matrixtool client -u @me:example.com json /_matrix/client/r0/publicRooms
52 ...
53  
54 =head1 OPTIONS
55  
56 The following additional options are recognised
57  
58 =over 4
59  
60 =item C<--user-id>, C<-u>
61  
62 User ID to supply the C<access_token> of.
63  
64 =item C<--server>, C<-s>
65  
66 Server to connect to. Usually optional, as it is implied by the C<user-id> if
67 provided.
68  
69 =back
70  
71 =head1 COMMANDS
72  
73 The following sub-commands are recognised
74  
75 =head2 json
76  
77 Perform a direct JSON request
78  
79 $ matrixtool client json PATH [DATA]
80  
81 See also L<App::MatrixTool::Command::client::json>.
82  
83 =head2 login
84  
85 Obtain a client authentication token
86  
87 $ matrixtool client login USER-ID
88  
89 See also L<App::MatrixTool::Command::client::login>.
90  
91 =head2 upload
92  
93 Upload a file to the media repository
94  
95 $ matrixtool client upload FILE [TYPE]
96  
97 See also L<App::MatrixTool::Command::client::upload>.
98  
99 =cut
100  
101 sub run
102 {
103 0 ▶ my $self = shift;
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
104 0 my ( $opts, @args ) = @_;
105  
106 0 ▶ $self->{$_} //= $opts->{$_} for qw( server user_id );
Condition0%0/3
$self->{$_} //= $opts->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$_} //= $opts->{$_}
$self->{$_} $opts->{$_}
Truth table0%0/3
A //= B
A= $self->{$_}B= $opts->{$_}
ABresult
1X1
011
000
107  
108 0 return $self->SUPER::run( @args );
109 }
110  
111 sub do_json
112 {
113 0 ▶ my $self = shift;
Subroutine do_json: not called CC=7 SCAR=40.3
Pod undocumented
114 0 my ( $method, $path, %opts ) = @_;
115  
116 0 my $client = $self->http_client;
117  
118 0 ▶ if( my $user_id = $self->{user_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user_id = $self->{"user_id"}) 0 0 0
119 0 my ( $server ) = $user_id =~ m/^@.*?:(.*)$/;
120 0 ▶ $self->{server} //= $server;
Condition0%0/3
$self->{"server"} //= $server
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"server"} //= $server
$self->{"server"} $server
Truth table0%0/3
A //= B
A= $self->{"server"}B= $server
ABresult
1X1
011
000
121 }
122  
123 defined $self->{server} or
124 0 ▶ die "Not sure what --server to use\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"server"} or die "Not sure what --server to use\n" 0 0 0
125  
126 0 ▶ if( $self->{server} && $self->{user_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"server"} and $self->{"user_id"}) 0 0 0
Condition0%0/3
$self->{"server"} and $self->{"user_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"server"} and $self->{"user_id"}
$self->{"server"} $self->{"user_id"}
Truth table0%0/3
A and B
A= $self->{"server"}B= $self->{"user_id"}
ABresult
0X0
100
111
127 my $token = $self->client_token_store->get(
128 server => $self->{server},
129 id => $self->{user_id},
130 0 );
131  
132 0 ▶ $opts{params}{access_token} = $token if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 0 0 0
133 }
134  
135 $client->request_json(
136 server => $self->{server},
137 0 method => $method,
138 path => $path,
139 %opts,
140 );
141 }
142  
143 =head1 AUTHOR
144  
145 Paul Evans <leonerd@leonerd.org.uk>
146  
147 =cut
148  
149 1 0x55AA;
« blib/lib/App/MatrixTool/Command/notary.pm ↑ Summary blib/lib/App/MatrixTool/Command/client/json.pm »