Skip to content

Commit

Permalink
actually handle OCS status responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Dagefoerde committed Aug 16, 2017
1 parent e7b9f33 commit 1974a5f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
41 changes: 41 additions & 0 deletions classes/request_exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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/>.

/**
* Exception for when an OCS request fails
*
* @package repository_owncloud
* @copyright 2017 Project seminar (Learnweb, University of Münster)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace repository_owncloud;

defined('MOODLE_INTERNAL') || die();

class request_exception extends \moodle_exception {

/**
* Constructor
* This exception is used when the configuration of the plugin can not be processed or database entries are
* missing.
* @param string $hint optional param for additional information of the problem
* @param string $debuginfo detailed information how to fix problem
*/
public function __construct($hint = '', $debuginfo = null) {
parent::__construct('request_exception', 'repository_owncloud', '', $hint, $debuginfo);
}
}
1 change: 1 addition & 0 deletions lang/en/repository_owncloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@

// Exceptions.
$string['configuration_exception'] = 'An error in the configuration of the OAuth 2 client occurred: {$a}';
$string['request_exception'] = 'A request to ownCloud has failed: {$a}';
17 changes: 8 additions & 9 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ public function get_listing($path='', $page = '') {
*
* @param string $url relative path to the chosen file
* @return string the generated downloadlink.
* @throws repository_exception if $url is empty an exception is thrown.
* @throws \repository_owncloud\request_exception If ownCloud responded badly
*
*/
public function get_link($url) {
// Use OCS to generate a public share to the requested file.
Expand All @@ -307,17 +308,15 @@ public function get_link($url) {
];

$response = $this->ocsclient->call('create_share', $ocsparams);

$ret = array();

$xml = simplexml_load_string($response);
$ret['code'] = $xml->meta->statuscode;
$ret['status'] = $xml->meta->status;

// Take the link and convert it into a download link.
$ret['link'] = $xml->data[0]->url[0] . "/download";
if (trim($xml->meta->status) !== 'ok') {
throw new \repository_owncloud\request_exception(sprintf('(%s) %s',
trim($xml->meta->statuscode), $xml->meta->message));
}

return $ret['link'];
// Take the link and convert it into a download link.
return trim($xml->data[0]->url[0]) . "/download";
}

/**
Expand Down
14 changes: 6 additions & 8 deletions tests/repository_owncloud_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,9 @@ public function test_get_link() {
<url>https://www.default.de</url>
</data>
<meta>
<statuscode>HTTP/1.1 200</statuscode>
<status>
OK
</status>
<statuscode>100</statuscode>
<status>ok</status>
<message/>
</meta>
</document>
XML;
Expand Down Expand Up @@ -401,10 +400,9 @@ public function test_get_file_reference_withoptionalparam() {
<url>https://www.default.de/somefile</url>
</data>
<meta>
<statuscode>HTTP/1.1 200</statuscode>
<status>
OK
</status>
<statuscode>100</statuscode>
<status>ok</status>
<message/>
</meta>
</document>
XML;
Expand Down

0 comments on commit 1974a5f

Please sign in to comment.