Quiq/Confluence/Client.pm

filter: Statementstmt 34.6% 34 / 98 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.8% 11 / 19 Podpod 100% 8 / 8 totaltotal 33.3% 53 / 159 CC 15 SCAR 28.3
CC
15
Cov
27%
send
56.0
createPage
20.0
updatePage
12.0
CRAP 16.9
« Quiq/Database/Cursor.pm ↑ Summary Quiq/ImagePool/Directory.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Confluence::Client - Confluence-Wiki Client
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen Client, der über die
16 L<Confluence REST API|https://docs.atlassian.com/confluence/REST/latest/> mit einem
17 Confluence-Server kommunizieren kann.
18  
19 Doku für Python: L<https://atlassian-python-api.readthedocs.io/>.Authentisierung gegen REST API der Cloud-Version s.
20 Abschnitt I<To authenticate to the Atlassian Cloud APIs Jira, Confluence,
21 ServiceDesk>. Als Passwort muss ein Token angegeben werden, das
22 im unter "Konto / Konto verwalten / Sicherheit / API Token"
23 angelegt und verwaltet werden kann.
24  
25 Die Implementierung der Klasse stellt die maßgeblichen Mechnismen
26 zur Kommunikation mit dem Server zur Verfügung, realisiert
27 z.Zt. jedoch nur einen kleinen Ausschnitt der Funktionalität der
28 Confluence REST API. Die Implementierung wird nach Bedarf
29 erweitert.
30  
31 =cut
32  
33 # -----------------------------------------------------------------------------
34  
35 package Quiq::Confluence::Client;
36 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 our $VERSION = '1.240';
44  
45 1 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Quiq::Confluence::Markup;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Quiq::Confluence::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # -----------------------------------------------------------------------------
53  
54 =head1 METHODS
55  
56 =head2 Konstruktor
57  
58 =head3 new() - Instantiiere Confluence-Client
59  
60 =head4 Synopsis
61  
62 $cli = $class->new(@keyVal);
63  
64 =head4 Arguments
65  
66 =over 4
67  
68 =item url => $url
69  
70 Basis-URL des Confluence Wiki, z.B. "https://<name>.atlassian.net".
71  
72 =item user => $user
73  
74 Name des Confluence-Benutzers, z.B. "admin".
75  
76 =item password => $password
77  
78 Passwort des Confluence-Benutzers.
79  
80 =item verbose => $bool (Default: 0)
81  
82 Gib Laufzeit-Informationen auf STDERR aus.
83  
84 =back
85  
86 =head4 Returns
87  
88 Client-Objekt (Typ Quiq::Confluence::Client)
89  
90 =head4 Description
91  
92 Instantiiere einen Client für Confluence mit den Eigenschaften
93 @keyval und liefere eine Referenz auf dieses Objekt zurück.
94  
95 =head4 Example
96  
97 Client für Atlassian Demo-Instanz:
98  
99 $cli = Quiq::Confluence::Client->new(
100 url => 'https://<name>.atlassian.net',
101 user => 'admin',
102 password => '<password>',
103 verbose => 1,
104 );
105  
106 =cut
107  
108 # -----------------------------------------------------------------------------
109  
110 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
111 0 my $class = shift;
112 # @_: @keyVal
113  
114 0 my $self = $class->SUPER::new(
115 url => undef,
116 user => undef,
117 password => undef,
118 verbose => 0,
119 ua => LWP::UserAgent->new,
120 );
121 0 $self->set(@_);
122  
123 0 return $self;
124 }
125  
126 # -----------------------------------------------------------------------------
127  
128 =head2 Confluence Operationen
129  
130 =head3 createPage() - Erzeuge Confluence Seite
131  
132 =head4 Synopsis
133  
134 $pag = $cli->createPage($parentId,$title,$markup,@opts);
135  
136 =head4 Arguments
137  
138 =over 4
139  
140 =item parentId => $pageId
141  
142 Die Page-Id der übergeordneten Seite.
143  
144 =item $title
145  
146 Der Titel der Seite.
147  
148 =item $markup
149  
150 Seiteninhalt in Confluence Wiki Markup.
151  
152 =back
153  
154 =head4 Options
155  
156 =over 4
157  
158 =item -warning => $bool (Default: 0)
159  
160 Setze eine Warnung an den Anfang der Seite, dass die Seite automatisch
161 generiert wurde.
162  
163 =back
164  
165 =head4 Returns
166  
167 nichts
168  
169 =head4 Description
170  
171 Erzeuge eine Confluence-Seite mit Titel $title und Inhalt $markup
172 (= Wiki Code) als Unterseite von der Seite mit der Seiten-Id
173 $parentId und liefere das Seiten-Objekt der der erzeugten Seite
174 zurück.
175  
176 Die erzeugte Seite ist (notwendigerweise) demselben Space wie die
177 übergeordnete Seite zugeordnet.
178  
179 =cut
180  
181 # -----------------------------------------------------------------------------
182  
183 0 ▶sub createPage {
Subroutine createPage: not called CC=4 SCAR=30.0
Pod documented
184 0 my ($self,$parentId,$title,$markup,@opts) = @_;
185  
186 # Optionen
187  
188 0 my $warning = 0;
189  
190 0 Quiq::Option->extract(\@_,
191 -warning => \$warning,
192 );
193  
194 # Führe Operation aus
195  
196 0 ▶ if ($warning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warning) 0 0 0
197 0 my $gen = Quiq::Confluence::Markup->new;
198 0 $markup = $gen->paragraph(
199 $gen->fmt('italic',q~
200 Achtung: Diese Seite wurde von einem Programm erzeugt.
201 Manuelle Änderungen gehen mit der nächsten Erzeugung
202 verloren!
203 ~),
204 ).$markup;
205 }
206  
207 0 my $pag = $self->getPage($parentId);
208  
209 0 ▶ my $res = $self->send(
Branch: 1 branch
BranchTrueFalseTotal
$parentId ? : 0 0 0
210 POST => "rest/api/content",
211 'application/json',
212 JSON::encode_json({
213 title => $title,
214 type => 'page',
215 space => {
216 key => $pag->space,
217 },
218 $parentId? (
219 ancestors => [{
220 id => $parentId,
221 }],
222 ): (),
223 body => {
224 storage => {
225 representation => 'wiki',
226 value => $markup,
227 },
228 },
229 })
230 );
231  
232 # Instantiiere Seiten-Objekt der erzeugten Seite und liefere
233 # dieses zurück
234  
235 0 $pag = Quiq::Confluence::Page->new($res->content);
236 0 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
237 0 warn sprintf "---RESULT---\n%s\n",$pag->asString;
238 }
239  
240 0 return $pag;
241 }
242  
243 # -----------------------------------------------------------------------------
244  
245 =head3 deletePage() - Lösche Confluence Seite
246  
247 =head4 Synopsis
248  
249 $pag = $cli->deletePage($pageId);
250  
251 =head4 Arguments
252  
253 =over 4
254  
255 =item $pageId
256  
257 Seiten-Id
258  
259 =back
260  
261 =head4 Returns
262  
263 Nichts
264  
265 =head4 Description
266  
267 Lösche die Confluence-Seite mit der Seiten-Id $pageId.
268  
269 =cut
270  
271 # -----------------------------------------------------------------------------
272  
273 0 ▶sub deletePage {
Subroutine deletePage: not called CC=2 SCAR=17.9
Pod documented
274 0 my ($self,$pageId) = @_;
275  
276 0 my $res = $self->send('DELETE',"rest/api/content/$pageId");
277 0 my $pag = Quiq::Confluence::Page->new($res->content);
278 0 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
279 0 warn sprintf "---RESULT---\n%s\n",$pag->asString;
280 }
281  
282 0 return;
283 }
284  
285 # -----------------------------------------------------------------------------
286  
287 =head3 getPage() - Liefere Confluence Seite
288  
289 =head4 Synopsis
290  
291 $pag = $cli->getPage($pageId);
292  
293 =head4 Arguments
294  
295 =over 4
296  
297 =item $pageId
298  
299 Seiten-Id
300  
301 =back
302  
303 =head4 Returns
304  
305 Seiten-Objekt (Typ Quiq::Confluence::Page)
306  
307 =head4 Description
308  
309 Rufe die Confluence-Seite mit der Seiten-Id $pageId ab und liefere
310 ein Seiten-Objekt vom Typ Quiq::Confluence::Page zurück.
311  
312 =cut
313  
314 # -----------------------------------------------------------------------------
315  
316 0 ▶sub getPage {
Subroutine getPage: not called CC=2 SCAR=17.9
Pod documented
317 0 my ($self,$pageId) = @_;
318  
319 # my $res = $self->send('GET',"rest/api/content/$pageId");
320 0 my $res = $self->send('GET',"wiki/rest/api/content/$pageId");
321 0 my $pag = Quiq::Confluence::Page->new($res->content);
322 0 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
323 0 warn sprintf "---RESULT---\n%s\n",$pag->asString;
324 }
325  
326 0 return $pag;
327 }
328  
329 # -----------------------------------------------------------------------------
330  
331 =head3 updatePage() - Aktualisiere Confluence Seite
332  
333 =head4 Synopsis
334  
335 $cli->updatePage($pageId,$markup,@opts);
336  
337 =head4 Arguments
338  
339 =over 4
340  
341 =item $pageId
342  
343 Seiten-Id
344  
345 =item $markup
346  
347 Seiteninhalt in Confluence Wiki Markup
348  
349 =back
350  
351 =head4 Options
352  
353 =over 4
354  
355 =item -warning => $bool (Default: 0)
356  
357 Setze eine Warnung an den Anfang der Seite, dass die Seite automatisch
358 erzeugt wurde.
359  
360 =item -title => $title
361  
362 Setze den Seitentitel.
363  
364 =back
365  
366 =head4 Returns
367  
368 nichts
369  
370 =head4 Description
371  
372 Ersetze den Inhalt der Confluence-Seite $pageId durch den neuen
373 Inhalt $markup. Für die Aktualisierung sind vier Angaben
374 erforderlich:
375  
376 =over 2
377  
378 =item *
379  
380 die PageId der Seite
381  
382 =item *
383  
384 der Inhalt der Seite
385  
386 =item *
387  
388 der Titel der Seite
389  
390 =item *
391  
392 die I<neue> Versionsnummer der Seite
393  
394 =back
395  
396 Um die neue Versionsnummer der Seite vergeben zu können, wird
397 intern zunächst der aktuelle Stand der Seite abgerufen, der
398 u.a. die bestehende Versionsnummer enthält. Die Versionsnummer ist
399 eine ganze Zahl, die mit jeder Aktualisierung um 1 erhöht werden
400 muss.
401  
402 Der Titel der Seite wird aus dem aktuellen Stand der Seite
403 übernommen, sofern er nicht mit der Option -title überschrieben
404 wird.
405  
406 =cut
407  
408 # -----------------------------------------------------------------------------
409  
410 0 ▶sub updatePage {
Subroutine updatePage: not called CC=3 SCAR=24.8
Pod documented
411 0 my ($self,$pageId,$markup,@opts) = @_;
412  
413 # Optionen
414  
415 0 my $warning = 0;
416 0 my $title = undef;
417  
418 0 Quiq::Option->extract(\@_,
419 -warning => \$warning,
420 -title => \$title,
421 );
422  
423 # Führe Operation aus
424  
425 0 ▶ if ($warning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warning) 0 0 0
426 0 my $gen = Quiq::Confluence::Markup->new;
427 0 $markup = $gen->paragraph(
428 $gen->fmt('italic',q~
429 Achtung: Diese Seite wurde von einem Programm erzeugt.
430 Manuelle Änderungen gehen mit der nächsten Erzeugung
431 verloren!
432 ~),
433 ).$markup;
434 }
435
436 0 my $pag = $self->getPage($pageId);
437  
438 0 ▶ $self->send(
Condition0%0/3
$title || $pag->title
l!l&&r!l&&!r
000
MC/DC0%0/2
$title || $pag->title
$title $pag->title
Truth table0%0/3
A || B
A= $titleB= $pag->title
ABresult
1X1
011
000
439 # PUT => "rest/api/content/$pageId",
440 PUT => "wiki/rest/api/content/$pageId",
441 'application/json',
442 JSON::encode_json({
443 type => 'page',
444 title => $title || $pag->title,
445 body => {
446 storage => {
447 representation => 'wiki',
448 value => $markup,
449 },
450 },
451 version => {
452 number => $pag->version+1,
453 },
454 })
455 );
456  
457 0 return;
458 }
459  
460 # -----------------------------------------------------------------------------
461  
462 =head3 createAttachment() - Füge Attachment zu Confluence-Seite hinzu
463  
464 =head4 Synopsis
465  
466 $pag = $cli->createAttachment($parentId,$file);
467  
468 =head4 Arguments
469  
470 =over 4
471  
472 =item parentId => $pageId
473  
474 Die Page-Id der übergeordneten Seite.
475  
476 =item $file
477  
478 Pfad zur Attchment-Datei.
479  
480 =back
481  
482 =head4 Returns
483  
484 nichts
485  
486 =head4 Description
487  
488 Füge Datei $file als Attachment zur Confluence-Seite mit der
489 Seiten-Id $pageId hinzu.
490  
491 =cut
492  
493 # -----------------------------------------------------------------------------
494  
495 0 ▶sub createAttachment {
Subroutine createAttachment: not called CC=1 SCAR=6.9
Pod documented
496 0 my ($self,$parentId,$path) = @_;
497  
498 0 $self->send(
499 POST => "rest/api/content/$parentId/child/attachment",
500 'form-data',[
501 file => [$path],
502 ],
503 );
504  
505 0 return;
506 }
507  
508 # -----------------------------------------------------------------------------
509  
510 =head2 Hilfsmethoden
511  
512 Die folgenden Methoden bilden die Grundlage für die Kommunikation
513 mit dem Confluence-Server. Sie werden normalerweise nicht direkt
514 gerufen.
515  
516 =head3 send() - Sende HTTP-Request an Confluence
517  
518 =head4 Synopsis
519  
520 $res = $cli->send($method,$path);
521 $res = $cli->send($method,$path,$contentType,$content);
522  
523 =head4 Arguments
524  
525 =over 4
526  
527 =item $method
528  
529 Die HTTP-Methode, z.B. 'PUT'.
530  
531 =item $path
532  
533 Der REST-Pfad, z.B. 'rest/api/content/32788'.
534  
535 =item $contentType
536  
537 Der Content-Type des HTTP-Body, z.B. 'application/json'.
538  
539 =item $content
540  
541 Der Inhalt des HTTP-Body, z.B. (auf die Toplevel-Attribute umbrochen)
542  
543 {"version":{"number":24},
544 "body":{"storage":{"representation":"wiki","value":"{cheese}"}},
545 "title":"Testseite",
546 "type":"page"}
547  
548 =back
549  
550 =head4 Returns
551  
552 HTTP-Antwort (Typ HTTP::Response)
553  
554 =head4 Description
555  
556 Sende einen HTTP-Request vom Typ $method mit dem REST-Pfad $path
557 und dem Body $content vom Typ $contentType an den Confluence-Server
558 und liefere die resultierende HTTP-Anwort zurück. Im Fehlerfall
559 wirft die Methode eine Exception.
560  
561 =cut
562  
563 # -----------------------------------------------------------------------------
564  
565 0 ▶sub send {
Subroutine send: not called CC=7 SCAR=40.3
Pod documented
566 0 my ($self,$method,$path,$contentType,$content) = @_;
567
568 0 my ($ua,$user,$password,$verbose) =
569 $self->get(qw/ua user password verbose/);
570  
571 0 my $req;
572 0 ▶ if ($method eq 'POST' && $contentType eq 'form-data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'POST' and $contentType eq 'form-data') { } 0 0 0
Condition0%0/3
$method eq "POST" and $contentType eq "form-data"
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "POST" and $contentType eq "form-data"
$method eq "POST" $contentType eq "form-data"
Truth table0%0/3
A and B
A= $method eq "POST"B= $contentType eq "form-data"
ABresult
0X0
100
111
573 # Attachments. Bei dieser POST-Funktion kann $content
574 # eine Array-Referenz mit mehreren Inhalten sein, die
575 # per multipart/form-data gepostet werden.
576  
577 0 $req = HTTP::Request::Common::POST(
578 $self->url($path),
579 'X-Atlassian-Token' => 'nocheck',
580 Content_Type => $contentType,
581 Content => $content,
582 );
583 }
584 else {
585 0 $req = HTTP::Request->new(
586 $method => $self->url($path),
587 );
588 0 ▶ if ($contentType) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contentType) 0 0 0
589 0 $req->header('Content-Type' => "$contentType; charset=utf-8");
590 0 $req->content($content) ;
591 }
592 }
593 0 $req->authorization_basic($user,$password);
594  
595 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
596 0 warn sprintf "---REQUEST---\n%s",$req->as_string;
597 }
598  
599 0 my $res = $ua->request($req);
600 0 ▶ if (!$res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
601 0 $self->throw(
602 'CLIENT-00001: HTTP request failed',
603 Request => $self->url($path),
604 Method => $method,
605 StatusLine => $res->status_line,
606 Response => $res->content,
607 );
608 }
609 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
610 0 warn sprintf "---RESPONSE---\n%s",$res->as_string;
611 }
612  
613 0 return $res;
614 }
615  
616 # -----------------------------------------------------------------------------
617  
618 =head3 url() - Erzeuge Request URL
619  
620 =head4 Synopsis
621  
622 $url = $cli->url;
623 $url = $cli->url($path);
624  
625 =head4 Arguments
626  
627 =over 4
628  
629 =item $path
630  
631 REST-API Pfad I<ohne> führenden Slash,
632 z.B. 'wiki/rest/api/content/32788'.
633  
634 =back
635  
636 =head4 Returns
637  
638 URL (String)
639  
640 =head4 Description
641  
642 Erzeuge einen REST-API URL bestehend aus dem beim Konstruktor-Aufruf
643 angegebenen Server-URL und dem Pfad $path und liefere diesen zurück.
644 Ohne Argument wird der Server-URL geliefert.
645  
646 =head4 Example
647  
648 Der Code
649  
650 $cli = Quiq::Confluence::Client->new(
651 url => 'https://<name>.atlassian.net',
652 ...
653 );
654 $url = $cli->url('wiki/rest/api/content/32788');
655  
656 liefert
657  
658 https://<name>.atlassian.net/wiki/rest/api/content/32788
659  
660 =cut
661  
662 # -----------------------------------------------------------------------------
663  
664 0 ▶sub url {
Subroutine url: not called CC=2 SCAR=17.9
Pod documented
665 0 my ($self,$path) = @_;
666  
667 0 my $url = $self->get('url');
668 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
669 0 $url .= "/$path";
670 }
671  
672 0 return $url;
673 }
674  
675 # -----------------------------------------------------------------------------
676  
677 =head1 VERSION
678  
679 1.240
680  
681 =head1 AUTHOR
682  
683 Frank Seitz, L<http://fseitz.de/>
684  
685 =head1 COPYRIGHT
686  
687 Copyright (C) 2026 Frank Seitz
688  
689 =head1 LICENSE
690  
691 This code is free software; you can redistribute it and/or modify
692 it under the same terms as Perl itself.
693  
694 =cut
695  
696 # -----------------------------------------------------------------------------
697  
698 1 1;
699  
700 # eof
« Quiq/Database/Cursor.pm ↑ Summary Quiq/ImagePool/Directory.pm »