Subject: | Add a browser param to new to allow a pre-configured user agent |
Attached is a patch against the SVN repos version that adds support for
a "browser" param to new. This allows the user to supply a
pre-configured user agent.
A test is included.
I have project that requires an LWP::UserAgent::POE object. I'm
currently working around this by setting $client->{browser} after
creation. Being able to supply the browser on creation would be much
cleaner.
-Marc
Subject: | oauth.diff |
Index: t/browser.t
===================================================================
--- t/browser.t (revision 0)
+++ t/browser.t (revision 0)
@@ -0,0 +1,37 @@
+#!perl -T
+
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+{
+ package My::Browser;
+ use base 'LWP::UserAgent';
+
+ sub new {
+ my $class = shift;
+ my $new = $class->SUPER::new(@_);
+
+ return bless $new, $class;
+ }
+}
+
+BEGIN { use_ok 'Net::OAuth::Simple' }
+
+my $browser = My::Browser->new(timeout => 20);
+
+my $client = Net::OAuth::Simple->new(
+ tokens => {
+ consumer_key => 'test',
+ consumer_secret => 'test',
+ },
+ urls => {
+ authorization_url => 'http://localhost/auth',
+ request_token_url => 'http://localhost/req',
+ access_token_url => 'http://localhost/acc',
+ },
+ browser => $browser,
+);
+
+isa_ok $client->{browser}, 'My::Browser';
+is $client->{browser}->timeout, 20, 'browser is preconfigured'
Index: lib/Net/OAuth/Simple.pm
===================================================================
--- lib/Net/OAuth/Simple.pm (revision 5504)
+++ lib/Net/OAuth/Simple.pm (working copy)
@@ -8,6 +8,7 @@
use LWP;
use CGI;
use Carp;
+use Scalar::Util qw(blessed);
require Net::OAuth::Request;
require Net::OAuth::RequestTokenRequest;
require Net::OAuth::AccessTokenRequest;
@@ -147,6 +148,11 @@
=back
+You can supply an optional C<browser> key. The value must be an LWP::UserAgent
+object (or an LWP::UserAgent derived class object, such as an
+LWP::UserAgent::POE). Use this parameter if you want to pre-configure the
+user agent.
+
=cut
sub new {
@@ -154,12 +160,12 @@
my %params = @_;
my $client = bless \%params, $class;
+ # Set up LibWWWPerl for HTTP requests
+ $client->{browser} ||= LWP::UserAgent->new;
+
# Verify arguments
$client->_check;
- # Set up LibWWWPerl for HTTP requests
- $client->{browser} = LWP::UserAgent->new;
-
# Client Object
return $client;
}
@@ -173,9 +179,12 @@
foreach my $param ( @required_constructor_params ) {
unless ( defined $self->{tokens}->{$param} ) {
- die "Missing required parameter '$param'";
+ croak "Missing required parameter '$param'";
}
}
+
+ croak "browser must be a LWP::UserAgent"
+ unless blessed $self->{browser} && $self->{browser}->isa('LWP::UserAgent');
}
=head2 authorized