2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Rest API base class mapping rest api methods to endpoints with http methods, args and post body.
21 * @copyright 2017 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core\oauth2;
30 defined('MOODLE_INTERNAL') || die();
32 require_once($CFG->libdir . '/filelib.php');
35 * Rest API base class mapping rest api methods to endpoints with http methods, args and post body.
37 * @copyright 2017 Damyon Wiese
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 /** @var curl $curl */
50 public function __construct(curl $curl) {
55 * Abstract function to define the functions of the rest API.
57 * @return array Example:
58 * [ 'listFiles' => [ 'method' => 'get', 'args' => [ 'folder' => PARAM_STRING ], 'response' => 'json' ] ]
60 public abstract function get_api_functions();
63 * Call a function from the Api with a set of arguments and optional data.
65 * @param string $functionname
66 * @param array $functionargs
67 * @param string $rawpost Optional param to include in the body of a post.
68 * @param string $contenttype The MIME type for the request's Content-Type header.
69 * @return string|stdClass
71 public function call($functionname, $functionargs, $rawpost = false, $contenttype = false) {
72 $functions = $this->get_api_functions();
73 $supportedmethods = [ 'get', 'put', 'post', 'patch', 'head', 'delete' ];
74 if (empty($functions[$functionname])) {
75 throw new coding_exception('unsupported api functionname: ' . $functionname);
78 $method = $functions[$functionname]['method'];
79 $endpoint = $functions[$functionname]['endpoint'];
81 $responsetype = $functions[$functionname]['response'];
82 if (!in_array($method, $supportedmethods)) {
83 throw new coding_exception('unsupported api method: ' . $method);
86 $args = $functions[$functionname]['args'];
88 foreach ($args as $argname => $argtype) {
89 if (isset($functionargs[$argname])) {
90 $callargs[$argname] = clean_param($functionargs[$argname], $argtype);
94 // Allow params in the URL path like /me/{parent}/children.
95 foreach ($callargs as $argname => $value) {
96 $newendpoint = str_replace('{' . $argname . '}', $value, $endpoint);
97 if ($newendpoint != $endpoint) {
98 $endpoint = $newendpoint;
99 unset($callargs[$argname]);
103 if ($rawpost !== false) {
104 $queryparams = $this->curl->build_post_data($callargs);
105 if (!empty($queryparams)) {
106 $endpoint .= '?' . $queryparams;
108 $callargs = $rawpost;
111 if (empty($contenttype)) {
112 $this->curl->setHeader('Content-type: application/json');
114 $this->curl->setHeader('Content-type: ' . $contenttype);
116 $response = $this->curl->$method($endpoint, $callargs);
118 if ($this->curl->errno == 0) {
119 if ($responsetype == 'json') {
120 $json = json_decode($response);
122 if (!empty($json->error)) {
123 throw new rest_exception($json->error->code . ': ' . $json->error->message);
126 } else if ($responsetype == 'headers') {
127 $response = $this->curl->get_raw_response();
131 throw new rest_exception($this->curl->error, $this->curl->errno);