| 1 | package CPAN::Testers::API::Controller::Upload; | ||||||||||||||
| 2 | 1 | our $VERSION = '0.025'; | |||||||||||||
| 3 | # ABSTRACT: API for uploads to CPAN | ||||||||||||||
| 4 | |||||||||||||||
| 5 | #pod =head1 DESCRIPTION | ||||||||||||||
| 6 | #pod | ||||||||||||||
| 7 | #pod =head1 SEE ALSO | ||||||||||||||
| 8 | #pod | ||||||||||||||
| 9 | #pod =over | ||||||||||||||
| 10 | #pod | ||||||||||||||
| 11 | #pod =item L<CPAN::Testers::Schema::Result::Upload> | ||||||||||||||
| 12 | #pod | ||||||||||||||
| 13 | #pod =item L<Mojolicious::Controller> | ||||||||||||||
| 14 | #pod | ||||||||||||||
| 15 | #pod =back | ||||||||||||||
| 16 | #pod | ||||||||||||||
| 17 | #pod =cut | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 1 | ▶ | use Mojo::Base 'Mojolicious::Controller'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 1 | ▶ | use CPAN::Testers::API::Base; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | #pod =method get | ||||||||||||||
| 24 | #pod | ||||||||||||||
| 25 | #pod ### Requests: | ||||||||||||||
| 26 | #pod GET /v1/upload | ||||||||||||||
| 27 | #pod GET /v1/upload?since=2016-01-01T12:34:00 | ||||||||||||||
| 28 | #pod GET /v1/upload/dist/My-Dist | ||||||||||||||
| 29 | #pod GET /v1/upload/dist/My-Dist?since=2016-01-01T12:34:00 | ||||||||||||||
| 30 | #pod GET /v1/upload/author/PREACTION | ||||||||||||||
| 31 | #pod GET /v1/upload/author/PREACTION?since=2016-01-01T12:34:00 | ||||||||||||||
| 32 | #pod | ||||||||||||||
| 33 | #pod ### Response: | ||||||||||||||
| 34 | #pod 200 OK | ||||||||||||||
| 35 | #pod Content-Type: application/json | ||||||||||||||
| 36 | #pod | ||||||||||||||
| 37 | #pod [ | ||||||||||||||
| 38 | #pod { | ||||||||||||||
| 39 | #pod "dist": "My-Dist", | ||||||||||||||
| 40 | #pod "version": "1.000", | ||||||||||||||
| 41 | #pod "author": "PREACTION", | ||||||||||||||
| 42 | #pod "filename": "My-Dist-1.000.tar.gz", | ||||||||||||||
| 43 | #pod "released": "2016-08-12T04:02:34Z", | ||||||||||||||
| 44 | #pod } | ||||||||||||||
| 45 | #pod ] | ||||||||||||||
| 46 | #pod | ||||||||||||||
| 47 | #pod Get CPAN upload data. Results can be limited by distribution (with the | ||||||||||||||
| 48 | #pod C<dist> key in the stash), by author (with the C<author> key in the | ||||||||||||||
| 49 | #pod stash), and by date (with the C<since> query parameter). | ||||||||||||||
| 50 | #pod | ||||||||||||||
| 51 | #pod =cut | ||||||||||||||
| 52 | |||||||||||||||
| 53 | ▶ | sub get( $c ) { | |||||||||||||
|
Subroutine
get: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||
| 54 | 0 | ▶ | $c->openapi->valid_input or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | my $rs = $c->schema->resultset( 'Upload' ); | |||||||||||||
| 57 | 0 | $rs = $rs->search( | |||||||||||||
| 58 | { }, | ||||||||||||||
| 59 | { | ||||||||||||||
| 60 | order_by => 'released', | ||||||||||||||
| 61 | columns => [qw( dist version author filename released )], | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | ); | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | ▶ | if ( my $since = $c->param( 'since' ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | $rs = $rs->since( $since ); | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | my @results; | |||||||||||||
| 70 | 0 | ▶ | if ( my $dist = $c->validation->param( 'dist' ) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 71 | 0 | $rs = $rs->by_dist( $dist ); | |||||||||||||
| 72 | 0 | @results = $rs->all; | |||||||||||||
| 73 | 0 | ▶ | if ( !@results ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 0 | return $c->render_error( 404, sprintf 'Distribution "%s" not found', $dist ); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | } | ||||||||||||||
| 77 | elsif ( my $author = $c->validation->param( 'author' ) ) { | ||||||||||||||
| 78 | 0 | @results = $rs->by_author( $author )->all; | |||||||||||||
| 79 | 0 | ▶ | if ( !@results ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 0 | return $c->render_error( 404, sprintf 'Author "%s" not found', $author ); | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | } | ||||||||||||||
| 83 | else { | ||||||||||||||
| 84 | 0 | @results = $rs->all; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 0 | my @formatted = map { +{ | |||||||||||||
| 88 | 0 | dist => $_->dist, | |||||||||||||
| 89 | version => $_->version, | ||||||||||||||
| 90 | author => $_->author, | ||||||||||||||
| 91 | filename => $_->filename, | ||||||||||||||
| 92 | released => $_->released . "", | ||||||||||||||
| 93 | } } @results; | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | return $c->render( | |||||||||||||
| 96 | openapi => \@formatted, | ||||||||||||||
| 97 | ); | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | #pod =method feed | ||||||||||||||
| 101 | #pod | ||||||||||||||
| 102 | #pod Get a feed for uploads to CPAN. This feed returns the same information as | ||||||||||||||
| 103 | #pod the regular API, but as they come in. | ||||||||||||||
| 104 | #pod | ||||||||||||||
| 105 | #pod =cut | ||||||||||||||
| 106 | |||||||||||||||
| 107 | ▶ | sub feed( $c ) { | |||||||||||||
|
Subroutine
feed: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 108 | 0 | $c->inactivity_timeout( 60000 ); | |||||||||||||
| 109 | 0 | ▶ | my $path = $c->stash( 'dist' ) ? '/upload/dist/' . $c->stash( 'dist' ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 110 | : $c->stash( 'author' ) ? '/upload/author/' . $c->stash( 'author' ) | ||||||||||||||
| 111 | : '/upload/dist' # Default to all dists | ||||||||||||||
| 112 | ; | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | my $ua = Mojo::UserAgent->new( inactivity_timeout => 6000 ); | |||||||||||||
| 115 | $ua->websocket( | ||||||||||||||
| 116 | ▶ | $c->app->config->{broker} . '/sub' . $path, | |||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 117 | sub( $ua, $tx ) { | ||||||||||||||
| 118 | 0 | $c->stash( tx => $tx ); | |||||||||||||
| 119 | ▶ | $tx->on(finish => sub( $tx, $code ) { | |||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 120 | # Broker closed connection, so close connection with | ||||||||||||||
| 121 | # client, unless that's what we're already doing | ||||||||||||||
| 122 | 0 | ▶ | $c->finish if !$c->stash( 'finished' ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 0 | }); | |||||||||||||
| 124 | |||||||||||||||
| 125 | ▶ | $tx->on( message => sub( $tx, $msg ) { | |||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 126 | 0 | $c->send( $msg ); | |||||||||||||
| 127 | 0 | } ); | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | 0 | ); | |||||||||||||
| 130 | |||||||||||||||
| 131 | 0 | $c->stash( ua => $ua ); | |||||||||||||
| 132 | ▶ | $c->on( finish => sub( $c, $tx ) { | |||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 133 | # Client closed connection, so close connection with broker | ||||||||||||||
| 134 | 0 | ▶ | if ( my $tx = $c->stash( 'tx' ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | 0 | $c->stash( finished => 1 ); | |||||||||||||
| 136 | 0 | $tx->finish; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | 0 | } ); | |||||||||||||
| 139 | 0 | $c->rendered( 101 ); | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 1 | 1; | |||||||||||||
| 143 | |||||||||||||||
| 144 | __END__ | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =pod | ||||||||||||||
| 147 | |||||||||||||||
| 148 | =head1 NAME | ||||||||||||||
| 149 | |||||||||||||||
| 150 | CPAN::Testers::API::Controller::Upload - API for uploads to CPAN | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =head1 VERSION | ||||||||||||||
| 153 | |||||||||||||||
| 154 | version 0.025 | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =head1 DESCRIPTION | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =head1 METHODS | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =head2 get | ||||||||||||||
| 161 | |||||||||||||||
| 162 | ### Requests: | ||||||||||||||
| 163 | GET /v1/upload | ||||||||||||||
| 164 | GET /v1/upload?since=2016-01-01T12:34:00 | ||||||||||||||
| 165 | GET /v1/upload/dist/My-Dist | ||||||||||||||
| 166 | GET /v1/upload/dist/My-Dist?since=2016-01-01T12:34:00 | ||||||||||||||
| 167 | GET /v1/upload/author/PREACTION | ||||||||||||||
| 168 | GET /v1/upload/author/PREACTION?since=2016-01-01T12:34:00 | ||||||||||||||
| 169 | |||||||||||||||
| 170 | ### Response: | ||||||||||||||
| 171 | 200 OK | ||||||||||||||
| 172 | Content-Type: application/json | ||||||||||||||
| 173 | |||||||||||||||
| 174 | [ | ||||||||||||||
| 175 | { | ||||||||||||||
| 176 | "dist": "My-Dist", | ||||||||||||||
| 177 | "version": "1.000", | ||||||||||||||
| 178 | "author": "PREACTION", | ||||||||||||||
| 179 | "filename": "My-Dist-1.000.tar.gz", | ||||||||||||||
| 180 | "released": "2016-08-12T04:02:34Z", | ||||||||||||||
| 181 | } | ||||||||||||||
| 182 | ] | ||||||||||||||
| 183 | |||||||||||||||
| 184 | Get CPAN upload data. Results can be limited by distribution (with the | ||||||||||||||
| 185 | C<dist> key in the stash), by author (with the C<author> key in the | ||||||||||||||
| 186 | stash), and by date (with the C<since> query parameter). | ||||||||||||||
| 187 | |||||||||||||||
| 188 | =head2 feed | ||||||||||||||
| 189 | |||||||||||||||
| 190 | Get a feed for uploads to CPAN. This feed returns the same information as | ||||||||||||||
| 191 | the regular API, but as they come in. | ||||||||||||||
| 192 | |||||||||||||||
| 193 | =head1 SEE ALSO | ||||||||||||||
| 194 | |||||||||||||||
| 195 | =over | ||||||||||||||
| 196 | |||||||||||||||
| 197 | =item L<CPAN::Testers::Schema::Result::Upload> | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =item L<Mojolicious::Controller> | ||||||||||||||
| 200 | |||||||||||||||
| 201 | =back | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =head1 AUTHOR | ||||||||||||||
| 204 | |||||||||||||||
| 205 | Doug Bell <preaction@cpan.org> | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 208 | |||||||||||||||
| 209 | This software is copyright (c) 2018 by Doug Bell. | ||||||||||||||
| 210 | |||||||||||||||
| 211 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 212 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =cut | ||||||||||||||