MatrixTool/Command/client.pm

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