Skip to content

Commit

Permalink
maintenance and new API v1
Browse files Browse the repository at this point in the history
  • Loading branch information
korelstar committed Apr 13, 2020
1 parent 02fdcee commit ffe6a02
Show file tree
Hide file tree
Showing 27 changed files with 1,057 additions and 1,013 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ appstore: clean lint build-js-production

### from vueexample

all: dev-setup lint build-js-production test
all: dev-setup build-js-production

# Dev env management
dev-setup: clean clean-dev init
Expand Down
111 changes: 69 additions & 42 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
<?php
/**
* Nextcloud - Notes
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Bernhard Posselt <[email protected]>
* @copyright Bernhard Posselt 2012, 2014
*/

return ['routes' => [
// page
////////// P A G E //////////
[
'name' => 'page#index',
'url' => '/',
Expand All @@ -30,7 +20,8 @@
'requirements' => ['id' => '\d+'],
],

// notes

////////// N O T E S //////////
[
'name' => 'notes#index',
'url' => '/notes',
Expand Down Expand Up @@ -59,22 +50,13 @@
'requirements' => ['id' => '\d+'],
],
[
'name' => 'notes#category',
'url' => '/notes/{id}/category',
'verb' => 'PUT',
'requirements' => ['id' => '\d+'],
],
[
'name' => 'notes#title',
'url' => '/notes/{id}/title',
'verb' => 'PUT',
'requirements' => ['id' => '\d+'],
],
[
'name' => 'notes#favorite',
'url' => '/notes/{id}/favorite',
'name' => 'notes#updateProperty',
'url' => '/notes/{id}/{property}',
'verb' => 'PUT',
'requirements' => ['id' => '\d+'],
'requirements' => [
'id' => '\d+',
'property' => '(modified|title|category|favorite)',
],
],
[
'name' => 'notes#destroy',
Expand All @@ -83,43 +65,88 @@
'requirements' => ['id' => '\d+'],
],

// api

////////// S E T T I N G S //////////
['name' => 'settings#set', 'url' => '/settings', 'verb' => 'PUT'],
['name' => 'settings#get', 'url' => '/settings', 'verb' => 'GET'],


////////// A P I //////////
[
'name' => 'notes_api#index',
'url' => '/api/v0.2/notes',
'url' => '/api/{apiVersion}/notes',
'verb' => 'GET',
'requirements' => [
'apiVersion' => '(v0.2|v1)',
],
],
[
'name' => 'notes_api#get',
'url' => '/api/v0.2/notes/{id}',
'url' => '/api/{apiVersion}/notes/{id}',
'verb' => 'GET',
'requirements' => ['id' => '\d+'],
'requirements' => [
'apiVersion' => '(v0.2|v1)',
'id' => '\d+',
],
],
[
'name' => 'notes_api#createAutoTitle',
'url' => '/api/{apiVersion}/notes',
'verb' => 'POST',
'requirements' => [
'apiVersion' => '(v0.2)',
],
],
[
'name' => 'notes_api#create',
'url' => '/api/v0.2/notes',
'url' => '/api/{apiVersion}/notes',
'verb' => 'POST',
'requirements' => [
'apiVersion' => '(v1)',
],
],
[
'name' => 'notes_api#updateAutoTitle',
'url' => '/api/{apiVersion}/notes/{id}',
'verb' => 'PUT',
'requirements' => [
'apiVersion' => '(v0.2)',
'id' => '\d+',
],
],
[
'name' => 'notes_api#update',
'url' => '/api/v0.2/notes/{id}',
'url' => '/api/{apiVersion}/notes/{id}',
'verb' => 'PUT',
'requirements' => ['id' => '\d+'],
'requirements' => [
'apiVersion' => '(v1)',
'id' => '\d+',
],
],
[
'name' => 'notes_api#destroy',
'url' => '/api/v0.2/notes/{id}',
'url' => '/api/{apiVersion}/notes/{id}',
'verb' => 'DELETE',
'requirements' => ['id' => '\d+'],
'requirements' => [
'apiVersion' => '(v0.2|v1)',
'id' => '\d+',
],
],
[
'name' => 'notes_api#fail',
'url' => '/api/{catchAll}',
'verb' => 'GET',
'requirements' => [
'catchAll' => '.*',
],
],
[
'name' => 'notes_api#preflighted_cors',
'url' => '/api/v0.2/{path}',
'url' => '/api/{apiVersion}/{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+'],
'requirements' => [
'apiVersion' => '(v0.2|v1)',
'path' => '.+',
],
],

// settings
['name' => 'settings#set', 'url' => '/settings', 'verb' => 'PUT'],
['name' => 'settings#get', 'url' => '/settings', 'verb' => 'GET'],
]];
2 changes: 2 additions & 0 deletions lib/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Application extends App {

public static $API_VERSIONS = [ '0.2', '1.0' ];

public function __construct(array $urlParams = []) {
parent::__construct('notes', $urlParams);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Capabilities implements ICapability {
public function getCapabilities() {
return [
'notes' => [
'api_version' => [ '0.2' ],
'api_version' => Application::$API_VERSIONS,
],
];
}
Expand Down
27 changes: 0 additions & 27 deletions lib/Controller/Errors.php

This file was deleted.

43 changes: 43 additions & 0 deletions lib/Controller/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace OCA\Notes\Controller;

use OCA\Notes\Application;
use OCA\Notes\Service\InsufficientStorageException;
use OCA\Notes\Service\NoteDoesNotExistException;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\ILogger;

class Helper {

private $logger;
private $appName;

public function __construct(
ILogger $logger,
string $appName
) {
$this->logger = $logger;
$this->appName = $appName;
}

public function handleErrorResponse(callable $respond) : DataResponse {
try {
$data = $respond();
$response = $data instanceof DataResponse ? $data : new DataResponse($data);
} catch (NoteDoesNotExistException $e) {
$this->logger->logException($e, [ 'app' => $this->appName ]);
$response = new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (InsufficientStorageException $e) {
$this->logger->logException($e, [ 'app' => $this->appName ]);
$response = new DataResponse([], Http::STATUS_INSUFFICIENT_STORAGE);
} catch (\Throwable $e) {
$this->logger->logException($e, [ 'app' => $this->appName ]);
$response = new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
$response->addHeader('X-Notes-API-Versions', implode(', ', Application::$API_VERSIONS));
return $response;
}
}
Loading

0 comments on commit ffe6a02

Please sign in to comment.