3 # http://wiki.apache.org/couchdb/Getting_started_with_Perl
10 use Data::Dump qw/dump/;
13 my ($class, $host, $port, $options) = @_;
15 my $ua = LWP::UserAgent->new;
19 $host ||= 'localhost';
26 base_uri => "http://$host:$port/",
30 sub ua { shift->{ua} }
31 sub base { shift->{base_uri} }
34 my ($self, $method, $uri, $content) = @_;
36 my $full_uri = $self->base . $uri;
39 if (defined $content) {
40 #Content-Type: application/json
42 $req = HTTP::Request->new( $method, $full_uri, undef, $content );
43 $req->header('Content-Type' => 'application/json');
45 $req = HTTP::Request->new( $method, $full_uri );
48 my $response = $self->ua->request($req);
50 if ($response->is_success) {
51 return $response->content;
53 die($response->status_line . ":" . $response->content);
60 my ($self, $url) = @_;
62 $self->request(DELETE => $url);
66 my ($self, $url) = @_;
68 from_json $self->request(GET => $url);
72 my ($self, $url, $json) = @_;
73 warn "put $url ",dump($json);
75 $rev->{$url} ||= eval { $self->get( $url )->{_rev} };
77 $json->{_rev} = $rev->{$url} if $rev->{$url};
79 $json = to_json $json if $json;
81 $self->request(PUT => $url, $json);
85 my ($self, $url, $json) = @_;
87 $self->request(POST => $url, $json);