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   612 use strict;
  1         1  
  1         26  
9 1     1   2 use warnings;
  1         1  
  1         27  
10 1     1   3 use base qw( App::MatrixTool::Command::client );
  1         1  
  1         74  
11              
12             our $VERSION = '0.06';
13              
14 1     1   3 use constant DESCRIPTION => "Perform a direct JSON request";
  1         1  
  1         41  
15 1     1   3 use constant ARGUMENTS => ( "path", "data?" );
  1         1  
  1         44  
16 1         191 use constant OPTIONS => (
17             'm|method=s' => "HTTP method",
18 1     1   3 );
  1         1  
19              
20             require JSON;
21             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 commandline argument only requires the path and optional query
36             parameters. If an access token is known for the user (see
37             C) then it is automatically added to the query
38             parameters as well.
39              
40             An optional second argument, I, 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 request. If no data is supplied, then a C 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 or C will be
56             performed, depending on whether the I argument was supplied.
57              
58             =back
59              
60             =cut
61              
62             sub run
63             {
64 0     0 0   my $self = shift;
65 0           my ( $opts, $pathquery, $data ) = @_;
66              
67 0           my $method = "GET";
68 0 0         $method = "PUT" if defined $data;
69              
70 0 0         $method = $opts->{method} if defined $opts->{method};
71              
72 0           my %opts;
73              
74 0 0         $opts{content} = $JSON->decode( $data ) if defined $data;
75              
76 0           my $uri = URI->new( $pathquery );
77 0 0         if( $uri->query_form ) {
78 0           $opts{params} = { $uri->query_form };
79             }
80              
81             $self->do_json( $method, $uri->path, %opts )->then( sub {
82 0     0     my ( $body, $response ) = @_;
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 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
113              
114             =cut
115              
116             0x55AA;