Archived
1
0

Many fixes on API calls

This commit is contained in:
Mercier Pierre-Olivier 2013-09-12 12:55:56 +02:00
parent 43a1b1b518
commit fd15ae2f37
2 changed files with 135 additions and 20 deletions

View File

@ -29,6 +29,7 @@ sub make_response($$)
$root->appendChild($message);
$doc->setDocumentElement( $root );
return $doc->toString();
}
@ -39,13 +40,11 @@ sub parse($$)
my $sax_handler;
$sax_handler = ResultHandler->new($parsed) if ($mod eq "ResultHandler");
$sax_handler = ProjectMemberHandler->new($parsed) if ($mod eq "ProjectMemberHandler");
my $parser = XML::SAX::ParserFactory->parser( Handler => $sax_handler );
my $str = shift;
print $str;
$parser->parse_string($str);
$parser->parse_string(shift);
return $parsed;
}
@ -58,20 +57,28 @@ sub get($$)
log(DEBUG, 'GET Request to ', API_URL, $url);
my $req = GET API_URL . $url;
return parse($next, $ua->request($req)->content);
my $cnt = $ua->request($req)->content;
log TRACE, $cnt;
return parse($next, $cnt);
}
sub send($$@)
sub send($$$)
{
my $next = shift;
my $url = shift;
my $ua = LWP::UserAgent->new;
log(DEBUG, 'POST Request to ', API_URL, $url);
my $req = POST API_URL . $url, @_;
return parse($next, $ua->request($req)->content);
my $req = POST API_URL . $url, shift;
my $cnt = $ua->request($req)->content;
log TRACE, $cnt;
return parse($next, $cnt);
}
@ -87,6 +94,7 @@ sub new ($$)
my $self = {
parsed => shift,
inResult => 0,
inMessage => 0,
values => ""
};
@ -102,13 +110,77 @@ sub start_element
if ($element->{Name} eq "result") {
$self->{inResult} = 1;
}
elsif ($element->{Name} eq "message") {
$self->{inMessage} = 1;
}
}
sub characters
{
my ($self, $characters) = @_;
if ($self->{inResult}) {
if ($self->{inResult} || $self->{inMessage}) {
$self->{values} .= $characters->{Data};
}
}
sub end_element
{
my ($self, $element) = @_;
if ($element->{Name} eq "result")
{
$self->{parsed}{result} = $self->{values};
$self->{inResult} = 0;
$self->{values} = "";
}
elsif ($element->{Name} eq "message")
{
$self->{parsed}{message} = $self->{values};
$self->{inMessage} = 0;
$self->{values} = "";
}
}
package ProjectMemberHandler;
use v5.10.1;
use strict;
use warnings;
sub new ($$)
{
my $class = shift;
my $self = {
parsed => shift,
inResult => 0,
inUser => 0,
values => ""
};
bless $self, $class;
return $self;
}
sub start_element
{
my ($self, $element) = @_;
if ($element->{Name} eq "result") {
$self->{inResult} = 1;
}
elsif ($element->{Name} eq "user") {
$self->{inUser} = 1;
}
}
sub characters
{
my ($self, $characters) = @_;
if ($self->{inResult} or $self->{inUser}) {
$self->{values} .= $characters->{Data};
}
}
@ -122,6 +194,14 @@ sub end_element
$self->{inResult} = 0;
$self->{values} = "";
}
elsif ($element->{Name} eq "user") {
if (!$self->{parsed}{user}) {
$self->{parsed}{user} = ();
}
push @{ $self->{parsed}{user} }, $self->{values};
$self->{inUser} = 0;
$self->{values} = "";
}
}
1;

View File

@ -7,18 +7,23 @@ use strict;
use warnings;
use ACU::API::Base;
use ACU::LDAP;
sub add($)
sub add($;$)
{
my $project_name = shift;
my $year = shift;
if ($year and $year != LDAP::get_year) {
return "Impossible d'ajouter un projet d'une autre année : non implémenté"
}
my $res = API::Base::send('ResultHandler', "projects/projects/add.xml", [ project_name => $project_name ]);
if ($res->{result} eq '0') {
say "Projet ajouté correctement";
}
else {
say "Ajout non ok: ".$res->{message};
if ($res->{result} ne '0') {
return "Erreur durant l'ajout : ".$res->{message};
} else {
return 0;
}
}
@ -70,17 +75,47 @@ sub add_grades($;$)
my $year = shift;
my %data;
$data{project_name} = $project_name if ($project_name);
$data{year} = $year if ($year);
my $res = API::Base::send('ResultHandler', "projects/notes/add.xml", \%data);
my $res = API::Base::get('ResultHandler', "projects/groups/generate.xml");
if ($res->{result} && $res->{result} eq '0') {
say "Ajout de notes effectué.";
}
else {
say "Ajout non ok: ".$res->{message};
say "Ajout non ok: ", $res->{message};
}
return $res;
}
sub gen_groups($;$)
{
my $project_name = shift;
my $year = shift;
my %data;
$data{project_name} = $project_name if ($project_name);
$data{year} = $year if ($year);
my $url;
if ($year) {
$url = "projects/groups/generate/$project_name/$year.xml";
}
else {
$url = "projects/groups/generate/$project_name.xml";
}
my $res = API::Base::get('ResultHandler', $url);
if ($res->{result} eq '0') {
say "Génération des groupes OK.";
}
else {
say "Génération non ok: ", $res->{message};
}
return $res;