From 8306d573a50cbac0d76bf01ce98e82beec36e42d Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Sun, 17 Mar 2019 00:42:02 -0600 Subject: [PATCH] respond with Future subclasses --- LICENSE | 6 ++-- dist.ini | 6 ++-- lib/HTTP/AnyUA/Backend/AnyEvent/HTTP.pm | 13 ++++++- lib/HTTP/AnyUA/Backend/Mojo/UserAgent.pm | 13 ++++++- t/50-future-subclass.t | 45 ++++++++++++++++++++++++ t/lib/AnyEvent/Future.pm | 8 +++++ t/lib/Future/Mojo.pm | 8 +++++ 7 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 t/50-future-subclass.t create mode 100644 t/lib/AnyEvent/Future.pm create mode 100644 t/lib/Future/Mojo.pm diff --git a/LICENSE b/LICENSE index a2abd4e..2eb2acb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -This software is copyright (c) 2017 by Charles McGarvey. +This software is copyright (c) 2019 by Charles McGarvey. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. @@ -12,7 +12,7 @@ b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- -This software is Copyright (c) 2017 by Charles McGarvey. +This software is Copyright (c) 2019 by Charles McGarvey. This is free software, licensed under: @@ -272,7 +272,7 @@ That's all there is to it! --- The Artistic License 1.0 --- -This software is Copyright (c) 2017 by Charles McGarvey. +This software is Copyright (c) 2019 by Charles McGarvey. This is free software, licensed under: diff --git a/dist.ini b/dist.ini index d554bf6..d8d6a4e 100644 --- a/dist.ini +++ b/dist.ini @@ -2,14 +2,14 @@ name = HTTP-AnyUA author = Charles McGarvey copyright_holder = Charles McGarvey -copyright_year = 2017 +copyright_year = 2019 license = Perl_5 [@Author::CCM] -AutoPrereqs.skip[0] = ^(AnyEvent::.+|HTTP::Request|Net::Curl::Easy)$ +AutoPrereqs.skip[0] = ^(AnyEvent::.+|Future::Mojo|HTTP::Request|Net::Curl::Easy)$ AutoPrereqs.skip[1] = ^(JSON|AnyEvent|Plack::.+|Mojo::.+)$ -[Prereqs / RuntimeSuggests] +[Prereqs / RuntimeRecommends] HTTP::Tiny = 0 [Prereqs / TestSuggests] diff --git a/lib/HTTP/AnyUA/Backend/AnyEvent/HTTP.pm b/lib/HTTP/AnyUA/Backend/AnyEvent/HTTP.pm index 14f3b28..334a664 100644 --- a/lib/HTTP/AnyUA/Backend/AnyEvent/HTTP.pm +++ b/lib/HTTP/AnyUA/Backend/AnyEvent/HTTP.pm @@ -6,6 +6,9 @@ package HTTP::AnyUA::Backend::AnyEvent::HTTP; This module adds support for the HTTP client L to be used with the unified programming interface provided by L. +If installed, requests will return L rather than L. This allows the use of +the C<< ->get >> method to await a result. + =head1 SEE ALSO =for :list @@ -24,6 +27,14 @@ use Future; use HTTP::AnyUA::Util; +my $future_class; +BEGIN { + $future_class = 'Future'; + eval 'use AnyEvent::Future'; ## no critic + $future_class = 'AnyEvent::Future' if !$@; +} + + =method options $backend->options(\%options); @@ -41,7 +52,7 @@ sub request { my ($method, $url, $args) = @_; my %opts = $self->_munge_request($method, $url, $args); - my $future = Future->new; + my $future = $future_class->new; require AnyEvent::HTTP; AnyEvent::HTTP::http_request($method => $url, %opts, sub { diff --git a/lib/HTTP/AnyUA/Backend/Mojo/UserAgent.pm b/lib/HTTP/AnyUA/Backend/Mojo/UserAgent.pm index 436eddf..aeb2b1f 100644 --- a/lib/HTTP/AnyUA/Backend/Mojo/UserAgent.pm +++ b/lib/HTTP/AnyUA/Backend/Mojo/UserAgent.pm @@ -6,6 +6,9 @@ package HTTP::AnyUA::Backend::Mojo::UserAgent; This module adds support for the HTTP client L to be used with the unified programming interface provided by L. +If installed, requests will return L rather than L. This allows the use of the +C<< ->get >> method to await a result. + =head1 CAVEATS =for :list @@ -29,13 +32,21 @@ use Future; use Scalar::Util; +my $future_class; +BEGIN { + $future_class = 'Future'; + eval 'use Future::Mojo'; ## no critic + $future_class = 'Future::Mojo' if !$@; +} + + sub response_is_future { 1 } sub request { my $self = shift; my ($method, $url, $args) = @_; - my $future = Future->new; + my $future = $future_class->new; my $tx = $self->_munge_request(@_); diff --git a/t/50-future-subclass.t b/t/50-future-subclass.t new file mode 100644 index 0000000..619d713 --- /dev/null +++ b/t/50-future-subclass.t @@ -0,0 +1,45 @@ +#!perl + +use warnings; +use strict; + +use lib 't/lib'; + +use HTTP::AnyUA; +use Test::More tests => 2; +use Util qw(test_user_agent); + +HTTP::AnyUA->register_backend(Mock => '+MockBackend'); + +subtest 'test Future::Mojo as a Future response' => sub { + test_user_agent 'Mojo::UserAgent' => sub { + plan tests => 1; + + my $ua = shift; + my $any_ua = HTTP::AnyUA->new($ua, response_is_future => 1); + + my $url = 'http://acme.tld/'; + + my $future = $any_ua->get($url); + isa_ok($future, 'Future::Mojo'); + + return $future; + }; +}; + +subtest 'test AnyEvent::Future as a Future response' => sub { + test_user_agent 'AnyEvent::HTTP' => sub { + plan tests => 1; + + my $ua = shift; + my $any_ua = HTTP::AnyUA->new($ua, response_is_future => 1); + + my $url = 'http://acme.tld/'; + + my $future = $any_ua->get($url); + isa_ok($future, 'AnyEvent::Future'); + + return $future; + }; +}; + diff --git a/t/lib/AnyEvent/Future.pm b/t/lib/AnyEvent/Future.pm new file mode 100644 index 0000000..32750e2 --- /dev/null +++ b/t/lib/AnyEvent/Future.pm @@ -0,0 +1,8 @@ +package AnyEvent::Future; + +use warnings; +use strict; + +use parent 'Future'; + +1; diff --git a/t/lib/Future/Mojo.pm b/t/lib/Future/Mojo.pm new file mode 100644 index 0000000..44b8745 --- /dev/null +++ b/t/lib/Future/Mojo.pm @@ -0,0 +1,8 @@ +package Future::Mojo; + +use warnings; +use strict; + +use parent 'Future'; + +1; -- 2.43.0