Permalink
Browse files

implement retrieve_token and retrieve_apikey methods

  • Loading branch information...
sekimura committed Feb 4, 2011
1 parent 07d329a commit e1638615f3194e9d252b5e6cf81c7088b379ce2c
Showing with 52 additions and 3 deletions.
  1. +24 −0 example/retrieve
  2. +28 −3 lib/WebService/Prowl.pm
View
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use WebService::Prowl;
+use Getopt::Long;
+
+GetOptions(
+ 'providerkey|k=s' => \my $providerkey,
+);
+
+die "Usage: $0 -k providerkey" unless $providerkey;
+
+my $ws = WebService::Prowl->new(providerkey => $providerkey);
+my $token_res = $ws->retrieve_token();
+my $retrieve_url = $token_res->{retrieve}{url};
+my $token = $token_res->{retrieve}{token};
+print "open $retrieve_url in your browser to grant access\n then press enter\n";
+while(<STDIN>){
+ my $apikey_res = $ws->retrieve_apikey(token => $token);
+ print "API Key: " . $apikey_res->{retrieve}{apikey} . "\n";
+ exit;
+}
+
View
@@ -25,7 +25,6 @@ sub new {
my $class = shift;
my %params = @_;
my $apikey = $params{'apikey'};
- croak("apikey is required") unless $apikey;
return bless {
apikey => $params{'apikey'},
ua => LWP::UserAgent->new( agent => __PACKAGE__ . '/' . $VERSION ),
@@ -41,11 +40,13 @@ sub error { $_[0]->{error} }
sub _build_url {
my ( $self, $method, %params ) = @_;
if ($method eq 'verify') {
+ croak("apikey is required") unless $self->{apikey};
my $url = $API_BASE_URL . 'verify?apikey=' . $self->{apikey};
$url .= '&providerkey=' . $self->{providerkey} if $self->{providerkey};
return $url;
}
elsif ($method eq 'add') {
+ croak("apikey is required") unless $self->{apikey};
my @params = qw/priority application event description url/;
my $req_params = +{ map { $_ => delete $params{$_} } @params };
@@ -74,6 +75,18 @@ sub _build_url {
my $q = join ('&', @out);
return $API_BASE_URL . 'add?' . $q;
}
+ elsif ($method eq 'retrieve_token') {
+ croak("providerkey is required") unless $self->{providerkey};
+ return $API_BASE_URL . 'retrieve/token?providerkey=' . $self->{providerkey};
+ }
+ elsif ($method eq 'retrieve_apikey') {
+ croak("providerkey is required") unless $self->{providerkey};
+ my $token = $params{'token'};
+ croak("token is required") unless $token;
+ my $url = $API_BASE_URL . 'retrieve/apikey?providerkey=' . $self->{providerkey};
+ $url .= '&token=' . $token;
+ return $url;
+ }
}
sub add {
@@ -88,8 +101,20 @@ sub verify {
$self->_send_request($url);
}
+sub retrieve_token {
+ my ( $self, %params, $cb ) = @_;
+ my $url = $self->_build_url('retrieve_token', %params);
+ $self->_send_request($url, $cb);
+}
+
+sub retrieve_apikey {
+ my ( $self, %params, $cb ) = @_;
+ my $url = $self->_build_url('retrieve_apikey', %params);
+ $self->_send_request($url, $cb);
+}
+
sub _send_request {
- my ( $self, $url ) = @_;
+ my ( $self, $url, $cb ) = @_;
my $res = $self->{ua}->get($url);
my $data = $self->_xmlin($res->content);
if ($res->is_error) {
@@ -99,7 +124,7 @@ sub _send_request {
: '';
return;
}
- return 1;
+ return $data;
}
sub _xmlin {

0 comments on commit e163861

Please sign in to comment.