| 1 | package Test::Smoke::Poster::HTTP_Tiny; | ||||||||||
| 2 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 5 | our $VERSION = '0.002'; | |||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | use base 'Test::Smoke::Poster::Base'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | ▶ | use URI::Escape qw(uri_escape); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Test::Smoke::Poster::HTTP_Tiny - Poster subclass using HTTP::Tiny. | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | This is a subclass of L<Test::Smoke::Poster::Base>. | ||||||||||
| 18 | |||||||||||
| 19 | =head2 Test::Smoke::Poster::HTTP_Tiny->new(%arguments) | ||||||||||
| 20 | |||||||||||
| 21 | =head3 Extra Arguments | ||||||||||
| 22 | |||||||||||
| 23 | None. | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | |||||||||||
| 27 | 8 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 28 | 8 | my $class = shift; | |||||||||
| 29 | 8 | my $self = $class->SUPER::new(@_); | |||||||||
| 30 | |||||||||||
| 31 | 8 | require HTTP::Tiny; | |||||||||
| 32 | 8 | ▶ | $self->{_ua} = HTTP::Tiny->new( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | agent => $self->agent_string(), | ||||||||||
| 34 | ( $self->ua_timeout ? (timeout => $self->ua_timeout) : () ), | ||||||||||
| 35 | keep_alive => 0, | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 8 | return $self; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | =head2 $poster->_post_data() | ||||||||||
| 42 | |||||||||||
| 43 | Post the json to CoreSmokeDB using HTTP::Tiny. | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | sub _post_data { | ||||||||
|
Subroutine
_post_data: called CC=4 SCAR=16.8
| |||||||||||
| 48 | 2 | my $self = shift; | |||||||||
| 49 | |||||||||||
| 50 | 2 | $self->log_info("Posting to %s via %s.", $self->smokedb_url, $self->poster); | |||||||||
| 51 | 2 | $self->log_debug("Report data: %s", my $json = $self->get_json); | |||||||||
| 52 | |||||||||||
| 53 | 2 | my $form_data = sprintf("json=%s", uri_escape($json)); | |||||||||
| 54 | 2 | my $response = $self->ua->request( | |||||||||
| 55 | POST => $self->smokedb_url, | ||||||||||
| 56 | { | ||||||||||
| 57 | headers => { | ||||||||||
| 58 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||||||||||
| 59 | 'Content-Length' => length($form_data), | ||||||||||
| 60 | }, | ||||||||||
| 61 | content => $form_data, | ||||||||||
| 62 | }, | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | 2 | ▶ | if (!$response->{success}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | $self->log_warn( | ||||||||||
| 67 | "POST failed: %s %s%s", | ||||||||||
| 68 | $response->{status}, | ||||||||||
| 69 | $response->{reason}, | ||||||||||
| 70 | 0 | ▶ | ($response->{content} ? " ($response->{content})" : ""), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | ); | ||||||||||
| 72 | die sprintf( | ||||||||||
| 73 | "POST to '%s' failed: %s %s%s\n", | ||||||||||
| 74 | $self->smokedb_url, | ||||||||||
| 75 | $response->{status}, $response->{reason}, | ||||||||||
| 76 | 0 | ▶ | ($response->{content} ? " ($response->{content})" : ""), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | ); | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 2 | $self->log_debug("[CoreSmokeDB] %s", $response->{content}); | |||||||||
| 81 | |||||||||||
| 82 | 2 | return $response->{content}; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | =head2 $poster->_post_data_api() | ||||||||||
| 86 | |||||||||||
| 87 | Post the json to CoreSmokeDB using HTTP::Tiny, using the API-function. | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||
| 90 | |||||||||||
| 91 | 2 | ▶ | sub _post_data_api { | ||||||||
|
Subroutine
_post_data_api: called CC=5 SCAR=17.0
| |||||||||||
| 92 | 2 | my $self = shift; | |||||||||
| 93 | |||||||||||
| 94 | 2 | $self->log_info("Posting to %s via %s.", $self->smokedb_url, $self->poster); | |||||||||
| 95 | 2 | $self->log_debug("Report data: %s", my $json = $self->get_json); | |||||||||
| 96 | |||||||||||
| 97 | 2 | my $post_data = sprintf(qq/{"report_data": %s}/, $json); | |||||||||
| 98 | 2 | my $response = $self->ua->request( | |||||||||
| 99 | POST => $self->smokedb_url, | ||||||||||
| 100 | { | ||||||||||
| 101 | headers => { | ||||||||||
| 102 | 'Content-Type' => 'application/json', | ||||||||||
| 103 | 'Content-Length' => length($post_data), | ||||||||||
| 104 | }, | ||||||||||
| 105 | content => $post_data, | ||||||||||
| 106 | }, | ||||||||||
| 107 | ); | ||||||||||
| 108 | |||||||||||
| 109 | 2 | ▶ | if (!$response->{success}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | $self->log_warn( | ||||||||||
| 111 | "POST failed: %s %s%s", | ||||||||||
| 112 | $response->{status}, | ||||||||||
| 113 | $response->{reason}, | ||||||||||
| 114 | 1 | ▶ | ($response->{content} ? " ($response->{content})" : ""), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | ); | ||||||||||
| 116 | 1 | ▶ | if (not $self->queue_this_report()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | die sprintf( | ||||||||||
| 118 | "POST to '%s' failed: %s %s%s\n", | ||||||||||
| 119 | $self->smokedb_url, | ||||||||||
| 120 | $response->{status}, $response->{reason}, | ||||||||||
| 121 | 0 | ▶ | ($response->{content} ? " ($response->{content})" : ""), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | ); | ||||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 2 | $self->log_debug("[CoreSmokeDB] %s", $response->{content}); | |||||||||
| 127 | |||||||||||
| 128 | 2 | return $response->{content}; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 5 | 1; | |||||||||
| 132 | |||||||||||
| 133 | =head1 COPYRIGHT | ||||||||||
| 134 | |||||||||||
| 135 | (c) 2002-2013, Abe Timmerman <abeltje@cpan.org> All rights reserved. | ||||||||||
| 136 | |||||||||||
| 137 | With contributions from Jarkko Hietaniemi, Merijn Brand, Campo | ||||||||||
| 138 | Weijerman, Alan Burlison, Allen Smith, Alain Barbet, Dominic Dunlop, | ||||||||||
| 139 | Rich Rauenzahn, David Cantrell. | ||||||||||
| 140 | |||||||||||
| 141 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 142 | it under the same terms as Perl itself. | ||||||||||
| 143 | |||||||||||
| 144 | See: | ||||||||||
| 145 | |||||||||||
| 146 | =over 4 | ||||||||||
| 147 | |||||||||||
| 148 | =item * L<http://www.perl.com/perl/misc/Artistic.html> | ||||||||||
| 149 | |||||||||||
| 150 | =item * L<http://www.gnu.org/copyleft/gpl.html> | ||||||||||
| 151 | |||||||||||
| 152 | =back | ||||||||||
| 153 | |||||||||||
| 154 | This program is distributed in the hope that it will be useful, | ||||||||||
| 155 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 156 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||
| 157 | |||||||||||
| 158 | =cut | ||||||||||