Archived
1
0

API refactor

This commit is contained in:
Mercier Pierre-Olivier 2013-09-12 16:48:28 +02:00
parent ca5d061fce
commit 42835c5930
2 changed files with 56 additions and 37 deletions

View File

@ -44,7 +44,22 @@ sub parse($$)
my $parser = XML::SAX::ParserFactory->parser( Handler => $sax_handler );
$parser->parse_string(shift);
eval {
$parser->parse_string(shift);
};
if ($@)
{
$parsed->{result} = 256;
$parsed->{message} = "Erreur du parser.";
log WARN, "Erreur du parser";
}
if (! exists $parsed->{result})
{
$parsed->{result} = -1;
$parsed->{message} = "Pas de code de retour fourni." if (! exists $parsed->{message});
}
$parsed->{message} = "" if (! exists $parsed->{message});
return $parsed;
}

View File

@ -5,6 +5,7 @@ package API::Projects;
use v5.10.1;
use strict;
use warnings;
use Carp;
use ACU::API::Base;
use ACU::LDAP;
@ -15,16 +16,18 @@ sub add($;$)
my $year = shift;
if ($year and $year != LDAP::get_year) {
return "Impossible d'ajouter un projet d'une autre année : non implémenté"
croak "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 ]);
my $res = API::Base::send('ResultHandler',
"projects/projects/add.xml",
[ project_name => $project_name ]);
if ($res->{result} ne '0') {
return "Erreur durant l'ajout : ".$res->{message};
} else {
return 0;
croak "Erreur durant l'ajout : ".$res->{message};
}
return $res;
}
sub get($;$)
@ -32,13 +35,11 @@ sub get($;$)
my $project_name = shift;
my $year = shift;
my $res = API::Base::get('ProjectMemberHandler', "projects/projects/get/$project_name.xml");
my $res = API::Base::get('ProjectMemberHandler',
"projects/projects/get/$project_name.xml");
if ($res->{result} && $res->{result} eq '0') {
say "Projet ajouté correctement";
}
else {
say "Ajout non ok: ".$res->{message};
if ($res->{result} ne '0') {
croak "Erreur durant l'ajout : " . $res->{message};
}
return $res;
@ -52,18 +53,14 @@ sub get_users($;$)
my $url;
if ($year) {
$url = "projects/projects/users/$project_name/$year.xml";
}
else {
} else {
$url = "projects/projects/users/$project_name.xml";
}
my $res = API::Base::get('ProjectMemberHandler', $url);
if ($res->{result} && $res->{result} eq '0') {
say "Projet ajouté correctement";
}
else {
say "Ajout non ok: ".$res->{message};
if ($res->{result} ne '0') {
croak "Erreur durant l'ajout : " . $res->{message};
}
return $res;
@ -71,22 +68,32 @@ sub get_users($;$)
sub add_grades($;$)
{
my $project_name = shift;
my $year = shift;
my %data;
$data{project_name} = $project_name if ($project_name);
$data{year} = $year if ($year);
my %data = (
project_name => shift
);
$data{year} = $_ if (shift);
my $res = API::Base::get('ResultHandler', "projects/groups/generate.xml");
if ($res->{result} && $res->{result} eq '0') {
say "Ajout de notes effectué.";
if ($res->{result} ne '0') {
croak "Erreur durant l'ajout : " . $res->{message};
}
else {
say "Ajout non ok: ", $res->{message};
return $res;
}
sub add_traces($$;$)
{
my %data = (
project_name => shift,
trace_name => shift,
);
$data{year} = $_ if (shift);
my $res = API::Base::send('ResultHandler', "projects/traces/add.xml", \%data);
if ($res->{result} ne '0') {
croak "Erreur durant l'ajout : " . $res->{message};
}
return $res;
@ -109,13 +116,10 @@ sub gen_groups($;$)
$url = "projects/groups/generate/$project_name.xml";
}
my $res = API::Base::get('ResultHandler', $url);
my $res = API::Base::send('ResultHandler', $url, %data);
if ($res->{result} eq '0') {
say "Génération des groupes OK.";
}
else {
say "Génération non ok: ", $res->{message};
if ($res->{result} ne '0') {
croak "Erreur lors de la génération de groupes : " . $res->{message};
}
return $res;