REST in Perl and role_assign_roles

REST in Perl and role_assign_roles

by Pia Liersch -
Number of replies: 3

I've a Problem with the correct array on Perl.

That is more a Perl Problem, but maybe can someone help me.

 

That is my try

    my $function = "core_role_assign_roles";

    my %assignment = ();  
#my %assignment = ('roleid' => $role_id, 'userid' => $user_id, 'contextid' => $context_id,);
    $assignment{"roleid"} = $role_id;
    $assignment{"userid"} = $user_id;
    $assignment{"contextid"} = $context_id;
    my $assignments = (%assignment);
    my $params = {assignments => %assignment};

    #my %params = ("assignments" => ["roleid" => $role_id, "userid" => $user_id, "contextid" => $context_id]);

 

<EXCEPTION class="invalid_parameter_exception">

<MESSAGE>Ungültiger Parameterwert gefunden</MESSAGE>

<DEBUGINFO>assignments =&gt; Ungültiger Parameterwert gefunden: Only arrays accepted. The bad value is: 'contextid'</DEBUGINFO>

</EXCEPTION>




Average of ratings: -
In reply to Pia Liersch

Re: REST in Perl and role_assign_roles

by Emmanuel Otton -

I had the same problem, with the same error message.

After trying tou see what did really arrive to my ws, by adding this in rest/locallib.php, in the webservice_rest_constructor:

$mes_parametres = array_merge($_GET,$_POST);
var_dump($mes_parametres);

I finally found the solution to use web services from Perl, thanks to an answer by Richard Gillette on this forum ( http://moodle.org/mod/forum/discuss.php?d=182471 ).

The key is not to try to pass structures to the ws, but to split your data in atomic items, each one identified by a php-array-like identifier.

The php cgi library seems not to understand anything else, and happily recreates the necessary data structure as described in ws documentation.

Do give us your feedback on this, as we perl programmers feel a bit lonely in this php world sad

Here is a short piece of code that will create a course (or more):

#!/usr/bin/perl 
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->ssl_opts( verify_hostname => 0 );
my $urlws = "https://mymoodleserver.mines-albi.fr/webservice/rest/server.php";
my $token = "myrealtokenisnotthisoneofcourse";
my $params = {
    'wstoken' => $token,
    'wsfunction' => 'core_course_create_courses',
    'moodlewsrestformat' => 'json',
    'courses[0][fullname]' => 'My first API-created course',
    'courses[0][shortname]' => 'I_DID_IT_01',
    'courses[0][categoryid]' => 4    ,
    'courses[0][idnumber]' => 'xxxxx1',
    'courses[0][format]' => 'topics' ,
    'courses[0][numsections]' => 3   ,
    'courses[1][fullname]' => 'My second API-created course',
.../... and so on
};

my $result = $ua->post( $urlws, $params );
if ($result->is_success) {
    print $result->content, "\n";
} else {
    print $result->status_line, "\n";
}

Average of ratings: Useful (1)
In reply to Emmanuel Otton

Re: REST in Perl and role_assign_roles

by Jérôme Mouneyrac -

Hi Emmanuel,

thanks for the code, I vote it as Useful.

Do you have time to make a PERL-REST demo client like the other ones: https://github.com/moodlehq/sample-ws-clients

If yes, you can clone the git repository and add a folder for a PERL-REST. Create an tracker issue for it. I'll test it (I don't know much about PERL but I guess I should manage testing smile) and I'll push your commits into the repository.

It would be useful for other PERL developers.

Cheers,
Jerome 

In reply to Jérôme Mouneyrac

Re: REST in Perl and role_assign_roles

by Pia Liersch -

A Example in Perl and it works fine...

 

sub webservice{
    my ($shortname,$function,$params) = @_;

    my $wsusername = "XXXX";
    my $wspasswd = "XXXX";

    my $browser = LWP::UserAgent->new;
    my $url = $baseurl."/".$shortname."/webservice/rest/simpleserver.php";
    my $server_param = $url . "?wsusername=" . $wsusername . "&wspassword=" . $wspasswd . "&wsfunction=".$function;

    my $response = $browser->post($server_param,$params);

    return $response->content;
}
# Webservice ermittelt Moodle User mit Hilfe des Usernamen
sub ws_userid{
    my ($shortname,$username) = @_;
    
    my $function = "local_plws_user_get_userid_by_username";
    my $params = {"username" => $username};

    my $response = &webservice($shortname,$function,$params);
    my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1);
    my $ref = XMLin($response);
    
    my $userid = $ref->{VALUE};

    return $userid;
}