Commit e56ed9a9 authored by Guillaume Huard's avatar Guillaume Huard
Browse files

push basique fonctionnel

parent 44b2df48
......@@ -3,51 +3,114 @@ use strict;
use LWP::UserAgent;
use Data::Dumper;
use File::Slurp;
use Getopt::Long qw(:config bundling);
use XML::LibXML;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $url = 'https://moodle.caseine.org/webservice/rest/server.php';
my $vplid='19435';
my $token='3d610600e046a8669cb39ec4798f8a56';
my $ua = LWP::UserAgent->new( agent => 'Secret');
my $debug=0;
my $vplid=$ENV{'CASEINE_VPLID'};
my $token=$ENV{'CASEINE_TOKEN'};
my $res = $ua->post( $url,
Content_Type => 'form-data',
Content => [
moodlewsrestformat => 'json',
id => "$vplid",
wstoken => "$token",
wsfunction => 'mod_vpl_set_setting',
settingname => 'name',
settingvalue => 'Essai',
],
sub help();
sub failure($);
sub debug($) {
print shift if ($debug);
}
sub save($@) {
my $content = shift;
my $num=0;
failure "Missing files" unless scalar(@_);
foreach my $file (@_) {
debug("Adding $file...\n");
my $data = read_file($file);
push @{$content}, "files[$num][name]", $file, "files[$num][data]", $data;
$num++;
}
}
sub get_setting($@) {
my $content = shift;
my $setting = shift;
failure "Extraneous arguments" unless not scalar(@_);
push @{$content}, "settingname", $setting;
}
sub set_setting($@) {
my $content = shift;
my $setting = shift;
failure "Extraneous arguments" unless not scalar(@_);
my ($name, $value) = split /=/, $setting;
failure "Invalid setting" unless not defined($value);
push @{$content}, "settingname", $name, "settingvalue", $value;
}
my %handlers = (
'save' => \&save,
'save_required_files' => \&save,
'save_corrected_files' => \&save,
'save_execution_files' => \&save,
'get_setting' => \&get_setting,
'set_setting' => \&set_setting
);
my @commands = keys(%handlers);
# print Dumper($res);
sub help() {
print <<"END";
usage :
$0 [ --help ] [ --token value ] [ --vplid value ] command [ arguments ]
my $data = read_file('./local.sh');
my $res = $ua->post( $url,
Content_Type => 'form-data',
Content => [
moodlewsrestformat => 'json',
Pushes data to caseine, where :
- token is the personnal token associated to the user of this script (by default, the value of the CASEINE_TOKEN environment variable)
- vplid is the id of the involved VPL (by default, the value of the CASEINE_VPLID environment variable)
- command is one of : @commands and its arguments are :
- filenames for save commands
- a setting name for get_setting
- an assignment of the form name=value for set_setting
END
exit(0);
}
sub failure($) {
print "@_\n";
help;
}
GetOptions('help|h|?' => \&help,
'vplid|v=s' => \$vplid,
'token|t=s' => \$token,
'debug|d' => \$debug
);
#failure "Missing command" if scalar(@ARGV) == 0;
debug "VPLID = $vplid, TOKEN = $token, ARGS = @ARGV\n";
my $command = shift @ARGV;
my $content = [
id => "$vplid",
wstoken => "$token",
wsfunction => 'mod_vpl_save_execution_files',
"files[0][name]" => 'local.sh',
"files[0][data]" => "$data",
],
wsfunction => "mod_vpl_$command"
];
failure "Unknown command" unless exists($handlers{$command});
&{$handlers{$command}}($content, @ARGV);
my $ua = LWP::UserAgent->new( agent => 'Secret');
my $res = $ua->post( $url,
Content_Type => 'form-data',
Content => $content
);
# Classic post does not work
# my $data = read_file('./local.sh');
# my $res = $ua->post( $url,
# Content_Type => 'form-data',
# Content => [
# moodlewsrestformat => 'json',
# id => "$vplid",
# wstoken => "$token",
# wsfunction => 'mod_vpl_save_execution_files',
# "files[0]" => [ './local.sh', 'local.sh' ],
# ],
# );
print Dumper($res);
if (exists($res->{'_content'})) {
my $answer = $res->{'_content'};
my $parser = XML::LibXML->new();
my $tree = $parser->parse_string($answer);
my $root = $tree->getDocumentElement;
my @values = $root->getElementsByTagName('VALUE');
print $values[0]->getFirstChild->getData, "\n";
} else {
failure "Invalid answer from server";
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment