Skip to content
Browse files

Move code into Net::Azure::BlobService

  • Loading branch information...
1 parent bd651c4 commit 420e5e06d631f90025abe4eb040816775f53cf5a @acme committed Jan 5, 2012
Showing with 85 additions and 62 deletions.
  1. +69 −0 lib/Net/Azure/BlobService.pm
  2. +16 −62 play.pl
View
69 lib/Net/Azure/BlobService.pm
@@ -0,0 +1,69 @@
+package Net::Azure::BlobService;
+use HTTP::Date;
+use Digest::MD5 qw(md5_base64);
+use Digest::SHA qw(hmac_sha256_base64);
+use MIME::Base64;
+use Moose;
+
+has 'account' => ( is => 'ro', isa => 'Str', required => 1 );
+has 'primary_access_key' => ( is => 'ro', isa => 'Str', required => 1 );
+has 'user_agent' => (
+ is => 'ro',
+ isa => 'LWP::UserAgent',
+ default => sub {
+ my $ua = LWP::UserAgent->new;
+ $ua->env_proxy;
+ return $ua;
+ }
+);
+
+sub sign_http_request {
+ my ( $self, $http_request ) = @_;
+
+ $http_request->header( ':x-ms-version', '2011-08-18' );
+ $http_request->header( 'Date', time2str() );
+ $http_request->content_length( length $http_request->content );
+
+ my $canonicalized_headers = join "",
+ map { lc( substr( $_, 1 ) ) . ':' . $http_request->header($_) . "\n" }
+ sort grep {/^:x-ms/i} $http_request->header_field_names;
+
+ my $canonicalized_resource
+ = '/' . $self->account . $http_request->uri->path . join "", map {
+ "\n"
+ . lc($_) . ':'
+ . join( ',', sort $http_request->uri->query_param($_) )
+ } sort $http_request->uri->query_param;
+
+ my $string_to_sign
+ = $http_request->method . "\n"
+ . ( $http_request->header('Content-Encoding') // '' ) . "\n"
+ . ( $http_request->header('Content-Language') // '' ) . "\n"
+ . ( $http_request->header('Content-Length') // '' ) . "\n"
+ . ( $http_request->header('Content-MD5') // '' ) . "\n"
+ . ( $http_request->header('Content-Type') // '' ) . "\n"
+ . ( $http_request->header('Date') // '' ) . "\n"
+ . ( $http_request->header('If-Modified-Since') // '' ) . "\n"
+ . ( $http_request->header('If-Match') // '' ) . "\n"
+ . ( $http_request->header('If-None-Match') // '' ) . "\n"
+ . ( $http_request->header('If-Unmodified-Since') // '' ) . "\n"
+ . ( $http_request->header('Range') // '' ) . "\n"
+ . $canonicalized_headers
+ . $canonicalized_resource;
+
+ my $signature = hmac_sha256_base64( $string_to_sign,
+ decode_base64( $self->primary_access_key ) );
+ $signature .= '=';
+
+ $http_request->header( 'Authorization',
+ "SharedKey " . $self->account . ":" . $signature );
+ return $http_request;
+}
+
+sub make_http_request {
+ my ( $self, $http_request ) = @_;
+ $self->sign_http_request($http_request);
+ return $self->user_agent->request($http_request);
+}
+
+__PACKAGE__->meta->make_immutable;
View
78 play.pl
@@ -1,14 +1,12 @@
#!/home/acme/Public/perl-5.14.2/bin/perl
use strict;
use warnings;
+use lib 'lib';
use 5.14.0;
-use Digest::MD5 qw(md5_base64);
-use Digest::SHA qw(hmac_sha256_base64);
-use HTTP::Date;
use HTTP::Request;
use HTTP::Request::Common qw(GET HEAD PUT DELETE);
use LWP::UserAgent;
-use MIME::Base64;
+use Net::Azure::BlobService;
use URI::URL;
use URI::QueryParam;
use XML::LibXML;
@@ -18,6 +16,11 @@
my $primary_access_key
= 'XXX';
+my $blobservice = Net::Azure::BlobService->new(
+ account => $account,
+ primary_access_key => $primary_access_key
+);
+
# Get Blob Service Properties
my $uri = URI->new("https://$account.blob.core.windows.net/");
$uri->query_form( [ restype => 'service', comp => 'properties' ] );
@@ -146,66 +149,17 @@
# $uri->query_form( [ comp => 'blocklist' ] );
# my $request = GET $uri;
-# And now the library code
-
-$request->header( ':x-ms-version', '2011-08-18' );
-$request->header( 'Date', time2str() );
-$request->content_length( length $request->content );
-
-my $canonicalized_headers = join "",
- map { lc( substr( $_, 1 ) ) . ':' . $request->header($_) . "\n" }
- sort grep {/^:x-ms/i} $request->header_field_names;
-
-# say "headers: $canonicalized_headers";
-
-my $canonicalized_resource = '/' . $account . $request->uri->path . join "",
- map {
- "\n"
- . lc($_) . ':'
- . join( ',', sort $request->uri->query_param($_) )
- } sort $request->uri->query_param;
-
-# say "resource: [$canonicalized_resource]";
-
-my $string_to_sign
- = $request->method . "\n"
- . ( $request->header('Content-Encoding') // '' ) . "\n"
- . ( $request->header('Content-Language') // '' ) . "\n"
- . ( $request->header('Content-Length') // '' ) . "\n"
- . ( $request->header('Content-MD5') // '' ) . "\n"
- . ( $request->header('Content-Type') // '' ) . "\n"
- . ( $request->header('Date') // '' ) . "\n"
- . ( $request->header('If-Modified-Since') // '' ) . "\n"
- . ( $request->header('If-Match') // '' ) . "\n"
- . ( $request->header('If-None-Match') // '' ) . "\n"
- . ( $request->header('If-Unmodified-Since') // '' ) . "\n"
- . ( $request->header('Range') // '' ) . "\n"
- . $canonicalized_headers
- . $canonicalized_resource;
-
-say $string_to_sign;
-
-my $signature = hmac_sha256_base64( $string_to_sign,
- decode_base64($primary_access_key) );
-$signature .= '=';
-
-#say $signature;
-
-$request->header( 'Authorization', "SharedKey $account:$signature" );
-
-say $request->as_string;
-
-my $ua = LWP::UserAgent->new;
-$ua->env_proxy;
-
-my $response = $ua->request($request);
+my $response = $blobservice->make_http_request($request);
if ( $response->is_success ) {
- say $response->as_string;
- my $xml = $response->decoded_content;
- say $xml;
- my $dom = XML::LibXML->load_xml( string => $xml );
- say $dom->toString(1);
+ if ( $response->content_type eq 'application/xml' ) {
+ my $xml = $response->decoded_content;
+ say $xml;
+ my $dom = XML::LibXML->load_xml( string => $xml );
+ say $dom->toString(1);
+ } else {
+ say $response->as_string;
+ }
} else {
die $response->status_line;
}

0 comments on commit 420e5e0

Please sign in to comment.
Something went wrong with that request. Please try again.