use JSON;
use Digest::MD5 qw(md5_hex);
use Data::Dump qw(dump);
+use Encode;
+use Carp;
+use Clone qw(clone);
sub new {
my ($class, $host, $port, $options) = @_;
sub base { shift->{base_uri} }
sub request {
- my ($self, $method, $uri, $content) = @_;
+ my ($self, $method, $uri, $content, $content_type) = @_;
+ $content_type ||= 'application/json';
my $full_uri = $self->base . $uri;
my $req;
- if (defined $content) {
- #Content-Type: application/json
+ warn "# request $method $full_uri ", $content ? length($content) : '-' , " $content_type\n";
- $req = HTTP::Request->new( $method, $full_uri, undef, encode_json $content );
- $req->header('Content-Type' => 'application/json');
+ if (defined $content) {
+ $req = HTTP::Request->new( $method, $full_uri, undef );
+ $req->header( 'Content-Type' => $content_type );
+ my $bytes = ref($content) ? encode_json($content) : $content;
+ $req->content( $bytes ); # convert to bytes
+ warn "### ",dump($bytes);
} else {
$req = HTTP::Request->new( $method, $full_uri );
}
sub get {
my ($self, $url) = @_;
- warn "## GET $url";
$self->request(GET => $url);
}
sub put {
my ($self, $url, $json) = @_;
- warn "## PUT $url";
$self->request(PUT => $url, $json);
}
$self->request(POST => $url, $json);
}
-sub update {
+sub modify {
my ($self, $url, $json) = @_;
- warn "# update_doc $url";
+ carp "# modify $url ", ref $json;
- my $json_md5 = md5_hex encode_json $json;
- $json->{x_sync}->{json_md5} = $json_md5;
+ my $ret;
if ( my $old = eval { $self->get( $url ) } ) {
- warn "# old ", $old->{_rev}; #dump($old);
+ warn "# old ", $old->{_rev}, dump($old);
- if ( $json_md5 ne $old->{x_sync}->{json_md5} ) {
+ if ( ref $json eq 'CODE' ) {
+ $json = $json->( clone $old );
+ warn "# json CODE ",dump($json);
+ } else {
+ warn "# overwrite old";
$json->{_rev} = $old->{_rev};
- warn :"# update $url";
- $self->put( $url => $json );
+ }
+
+ my $json_md5 = md5_hex encode_json $json;
+ $json->{x_sync}->{json_md5} = $json_md5;
+
+ warn "# json_md5 $json_md5 == $old->{x_sync}->{json_md5}\n";
+ if ( $json_md5 ne $old->{x_sync}->{json_md5} ) {
+ warn "# modify $url";
+ $ret = $self->put( $url => $json );
} else {
warn "# unchanged $url";
+ $ret->{rev} = $old->{_rev};
+ $ret->{id} = $old->{_id};
}
} else {
+ $json = $json->({}) if ref $json eq 'CODE';
warn "# insert $url ", dump($json);
- $self->put( $url => $json );
+ $ret = $self->put( $url => $json );
}
+ return $ret;
}
1;
use JSON;
use Data::Dump qw(dump);
use RT::Client::REST;
+use URI::Escape;
use CouchDB;
use Digest::MD5 qw(md5_hex);
$items->{$id} = $item;
- $db->update( "zotero_$UserID/$id" => $item );
+ $db->modify( "zotero_$UserID/$id" => $item );
}
foreach my $nr ( keys %$ticket_items ) {
- my $ticket = $rt->show(type => 'ticket', id => $nr);
+ my $ticket = eval { $rt->show(type => 'ticket', id => $nr) };
warn "# ticket $nr ",dump($ticket);
- $db->update( "rt/$nr" => $ticket );
+ next unless $ticket;
+
+ $ticket->{zotero_items} = $ticket_items->{$nr};
+
+ my $modified = $db->modify( "rt/$nr" => sub {
+ my $doc = shift;
+ $doc->{$_} = $ticket->{$_} foreach keys %$ticket;
+ return $doc;
+ });
+
+ warn "# modified ",dump($modified);
+
+ # copy attachments to CouchDB (they never change, so do it just once
+ if ( my @attachment_ids = $rt->get_attachment_ids( id => $nr ) ) {
+
+ warn "# get_attachment_ids = ",dump( @attachment_ids );
+ my $doc = $db->get("rt/$nr");
+ my @attachments;
+
+ foreach my $attachment_id ( @attachment_ids ) {
+ my $attachment = $rt->get_attachment( parent_id => $nr, id => $attachment_id );
+ if ( $attachment->{Filename} && $attachment->{ContentEncoding} eq 'base64' ) {
+ #$attachment->{Filename} ||= $attachment_id;
+ my $content = delete $attachment->{Content};
+ if ( ! exists $doc->{_attachments}->{ $attachment->{Filename} } ) {
+ utf8::encode($content) || warn "utf8::encode error!";
+ warn "# extracted ",length( $content ), " bytes";
+ warn "## attachment ",dump( $attachment );
+ my $url = sprintf 'rt/%d/%s?rev=%s', $nr, uri_escape($attachment->{Filename}), $modified->{rev};
+# $modified = $db->request( PUT => $url, $content, $attachment->{ContentType} );
+ }
+ }
+ push @attachments, $attachment;
+ }
+
+
+ $db->modify( "rt/$nr" => sub {
+ my $doc = shift;
+ $doc->{attachments} = [ @attachments ];
+ warn "## attachments on $nr = ", $#attachments + 1;
+ return $doc;
+ }) if @attachments;
+
+ }
if ( $ticket->{Queue} !~ m/ILL/i ) {
warn "SKIP $ticket not in ILL queue!";
# $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) );
- last; # FIXME just first
-
}
}