Context: Recently one of our Linux administrator requested us to provide an API to update atlassian wiki(confluence) in an automated way through some of their script(script will generate an output file - According to atlassian wiki storage format and pass that output file as confluence content). I did google to see any one has already created a REST API based, but I didn't find. so created this module and sharing with other people.
Modules dependency:
MIME::Base64, REST::Client and JSON
Steps:
Modules dependency:
MIME::Base64, REST::Client and JSON
Steps:
-
Copy the below perl module(REST.pm) in the perl library path(folder hierarchy should be <perl_lib>/Confluence/Client/REST.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package Confluence::Client::REST; use strict; use warnings; # ABSTRACT: Client for the Atlassian Confluence wiki, based on REST Api # Author: Raghunathan K Semburakkiannan # Create Date: 4/7/2015 use MIME::Base64; use REST::Client; use JSON; use fields qw(url user pwd); # To by pass SSL certificate error $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; sub new { my Confluence::Client::REST $self = shift; my ($url, $user, $pwd) = @_; unless ( ref $self ) { $self = fields::new($self); } $self->{url} = $url; $self->{user} = $user; $self->{pwd} = $pwd; return $self; } sub updatePage { my Confluence::Client::REST $self = shift; my ($page_id, $content_path) = @_; my $client = REST::Client->new(host => $self->{url}); # Get the content $client->GET("/rest/api/content/$page_id?expand=body.storage,version,ancestors&os_authType=basic&os_username=$self->{user}&os_password=$self->{pwd}"); if ($client->responseCode() ne '200') { print 'Unable to get the content'; print 'Response: ' . $client->responseContent() . "\n"; print 'Status: ' . $client->responseCode() . "\n"; return -1; } # read the output file to update the wiki local $/ = undef; open FILE, $content_path or die "Couldn't open file: $!"; binmode FILE; my $wiki_content_toUpdate = <FILE>; close FILE; # decode the response my $result_hash = decode_json($client->responseContent()); my $storage_value = $result_hash->{body}->{storage}->{value} ; $result_hash->{version}->{message} = ''; $result_hash->{version}->{number} = $result_hash->{version}->{number} + 1; $result_hash->{body}->{storage}->{value} = $wiki_content_toUpdate; $client->PUT("/rest/api/content/$page_id?expand=&os_authType=basic&os_username=$self->{user}&os_password=$self->{pwd}", encode_json($result_hash), {'Content-Type' => 'application/json'}); if ($client->responseCode() ne '200') { print 'Unable to update the content'; print 'Response: ' . $client->responseContent() . "\n"; print 'Status: ' . $client->responseCode() . "\n"; return -2; } else { print "\nUpdate - Successful !\n"; } } 1;
- Code to update the page:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#Three params: Confluence URL, userID, password my $client = Confluence::Client::REST->new('https://atlassian.wiki.com','myUserID','password'); #Two params: Confluence PageID, output file to write(output format can be found here: https://confluence.atlassian.com/display/DOC/Confluence+Storage+Format) $client->updatePage('2520145','/home/SembuR/some_output.out');