| 1 | # ABSTRACT: ponapi CLI query runner | ||
| 2 | package PONAPI::CLI::RunQuery; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use JSON::MaybeXS; | ||
| 8 | use HTTP::Tiny; | ||
| 9 | |||
| 10 | sub run { | ||
| 11 | my ( $port, $query_string, $only_json ) = @_; | ||
| 12 | |||
| 13 | my $url = $query_string || random_url( $port ); | ||
| 14 | |||
| 15 | my $res = HTTP::Tiny->new->get( $url, { | ||
| 16 | headers => { | ||
| 17 | 'Accept' => 'application/vnd.api+json', | ||
| 18 | }, | ||
| 19 | }); | ||
| 20 | |||
| 21 | die "Failed to connect to a local server (try 'ponapi demo -s' to start one)\n" | ||
| 22 | unless $res and ref($res) eq 'HASH' and $res->{status} < 500; | ||
| 23 | |||
| 24 | unless ( $only_json ) { | ||
| 25 | print "\nGET $url\n\n"; | ||
| 26 | print $res->{protocol} . " " . $res->{status} . " " . $res->{reason} . "\n"; | ||
| 27 | |||
| 28 | print "Content-Type: " . $res->{headers}{'content-type'} . "\n\n"; | ||
| 29 | } | ||
| 30 | |||
| 31 | my $json = JSON::MaybeXS->new; | ||
| 32 | print $json->pretty(1)->encode( $json->decode($res->{content}) ); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub random_url { | ||
| 36 | my $port = shift; | ||
| 37 | |||
| 38 | my %rels = ( | ||
| 39 | articles => { | ||
| 40 | id => [ 1, 2, 3 ], | ||
| 41 | include => [qw< comments authors >], | ||
| 42 | fields => [qw< body title created updated status >], | ||
| 43 | }, | ||
| 44 | comments => { | ||
| 45 | id => [ 5, 12 ], | ||
| 46 | include => [qw< articles >], | ||
| 47 | }, | ||
| 48 | people => { | ||
| 49 | id => [ 42, 88, 91 ], | ||
| 50 | include => [qw< articles >], | ||
| 51 | fields => [qw< name age gender >], | ||
| 52 | }, | ||
| 53 | ); | ||
| 54 | |||
| 55 | my $type = ( keys %rels )[ _rand(\%rels) ]; | ||
| 56 | |||
| 57 | my $id = _rand(2) ? '/' . $rels{$type}{id}->[ _rand( $rels{$type}{id} ) ] : ""; | ||
| 58 | |||
| 59 | my @type_inc = @{ $rels{$type}{include} }; | ||
| 60 | my @include = _rand(2) | ||
| 61 | ? ( scalar @type_inc > 1 ? map { _rand(2) ? $_ : () } @type_inc : @type_inc ) | ||
| 62 | : (); | ||
| 63 | |||
| 64 | my $include = @include ? "include=" . ( join ',' => @include ) : ""; | ||
| 65 | |||
| 66 | my @fields = _rand(2) ? map { _rand(2) ? $_ : () } @{ $rels{$type}{fields} } : (); | ||
| 67 | my $fields = @fields ? "fields[$type]=" . ( join ',' => @fields, @include ) : ""; | ||
| 68 | |||
| 69 | my $sort = ( !$id and _rand(2) ) | ||
| 70 | ? "sort=" . ( _rand(2) ? '-' : '' ) . ( 'id', @fields )[ _rand(@fields+1) ] | ||
| 71 | : ""; | ||
| 72 | |||
| 73 | my $is_query = ( $include || $fields || $sort ? "?" : "" ); | ||
| 74 | |||
| 75 | my $url = "http://localhost:$port/$type" | ||
| 76 | . $id . $is_query . join( '&' => grep { $_ } $include, $fields, $sort ); | ||
| 77 | |||
| 78 | return $url; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _rand { | ||
| 82 | my $s = | ||
| 83 | @_ > 1 ? scalar( @_ ) : | ||
| 84 | ref($_[0]) eq 'HASH' ? scalar( keys %{$_[0]} ) : | ||
| 85 | ref($_[0]) eq 'ARRAY' ? scalar( @{$_[0]} ) : | ||
| 86 | $_[0]; | ||
| 87 | |||
| 88 | return int(rand($s)); | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | =encoding UTF-8 | ||
| 98 | |||
| 99 | =head1 NAME | ||
| 100 | |||
| 101 | PONAPI::CLI::RunQuery - ponapi CLI query runner | ||
| 102 | |||
| 103 | =head1 VERSION | ||
| 104 | |||
| 105 | version 0.003002 | ||
| 106 | |||
| 107 | =head1 AUTHORS | ||
| 108 | |||
| 109 | =over 4 | ||
| 110 | |||
| 111 | =item * | ||
| 112 | |||
| 113 | Mickey Nasriachi <mickey@cpan.org> | ||
| 114 | |||
| 115 | =item * | ||
| 116 | |||
| 117 | Stevan Little <stevan@cpan.org> | ||
| 118 | |||
| 119 | =item * | ||
| 120 | |||
| 121 | Brian Fraser <hugmeir@cpan.org> | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head1 COPYRIGHT AND LICENSE | ||
| 126 | |||
| 127 | This software is copyright (c) 2017 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||
| 128 | |||
| 129 | This is free software; you can redistribute it and/or modify it under | ||
| 130 | the same terms as the Perl 5 programming language system itself. | ||
| 131 | |||
| 132 | =cut |