| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 2 |  |  | 2 |  | 16392 | use strict; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 65 |  | 
| 2 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 115 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package Footprintless::Plugin::Atlassian::Confluence::RequestBuilder; | 
| 5 |  |  |  |  |  |  | $Footprintless::Plugin::Atlassian::Confluence::RequestBuilder::VERSION = '1.01'; | 
| 6 |  |  |  |  |  |  | # ABSTRACT: A request builder for the Atlassian Confluence REST API | 
| 7 |  |  |  |  |  |  | # PODNAME: Footprintless::Plugin::Atlassian::Confluence::RequestBuilder | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 2 |  |  | 2 |  | 504 | use HTTP::Request; | 
|  | 2 |  |  |  |  | 18095 |  | 
|  | 2 |  |  |  |  | 72 |  | 
| 10 | 2 |  |  | 2 |  | 586 | use JSON; | 
|  | 2 |  |  |  |  | 8783 |  | 
|  | 2 |  |  |  |  | 62 |  | 
| 11 | 2 |  |  | 2 |  | 734 | use Log::Any; | 
|  | 2 |  |  |  |  | 9059 |  | 
|  | 2 |  |  |  |  | 10 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | my $logger = Log::Any->get_logger(); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub new { | 
| 16 | 2 |  |  | 2 | 1 | 10739 | return bless( {}, shift )->_init(@_); | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub create_content { | 
| 20 | 0 |  |  | 0 | 1 | 0 | my ( $self, $content, %options ) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  |  |  | 0 | return HTTP::Request->new( | 
| 23 |  |  |  |  |  |  | 'POST', | 
| 24 |  |  |  |  |  |  | $self->_url( "/rest/api/content", %options ), | 
| 25 |  |  |  |  |  |  | [ 'Content-Type' => 'application/json' ], | 
| 26 |  |  |  |  |  |  | encode_json($content) | 
| 27 |  |  |  |  |  |  | ); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub delete_content { | 
| 31 | 0 |  |  | 0 | 1 | 0 | my ( $self, $id ) = @_; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 0 |  |  |  |  | 0 | return HTTP::Request->new( 'DELETE', $self->_url("/rest/api/content/$id") ); | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub get_content { | 
| 37 | 5 |  |  | 5 | 1 | 7544 | my ( $self, %options ) = @_; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 5 |  |  |  |  | 10 | my $id = delete( $options{id} ); | 
| 40 | 5 | 100 |  |  |  | 36 | return HTTP::Request->new( 'GET', | 
| 41 |  |  |  |  |  |  | $id | 
| 42 |  |  |  |  |  |  | ? $self->_url( "/rest/api/content/$id", %options ) | 
| 43 |  |  |  |  |  |  | : $self->_url( "/rest/api/content",     %options ) ); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub get_content_children { | 
| 47 | 0 |  |  | 0 | 1 | 0 | my ( $self, $id, %options ) = @_; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 |  |  |  |  | 0 | my $type = delete( $options{type} ); | 
| 50 | 0 | 0 |  |  |  | 0 | return HTTP::Request->new( 'GET', | 
| 51 |  |  |  |  |  |  | $type | 
| 52 |  |  |  |  |  |  | ? $self->_url( "/rest/api/content/$id/child/$type", %options ) | 
| 53 |  |  |  |  |  |  | : $self->_url( "/rest/api/content/$id/child",       %options ) ); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub _init { | 
| 57 | 2 |  |  | 2 |  | 4 | my ( $self, $base_url ) = @_; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 2 |  |  |  |  | 15 | $self->{base_url} = $base_url; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 2 |  |  |  |  | 10 | return $self; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub update_content { | 
| 65 | 0 |  |  | 0 | 1 | 0 | my ( $self, $id, $content, %options ) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 |  |  |  |  | 0 | return HTTP::Request->new( | 
| 68 |  |  |  |  |  |  | 'PUT', | 
| 69 |  |  |  |  |  |  | $self->_url( "/rest/api/content/$id", %options ), | 
| 70 |  |  |  |  |  |  | [ 'Content-Type' => 'application/json' ], | 
| 71 |  |  |  |  |  |  | encode_json($content) | 
| 72 |  |  |  |  |  |  | ); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub _url { | 
| 76 | 5 |  |  | 5 |  | 12 | my ( $self, $path, %query_params ) = @_; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 5 |  |  |  |  | 13 | my $url = "$self->{base_url}$path"; | 
| 79 | 5 | 100 |  |  |  | 18 | if (%query_params) { | 
| 80 | 3 |  |  |  |  | 13 | require URI::Escape; | 
| 81 | 3 |  |  |  |  | 5 | my @query_string = (); | 
| 82 | 3 |  |  |  |  | 12 | foreach my $key ( sort( keys(%query_params) ) ) { | 
| 83 | 6 | 100 |  |  |  | 63 | push( @query_string, ( @query_string ? '&' : '?' ) ); | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | push( | 
| 86 |  |  |  |  |  |  | @query_string, | 
| 87 |  |  |  |  |  |  | join( | 
| 88 |  |  |  |  |  |  | '&', | 
| 89 | 6 |  |  |  |  | 10 | map { URI::Escape::uri_escape($key) . '=' . URI::Escape::uri_escape($_) } ( | 
| 90 |  |  |  |  |  |  | ref( $query_params{$key} ) eq 'ARRAY' | 
| 91 | 0 |  |  |  |  | 0 | ? @{ $query_params{$key} } | 
| 92 | 6 | 50 |  |  |  | 14 | : ( $query_params{$key} ) | 
| 93 |  |  |  |  |  |  | ) | 
| 94 |  |  |  |  |  |  | ) | 
| 95 |  |  |  |  |  |  | ); | 
| 96 |  |  |  |  |  |  | } | 
| 97 | 3 | 50 |  |  |  | 72 | $url .= ( @query_string ? join( '', @query_string ) : '' ); | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 5 |  |  |  |  | 34 | return $url; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ |