AnyEvent-CouchDB
view release on metacpan or search on metacpan
lib/AnyEvent/CouchDB/Database.pm view on Meta::CPAN
$options->{error}->(@_) if ($options->{error});
$cv->croak(encode_json $headers);
};
my $cb = sub {
my ($body, $headers) = @_;
if ($headers->{Status} >= 200 and $headers->{Status} < 400) {
$success->(@_);
} else {
$error->($headers);
}
};
http_request(
GET => $self->uri
. uri_escape_utf8( $doc->{_id} ) . "/"
. uri_escape_utf8($attachment),
headers => $self->_build_headers($options),
$cb
);
$cv;
}
sub detach {
my ( $self, $doc, $attachment, $options ) = @_;
if ( $options->{success} ) {
my $orig = $options->{success};
$options->{success} = sub {
my ($resp) = @_;
$orig->($resp);
$doc->{_id} = $resp->{id};
$doc->{_rev} = $resp->{rev};
delete $doc->{_attachments}->{$attachment};
};
}
else {
$options->{success} = sub {
my ($resp) = @_;
$doc->{_id} = $resp->{id};
$doc->{_rev} = $resp->{rev};
delete $doc->{_attachments}->{$attachment};
};
}
my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );
http_request(
DELETE => $self->uri
. uri_escape_utf8( $doc->{_id} ) . "/"
. uri_escape_utf8($attachment)
. $query->( { rev => $doc->{_rev} } ),
headers => $self->_build_headers($options),
$cb
);
$cv;
}
sub bulk_docs {
my ( $self, $docs, $options ) = @_;
my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );
my %props = (); ## _bulk_docs properties go to the request body
foreach my $property (qw(all_or_nothing new_edits)) {
if (my $value = delete $options->{$property}) {
## convert the respective value to the JSON boolean type
$props{$property} = $value eq 'false' ? JSON::false() : JSON::true();
}
}
http_request(
POST => $self->uri . '_bulk_docs',
headers => $self->_build_headers($options),
body => $self->json( { %props, docs => $docs } ),
$cb
);
$cv;
}
sub query {
my ( $self, $map_fun, $reduce_fun, $language, $options ) = @_;
my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );
$language ||= ( ref($map_fun) eq 'CODE' ) ? 'text/perl' : 'javascript';
my $body = {
language => $language,
map => $code_to_string->($map_fun),
};
if ($reduce_fun) {
$body->{reduce} = $code_to_string->($reduce_fun);
}
http_request(
POST => $self->uri . '_temp_view' . $query->($options),
headers => $self->_build_headers($options),
body => $self->json($body),
$cb
);
$cv;
}
sub view {
my ( $self, $name, $options ) = @_;
my ( $cv, $cb ) = cvcb( $options, undef, $self->json_encoder );
my ( $dname, $vname ) = split( '/', $name );
my $uri = $self->uri . "_design/" . $dname . "/_view/" . $vname;
if ( $options->{keys} ) {
my $body = { keys => $options->{keys} };
my $opts = { %$options };
delete $opts->{keys};
http_request(
POST => $uri . $query->($opts),
headers => $self->_build_headers($options),
body => $self->json($body),
$cb
);
}
else {
my $headers = $self->_build_headers($options);
http_request(
GET => $uri . $query->($options),
headers => $headers,
$cb
);
}
$cv;
( run in 0.573 second using v1.01-cache-2.11-cpan-39bf76dae61 )