Skip to content

Commit

Permalink
experimental fallback to HTTP::Tiny
Browse files Browse the repository at this point in the history
  • Loading branch information
monken committed Jul 19, 2013
1 parent d310c82 commit 09a2d82
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cpanfile
@@ -1,4 +1,4 @@
requires 'AnyEvent::Curl::Multi';
recommends 'AnyEvent::Curl::Multi';
requires 'App::Prove';
requires 'CPAN::Meta::Requirements';
requires 'Captcha::reCAPTCHA', '0.94';
Expand Down Expand Up @@ -33,6 +33,7 @@ requires 'HTML::Tree';
requires 'HTTP::Message::PSGI';
requires 'HTTP::Request';
requires 'HTTP::Request::Common';
requires 'HTTP::Tiny';
requires 'Hash::AsObject';
requires 'Hash::Merge';
requires 'JSON';
Expand Down
40 changes: 38 additions & 2 deletions lib/MetaCPAN/Web/Model/API.pm
Expand Up @@ -8,15 +8,24 @@ has [qw(api api_secure)] => ( is => 'ro' );
use Encode ();
use JSON;
use HTTP::Request ();
use AnyEvent::Curl::Multi;
use AnyEvent;
use HTTP::Response;
use Try::Tiny 0.09;
use MooseX::ClassAttribute;
use namespace::autoclean;

class_has client => ( is => 'ro', lazy_build => 1 );

sub _build_client {
return AnyEvent::Curl::Multi->new( max_concurrency => 5 );
my $client;
eval {
require AnyEvent::Curl::Multi;
$client = AnyEvent::Curl::Multi->new( max_concurrency => 5 );
} or do {
warn "Install AnyEvent::Curl::Multi for better performance, falling back to HTTP::Tiny";
$client = MetaCPAN::Web::Model::API::HTTP::Win32->new;
};
return $client;
}

{
Expand Down Expand Up @@ -117,4 +126,31 @@ sub raw_api_response {
return +{ raw => $data };
}

package MetaCPAN::Web::Model::API::HTTP::Win32::Handle;
use Moose;

has cv => ( is => "ro" );

__PACKAGE__->meta->make_immutable;

package MetaCPAN::Web::Model::API::HTTP::Win32;
use Moose;
use AE;
use HTTP::Tiny;

sub request {
my ($self, $req) = @_;
my $cv = AE::cv;
my $res = HTTP::Tiny->new->request($req->method, $req->uri, { headers => $req->headers, content => $req->content });
warn $res->{content};
$cv->send(
HTTP::Response->new(
$res->{status},
$res->{reason},
HTTP::Headers->new(%{$res->{headers}}),
$res->{content}
));
return MetaCPAN::Web::Model::API::HTTP::Win32::Handle->new( cv => $cv );
}

1;

0 comments on commit 09a2d82

Please sign in to comment.