File Coverage

blib/lib/App/MatrixTool/Command/client/json.pm
Criterion Covered Total %
statement 18 32 56.2
branch 0 8 0.0
condition n/a
subroutine 6 8 75.0
pod 0 1 0.0
total 24 49 48.9


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