Using Perl to access the JAZZ REST API
Introduction
The following article will help the reader to see how simple and powerful using the PERL language to access the JAZZ Server REST API can be.
The following practical example will demonstrate how to create a project area. The example is a project area for Rational Team Concert, but could be for any other product that is part of the Rational solution for Collaborative Lifecycle Management, version 3.0. or 4.0.
The reader of this article should have basic knowledge of Perl and consider reading about the JAZZ server API draft specification
Description
So, to stop beating around the bush, here is the Perl code that creates the project area:
use strict; use LWP::UserAgent; use HTTP::Request::Common; use HTTP::Cookies; my $userAgent = LWP::UserAgent->new(agent => 'perl get'); $userAgent->cookie_jar({}); my $name = "TESTPA"; #Authenticate my $response = $userAgent->request(POST 'https://localhost:9443/jts/authenticated/j_security_check?j_username=jtsadmin&j_password=jazz123'); if(!existsPA($name,$userAgent)) { createPA($name,$userAgent); print "Created PA $namen"; } sub existsPA { my ($name, $userAgent) = @_; #Get Project Areas my $response = $userAgent->request(GET 'https://localhost:9443/ccm/process/project-areas'); $response->header(Accept =>"text/html, */*;q=0.1"); print $response->error_as_HTML unless $response->is_success; my $response_string = $response->as_string; my @response_lines = split(/n/,$response_string); my $i = 0; foreach my $line (@response_lines) { my ($pa_name) = ($line =~ /project-area jp06:name="(S+)"/); if ($pa_name eq $name) { return "true"; } } return ""; } sub createPA { my ($name, $userAgent) = @_; #Post to create PA my $message = "<?xml version="1.0" encoding="UTF-8"?> <jp06:project-area xmlns:jp06="http://jazz.net/xmlns/prod/jazz/process/0.6/" jp06:name="${name}" jp06:templateId="scrum2.process.ibm.com" jp06:templateLocale="es"> <jp06:summary>${name}</jp06:summary> <jp06:description>${name}</jp06:description> <jp06:visibility jp06:access="PUBLIC"/> </jp06:project-area>"; my $response = $userAgent->request(POST 'https://localhost:9443/ccm/process/project-areas', Content_Type => 'text/xml', Referer => 'https://localhost:9443/ccm/admin' , Content => $message); die $response->error_as_HTML unless $response->is_success; print $response->as_string; }
Please note: The services entry points are hard-coded (eg: https://localhost:9443/ccm/process/project-areas). You can tune the code and add parameters as desired.
You can see how the first part of the code creates a connection to the server JAZZ. This is performed with a POST request.
my $response = $userAgent->request(POST 'https://localhost:9443/jts/authenticated/j_security_check?j_username=jtsadmin&j_password=jazz123');
It is noteworthy that the connection created should store the cookies that are generated:
$userAgent->cookie_jar({});
This ensures that once authenticated to the server it reuses the cookie credentials for future connections
The function "existsPA" uses
my $response = $userAgent->request(GET 'https://localhost:9443/ccm/process/project-areas');
to retreive all the project areas in the server and check if a project area with same name already exists on the server.
Finally, function "createPA" performs a POST request to create the project area
my $response = $userAgent->request(POST 'https://localhost:9443/ccm/process/project-areas', Content_Type => 'text/xml', Referer => 'https://localhost:9443/ccm/admin' , Content => $message);
Where $response is the response from the server, "Referer" is mandatory to be set to "https://localhost:9443/ccm/admin" otherwise project area creation is not allowed and $message is the XML body with the project area request. Please check link to draft JAZZ REST API specification https://jazz.net/wiki/bin/view/Main/DraftTeamProcessRestApi for futher details about XML body.
It should be retrieved a 201 response from server that confirms the project area has been created when the code is executed
Through the server web interface https://localhost:9443/ccm/admin#action=jazz.viewPage&id=com.ibm.team.process.ProjectAreaManagement you should see the new project area created "TESTPA".
References
- Basis of PERL LWP library can be found at this interesting article http://www.perl.com/pub/2002/08/20/perlandlwp.html
- LWP specification can be found at CPAN library http://search.cpan.org/dist/libwww-perl/lib/LWP.pm
- Draft Jazz REST API specification can be found at https://jazz.net/wiki/bin/view/Main/DraftTeamProcessRestApi
- Information about Firefox poster can be found at https://addons.mozilla.org/en-us/firefox/addon/poster/
About the author
Luis Bazo Garcia is an Expert Certified IT Specialist in the area of development tools working for IBM Spain since 2006 He can be contacted at luis.bazo@es.ibm.com
Copyright © 2012 IBM Corporation