Skip Menu |

This queue is for tickets about the WebService-HIBP CPAN distribution.

Report information
The Basics
Id: 128876
Status: resolved
Priority: 0/
Queue: WebService-HIBP

People
Owner: Nobody in particular
Requestors: PCRONIN [...] cpan.org
Cc: oshihuna [...] gmail.com
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: 0.10



CC: oshihuna [...] gmail.com
Greetings! I have found WebService::HIBP to be quite helpful in saving me a couple hundred lines of code. I'd like to be able to put a request timeout on requests to the endpoint. I think the easiest way to do that would be to expand the public interface of the constructor to allow users to pass in their own LWP::UserAgent, defaulting to the new one the module currently creates if the user does not pass one in. That way, I could create my own $ua, configure it how I want, and then provide it to the module. Would you be amenable to such a change? If so, I could prepare a patch or PR.
On Tue Mar 19 01:45:01 2019, PCRONIN wrote: Show quoted text
> Greetings! I have found WebService::HIBP to be quite helpful in saving > me a couple hundred lines of code. I'd like to be able to put a > request timeout on requests to the endpoint. I think the easiest way > to do that would be to expand the public interface of the constructor > to allow users to pass in their own LWP::UserAgent, defaulting to the > new one the module currently creates if the user does not pass one in. > That way, I could create my own $ua, configure it how I want, and then > provide it to the module. Would you be amenable to such a change? If > so, I could prepare a patch or PR.
Had a spare couple of minutes. Does the attached patch fit your requirements?
Subject: hibp_user_agent_parameter.patch
diff -Naur old/lib/WebService/HIBP.pm new/lib/WebService/HIBP.pm --- old/lib/WebService/HIBP.pm 2019-02-17 20:58:15.000000000 +1100 +++ new/lib/WebService/HIBP.pm 2019-03-19 19:33:02.090062356 +1100 @@ -9,18 +9,23 @@ use WebService::HIBP::Breach(); use WebService::HIBP::Paste(); -our $VERSION = '0.08'; +our $VERSION = '0.09'; sub _LENGTH_OF_PASSWORD_PREFIX { return 5; } sub new { - my ($class) = @_; + my ( $class, %params ) = @_; my $self = {}; bless $self, $class; $self->{url} = 'https://haveibeenpwned.com/api/v2/'; $self->{password_url} = 'https://api.pwnedpasswords.com/range/'; - $self->{ua} = LWP::UserAgent->new( agent => 'WebService-HIBP' ); - $self->{ua}->env_proxy(); + if ( $params{user_agent} ) { + $self->{ua} = $params{user_agent}; + } + else { + $self->{ua} = LWP::UserAgent->new( agent => 'WebService-HIBP' ); + $self->{ua}->env_proxy(); + } return $self; } @@ -204,7 +209,13 @@ =head2 new -a new C<WebService::HIBP> object, ready to check how bad the pwnage is. +a new C<WebService::HIBP> object, ready to check how bad the pwnage is. It accepts an optional hash as a parameter. Allowed keys are below; + +=over 4 + +=item * user_agent - A pre-configured instance of L<LWP::UserAgent|LWP::UserAgent> that will be used instead of the automatically created one. This allows full control of the user agent properties if desired + +=back =head2 password diff -Naur old/t/01-hibp.t new/t/01-hibp.t --- old/t/01-hibp.t 2019-02-17 20:51:17.000000000 +1100 +++ new/t/01-hibp.t 2019-03-19 19:31:17.527794579 +1100 @@ -384,5 +384,6 @@ ok($@, "account threw an error when supplied a bad proxy:$@"); } +$hibp = WebService::HIBP->new(user_agent => LWP::UserAgent->new()); done_testing();
On Tue Mar 19 04:35:00 2019, DDICK wrote: Show quoted text
> On Tue Mar 19 01:45:01 2019, PCRONIN wrote:
> > Greetings! I have found WebService::HIBP to be quite helpful in > > saving > > me a couple hundred lines of code. I'd like to be able to put a > > request timeout on requests to the endpoint. I think the easiest way > > to do that would be to expand the public interface of the constructor > > to allow users to pass in their own LWP::UserAgent, defaulting to the > > new one the module currently creates if the user does not pass one > > in. > > That way, I could create my own $ua, configure it how I want, and > > then > > provide it to the module. Would you be amenable to such a change? If > > so, I could prepare a patch or PR.
> > Had a spare couple of minutes. Does the attached patch fit your > requirements?
That looks great and works well too! A collages of mine (OALDERS) suggested that it may be a good idea to also validate that the value provided as `user_agent` is actually an LWP::UserAgent instance, but that's up to you. Much appreciation for your fast response!
On Wed Mar 20 02:08:39 2019, PCRONIN wrote: Show quoted text
> On Tue Mar 19 04:35:00 2019, DDICK wrote:
> > On Tue Mar 19 01:45:01 2019, PCRONIN wrote:
> That looks great and works well too! A collages of mine (OALDERS) > suggested that it may be a good idea to also validate that the value > provided as `user_agent` is actually an LWP::UserAgent instance, but > that's up to you. Much appreciation for your fast response!
Released as 0.10 due to an introduced mistake in the test suite. I'm not validating the user_agent field as the only LWP::UserAgent method i'm relying on is "get" (and i assume that it returns an HTTP::Response), so i want to have the interface relaxed enough that it can accept other user agents if desired.