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

filter: Statementstmt 61.1% 22 / 36 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 1 totaltotal 52.8% 28 / 53 SCAR 20.9
CC
5
Cov
50%
run
30.0
__ANON__
2.0
BEGIN
1.0
CRAP 8.1
« blib/lib/App/MatrixTool/Command/client.pm ↑ Summary blib/lib/App/MatrixTool/Command/client/upload.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::json;
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::Command::client );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.06';
13  
14 1 ▶use constant DESCRIPTION => "Perform a direct JSON request";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant ARGUMENTS => ( "path", "data?" );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 use constant OPTIONS => (
17 'm|method=s' => "HTTP method",
18 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 require JSON;
21 1 my $JSON = JSON->new->utf8(1)->pretty(1);
22  
23 =head1 NAME
24  
25 matrixtool client json - Perform a direct JSON request
26  
27 =head1 SYNOPSIS
28  
29 $ matrixtool client -u @me:example.com json /_matrix/client/r0/publicRooms
30  
31 =head1 DESCRIPTION
32  
33 This command performs a direct low-level HTTP request to a Matrix homeserver.
34 The server name part of the URL is automatically inferred from the given user
35 ID; the I<PATH> commandline argument only requires the path and optional query
36 parameters. If an access token is known for the user (see
37 C<matrixtool client login>) then it is automatically added to the query
38 parameters as well.
39  
40 An optional second argument, I<DATA>, may be provided. If so, this should
41 contain a JSON encoding of data to supply with the request, turning it into a
42 C<PUT> request. If no data is supplied, then a C<GET> request is performed
43 instead.
44  
45 The resulting JSON data from the homeserver is parsed and re-printed in a more
46 human-readable form to standard output. Linefeeds and indentation whitespace
47 are used to increase readability.
48  
49 =head1 OPTIONS
50  
51 =over 4
52  
53 =item C<--method>, C<-m>
54  
55 Use a different HTTP method. If not specified, C<GET> or C<PUT> will be
56 performed, depending on whether the I<DATA> argument was supplied.
57  
58 =back
59  
60 =cut
61  
62 sub run
63 {
64 0 ▶ my $self = shift;
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
65 0 my ( $opts, $pathquery, $data ) = @_;
66  
67 0 my $method = "GET";
68 0 ▶ $method = "PUT" if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
69  
70 0 ▶ $method = $opts->{method} if defined $opts->{method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"method"} 0 0 0
71  
72 0 my %opts;
73  
74 0 ▶ $opts{content} = $JSON->decode( $data ) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
75  
76 0 my $uri = URI->new( $pathquery );
77 0 ▶ if( $uri->query_form ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query_form) 0 0 0
78 0 $opts{params} = { $uri->query_form };
79 }
80  
81 $self->do_json( $method, $uri->path, %opts )->then( sub {
82 0 ▶ my ( $body, $response ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
83  
84 0 print $JSON->encode( $body ) . "\n";
85 0 Future->done();
86 0 });
87 }
88  
89 =head1 EXAMPLES
90  
91 For example, directly querying your user profile data:
92  
93 $ matrixtool client -u @me:example.com json \
94 /_matrix/client/r0/profile/@me:example.com
95 {
96 "avatar_url": "mxc://example.com/aBcDeFgHiJ...",
97 "displayname": "Mr Example",
98 }
99  
100 By supplying a second parameter containing JSON-encoded data, we can perform
101 a C<PUT> request to update the displayname:
102  
103 $ matrixtool client -u @me:example.com json \
104 /_matrix/client/r0/profile/@me:example.com/displayname \
105 '{"displayname":"Mr. Example"}'
106 {}
107  
108 =cut
109  
110 =head1 AUTHOR
111  
112 Paul Evans <leonerd@leonerd.org.uk>
113  
114 =cut
115  
116 1 0x55AA;
« blib/lib/App/MatrixTool/Command/client.pm ↑ Summary blib/lib/App/MatrixTool/Command/client/upload.pm »