Skip to content

Commit

Permalink
client: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeCarrier committed Jun 16, 2018
1 parent 2ef71e6 commit ac23a89
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ A Moodle webservice protocol implementation that allows requests with bodies. It
1. Ensure _Enable web services_ is enabled under _Site administration_ > _Advanced features_.
2. Navigate to _Site administration_ > _Plugins_ > _Web services_ > _Manage protocols_ and enable _Loaded REST protocol_.
3. Assign the _Use Loaded REST protocol_ capability to the desired roles under _Site administration_ > _Users_ > _Permissions_ > _Define roles_.

## Sample client usage

```php
use webservice_loadedrest\client;

$serverurl = '/webservice/loadedrest/server.php';
$token = '000000000000000000000000000000000000';
$httpmethod = 'PUT';
$wsfunction = 'component_function';
$params = [
'myargs' => [
[
'param' => 'look, nesting!',
],
],
];

$client = new client($serverurl, $token);
try {
var_dump($client->call($httpmethod, $wsfunction, $params));
} catch (Exception $e) {
var_dump($e);
}
```
106 changes: 106 additions & 0 deletions classes/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Loaded REST.
*
* @package webservice_loadedrest
* @author Luke Carrier <[email protected]>
* @copyright 2018 Luke Carrier
*/

namespace webservice_loadedrest;

use moodle_exception;
use moodle_url;
use webservice_loadedrest\format\format_factory;

defined('MOODLE_INTERNAL') || die;

/**
* Test webservice client.
*/
class client {
/**
* Path to server.php, relative to wwwroot.
*
* @var string
*/
protected $serverurl;

/**
* Authentication token.
*
* @var string
*/
protected $token;

/**
* I/O format.
*
* @var string|null
*/
protected $formatname;

/**
* Initialiser.
*
* @param string $baseurl
* @param string $token
* @param string|null $format
*/
public function __construct($baseurl, $token, $format=null) {
$this->serverurl = $baseurl;
$this->token = $token;
$this->formatname = $format ?? format_factory::FORMAT_DEFAULT;
}

/**
* Call an external function.
*
* @param $httpmethod
* @param $wsfunction
* @param $bodyparams
*
* @return mixed
*
* @throws moodle_exception
*/
public function call($httpmethod, $wsfunction, $bodyparams) {
$format = format_factory::create($this->formatname);

$getparams = [
'wstoken' => $this->token,
'wsfunction' => $wsfunction,
];
$url = new moodle_url($this->serverurl, $getparams);
$headers = [
'Content-Type' => $this->formatname,
];
$body = $format->serialise($bodyparams);

$curl = curl_init($url->out(false));
curl_setopt_array($curl, [
CURLOPT_CUSTOMREQUEST => $httpmethod,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $body,
CURLOPT_RETURNTRANSFER => true,
]);
$result = curl_exec($curl);

return $format->deserialise($result);
}
}

0 comments on commit ac23a89

Please sign in to comment.