[libflickr-api-perl] 28/40: patch and tests for handling of unicode strings - requires unicode=1 flag

Lucas Kanashiro kanashiro-guest at moszumanska.debian.org
Sat Jul 25 21:12:19 UTC 2015


This is an automated email from the git hooks/post-receive script.

kanashiro-guest pushed a commit to tag 1.08
in repository libflickr-api-perl.

commit 00a0f6c5eef49147750457acca03929ea922edae
Author: Cal Henderson <cal at iamcal.com>
Date:   Thu May 23 10:18:36 2013 -0700

    patch and tests for handling of unicode strings - requires unicode=1 flag
---
 Changes                   |  3 +++
 lib/Flickr/API.pm         | 24 ++++++++++++++++++++----
 lib/Flickr/API/Request.pm |  9 +++++++++
 test.pl                   |  7 ++++++-
 4 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/Changes b/Changes
index efad38d..1f6fc0b 100644
--- a/Changes
+++ b/Changes
@@ -24,3 +24,6 @@ Changes to Flickr::API
 
 1.06, 2013-05-11
  * Added `lwpobj` argument, to allow using subclasses of LWP::UserAgent
+
+1.07, 2013-05-23
+ * Added flag to enable handling of native Unicode strings
diff --git a/lib/Flickr/API.pm b/lib/Flickr/API.pm
index 8175307..310e030 100644
--- a/lib/Flickr/API.pm
+++ b/lib/Flickr/API.pm
@@ -8,10 +8,11 @@ use Flickr::API::Request;
 use Flickr::API::Response;
 use Digest::MD5 qw(md5_hex);
 use Scalar::Util qw(blessed);
+use Encode qw(encode_utf8);
 
 our @ISA = qw(LWP::UserAgent);
 
-our $VERSION = '1.06';
+our $VERSION = '1.07';
 
 sub new {
 	my $class = shift;
@@ -34,6 +35,7 @@ sub new {
 	$self->{api_secret}	= $options->{secret};
 	$self->{rest_uri}	= $options->{rest_uri} || 'http://api.flickr.com/services/rest/';
 	$self->{auth_uri}	= $options->{auth_uri} || 'http://api.flickr.com/services/auth/';
+	$self->{unicode}	= $options->{unicode} || 0;
 
 	eval {
 		require Compress::Zlib;
@@ -59,6 +61,7 @@ sub sign_args {
 		$sig .= $key . $value;
 	}
 
+	return md5_hex(encode_utf8($sig)) if $self->{unicode};
 	return md5_hex($sig);
 }
 
@@ -90,7 +93,12 @@ sub request_auth_url {
 sub execute_method {
 	my ($self, $method, $args) = @_;
 
-	my $request = new Flickr::API::Request({'method' => $method, 'args' => $args, rest_uri => $self->{rest_uri}});
+	my $request = new Flickr::API::Request({
+		'method'	=> $method,
+		'args'		=> $args,
+		'rest_uri'	=> $self->{rest_uri},
+		'unicode'	=> $self->{unicode},
+	});
 
 	$self->execute_request($request);
 }
@@ -168,8 +176,11 @@ Flickr::API - Perl interface to the Flickr API
 
   use Flickr::API;
 
-  my $api = new Flickr::API({'key'    => 'your_api_key',
-                             'secret' => 'your_app_secret'});
+  my $api = new Flickr::API({
+		'key'    => 'your_api_key',
+		'secret' => 'your_app_secret',
+		'unicode'=> 0,
+	});
 
   my $response = $api->execute_method('flickr.test.echo', {
 		'foo' => 'bar',
@@ -225,6 +236,11 @@ Override the URIs used for contacting the API.
 Base the C<Flickr::API> on this object, instead of creating a new instance of C<LWP::UserAgent>.
 This is useful for using the features of e.g. C<LWP::UserAgent::Cached>.
 
+=item C<unicode>
+
+This flag controls whether Flicrk::API expects you to pass UTF-8 bytes (unicode=0, the default) or
+actual unicode strings (unicode=1) in the request.
+
 =back
 
 =item C<execute_method($method, $args)>
diff --git a/lib/Flickr/API/Request.pm b/lib/Flickr/API/Request.pm
index e8f3236..2c8bcf5 100644
--- a/lib/Flickr/API/Request.pm
+++ b/lib/Flickr/API/Request.pm
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use HTTP::Request;
 use URI;
+use Encode qw(encode_utf8);
 
 our @ISA = qw(HTTP::Request);
 our $VERSION = '0.03';
@@ -15,6 +16,7 @@ sub new {
 	$self->{api_method}	= $options->{method};
 	$self->{api_args}	= $options->{args};
 	$self->{rest_uri}	= $options->{rest_uri} || 'http://api.flickr.com/services/rest/';
+	$self->{unicode}	= $options->{unicode} || 0;
 
 	bless $self, $class;
 
@@ -28,7 +30,14 @@ sub encode_args {
 	my ($self) = @_;
 
 	my $url = URI->new('http:');
+
+	if ($self->{unicode}){
+		for my $k(keys %{$self->{api_args}}){
+			$self->{api_args}->{$k} = encode_utf8($self->{api_args}->{$k});
+		}
+	}
 	$url->query_form(%{$self->{api_args}});
+
 	my $content = $url->query;
 
 	$self->header('Content-Type' => 'application/x-www-form-urlencoded');
diff --git a/test.pl b/test.pl
index e8feb9e..5697b1d 100644
--- a/test.pl
+++ b/test.pl
@@ -1,5 +1,5 @@
 use Test::More;
-BEGIN { plan tests => 17 };
+BEGIN { plan tests => 19 };
 
 BEGIN { use_ok( 'Flickr::API' ); }
 
@@ -51,6 +51,11 @@ if ($rsp->{_rc} eq '200'){
 ok('466cd24ced0b23df66809a4d2dad75f8' eq $api->sign_args({'foo' => 'bar'}), "Signing test 1");
 ok('f320caea573c1b74897a289f6919628c' eq $api->sign_args({'foo' => undef}), "Signing test 2");
 
+$api->{unicode} = 0;
+is('b8bac3b2a4f919d04821e43adf59288c', $api->sign_args({'foo' => "\xE5\x8C\x95\xE4\xB8\x83"}), "Signing test 3 (unicode=0)");
+
+$api->{unicode} = 1;
+is('b8bac3b2a4f919d04821e43adf59288c', $api->sign_args({'foo' => "\x{5315}\x{4e03}"}), "Signing test 4 (unicode=1)");
 
 ##################################################
 #

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libflickr-api-perl.git



More information about the Pkg-perl-cvs-commits mailing list