Permalink
Browse files

MDL-29279 REST server can return json format

  • Loading branch information...
1 parent 9cfaebb commit a08b4cc8ebd7e4b9b0e0f627b8fec9d9dcba0c72 @mouneyrac mouneyrac committed Sep 19, 2011
Showing with 63 additions and 16 deletions.
  1. +50 −15 webservice/rest/locallib.php
  2. +13 −1 webservice/rest/server.php
@@ -30,12 +30,17 @@
* @author Petr Skoda (skodak)
*/
class webservice_rest_server extends webservice_base_server {
+
+ /** @property string $alt return method (XML / JSON) */
+ protected $restformat;
+
/**
* Contructor
*/
- public function __construct($authmethod) {
+ public function __construct($authmethod, $restformat = 'xml') {
parent::__construct($authmethod);
$this->wsname = 'rest';
+ $this->restformat = ($restformat != 'xml' && $restformat != 'json')?'xml':$restformat; //sanity check, we accept only xml or json
}
/**
@@ -78,11 +83,27 @@ protected function parse_request() {
*/
protected function send_response() {
$this->send_headers();
- $xml = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
- $xml .= '<RESPONSE>'."\n";
- $xml .= self::xmlize_result($this->returns, $this->function->returns_desc);
- $xml .= '</RESPONSE>'."\n";
- echo $xml;
+ if ($this->restformat == 'json') {
+ try {
+ $response = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
+ } catch (Exception $ex) {
+ $error = new stdClass;
+ $error->exception = get_class($ex);
+ $error->message = $ex->getMessage();
+ if (debugging() and isset($ex->debuginfo)) {
+ $error->debuginfo = $ex->debuginfo;
+ }
+ echo json_encode($error);
+ }
+ $json = json_encode($response);
+ echo $json;
+ } else {
+ $xml = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
+ $xml .= '<RESPONSE>'."\n";
+ $xml .= self::xmlize_result($this->returns, $this->function->returns_desc);
+ $xml .= '</RESPONSE>'."\n";
+ echo $xml;
+ }
}
/**
@@ -93,23 +114,37 @@ protected function send_response() {
*/
protected function send_error($ex=null) {
$this->send_headers();
- $xml = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
- $xml .= '<EXCEPTION class="'.get_class($ex).'">'."\n";
- $xml .= '<MESSAGE>'.htmlentities($ex->getMessage(), ENT_COMPAT, 'UTF-8').'</MESSAGE>'."\n";
- if (debugging() and isset($ex->debuginfo)) {
- $xml .= '<DEBUGINFO>'.htmlentities($ex->debuginfo, ENT_COMPAT, 'UTF-8').'</DEBUGINFO>'."\n";
+ if ($this->restformat == 'json') {
+ $error = new stdClass;
+ $error->exception = get_class($ex);
+ $error->message = $ex->getMessage();
+ if (debugging() and isset($ex->debuginfo)) {
+ $error->debuginfo = $ex->debuginfo;
+ }
+ echo json_encode($error);
+ } else {
+ $xml = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
+ $xml .= '<EXCEPTION class="'.get_class($ex).'">'."\n";
+ $xml .= '<MESSAGE>'.htmlentities($ex->getMessage(), ENT_COMPAT, 'UTF-8').'</MESSAGE>'."\n";
+ if (debugging() and isset($ex->debuginfo)) {
+ $xml .= '<DEBUGINFO>'.htmlentities($ex->debuginfo, ENT_COMPAT, 'UTF-8').'</DEBUGINFO>'."\n";
+ }
+ $xml .= '</EXCEPTION>'."\n";
+ echo $xml;
}
- $xml .= '</EXCEPTION>'."\n";
- echo $xml;
}
/**
* Internal implementation - sending of page headers.
* @return void
*/
protected function send_headers() {
- header('Content-Type: application/xml; charset=utf-8');
- header('Content-Disposition: inline; filename="response.xml"');
+ if ($this->restformat == 'json') {
+ header('Content-type: application/json');
+ } else {
+ header('Content-Type: application/xml; charset=utf-8');
+ header('Content-Disposition: inline; filename="response.xml"');
+ }
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
View
@@ -34,7 +34,19 @@
die;
}
-$server = new webservice_rest_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
+$restformat = optional_param('moodlewsrestformat', 'xml', PARAM_ALPHA);
+//remove the alt from the request
+if(isset($_REQUEST['moodlewsrestformat'])) {
+ unset($_REQUEST['moodlewsrestformat']);
+}
+if(isset($_GET['moodlewsrestformat'])) {
+ unset($_GET['moodlewsrestformat']);
+}
+if(isset($_POST['moodlewsrestformat'])) {
+ unset($_POST['moodlewsrestformat']);
+}
+
+$server = new webservice_rest_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, $restformat);
$server->run();
die;

0 comments on commit a08b4cc

Please sign in to comment.