[Jifty-commit] r4554 - in Net-Jifty: .

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Nov 28 16:42:07 EST 2007


Author: sterling
Date: Wed Nov 28 16:42:07 2007
New Revision: 4554

Modified:
   Net-Jifty/   (props changed)
   Net-Jifty/lib/Net/Jifty.pm

Log:
 r14258 at riddle:  andrew | 2007-11-28 15:38:06 -0600
 Fixing support for DELETE and PUT requets since LWP::UserAgent doesn't provide nice helpers for them.


Modified: Net-Jifty/lib/Net/Jifty.pm
==============================================================================
--- Net-Jifty/lib/Net/Jifty.pm	(original)
+++ Net-Jifty/lib/Net/Jifty.pm	Wed Nov 28 16:42:07 2007
@@ -8,6 +8,7 @@
 use DateTime;
 use Email::Address;
 use Fcntl qw(:mode);
+use HTTP::Request;
 
 =head1 NAME
 
@@ -102,7 +103,7 @@
         my $args = shift;
 
         my $ua = LWP::UserAgent->new;
-
+        
         $ua->cookie_jar({});
 
         # Load the user's proxy settings from %ENV
@@ -222,6 +223,31 @@
     }
 }
 
+=head2 form_url_encoded_args ARGS
+
+This will take a hash containing arguments and convert those arguments into URL encoded form. I.e., (x => 1, y => 2, z => 3) becomes:
+
+  x=1&y=2&z=3
+
+These are then ready to be appened to the URL on a GET or placed into the content of a PUT.
+
+=cut
+
+sub form_url_encoded_args {
+    my $self = shift;
+    my %args = @_;
+
+    my $uri;
+    while (my ($key, $value) = each %args) {
+        $uri .= '&' . join '=', map { $self->escape($_) } $key, $value;
+    }
+
+    # it's easier than keeping a flag of "did we already append?"
+    $uri =~ s/^&//;
+
+    return $uri;
+}
+
 =head2 method METHOD, URL[, ARGS]
 
 This will perform a GET, POST, PUT, DELETE, etc using the internal
@@ -255,24 +281,33 @@
     if ($method eq 'get' || $method eq 'head') {
         my $uri = $self->site . '/=/' . $url . '.yml';
 
-        if (keys %args) {
-            $uri .= '?';
-            while (my ($key, $value) = each %args) {
-                $uri .= '&' . join '=', map { $self->escape($_) } $key, $value;
-            }
-            # it's easier than keeping a flag of "did we already append?"
-            $uri =~ s/\?&/?/;
-        }
+        $uri .= '?' . $self->form_url_encoded_args(%args)
+            if keys %args;
 
         $res = $self->ua->$method($uri);
     }
-    else {
+    elsif ($method eq 'post') {
         $res = $self->ua->$method(
             $self->site . '/=/' . $url . '.yml',
             \%args
         );
     }
 
+    # LWP::UserAgent provides direct methods only for get, head, and post
+    else {
+        my $req = HTTP::Request->new(
+            uc($method) => $self->site . '/=/' . $url . '.yml'
+        );
+
+        if (keys %args) {
+            my $content = $self->form_url_encoded_args(%args);
+            $req->header('Content-type' => 'application/x-www-form-urlencoded');
+            $req->content($content);
+        }
+
+        $res = $self->ua->request($req);
+    }
+
     if ($res->is_success) {
         return YAML::Load( Encode::decode_utf8($res->content) );
     } else {


More information about the Jifty-commit mailing list