Browse Source

Fill in more of the REST handler.

Not yet functional.
microsub
J. King 7 years ago
parent
commit
9a8def03c2
  1. 1
      composer.json
  2. 46
      composer.lock
  3. 47
      lib/REST.php
  4. 8
      lib/REST/Handler.php
  5. 11
      lib/REST/NextCloudNews/Versions.php
  6. 21
      lib/REST/Request.php
  7. 7
      lib/REST/Response.php
  8. 5
      tests/test.php

1
composer.json

@ -22,7 +22,6 @@
"ext-intl": "*",
"ext-iconv": "*",
"fguillot/picofeed": ">=0.1.31",
"respect/rest": "^0.6",
"jkingweb/druuid": "^3.0",
"phpseclib/phpseclib": "^2.0",
"hosteurope/password-generator": "^1.0",

46
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "31bedf1105f6249d6e9d990abd2125de",
"content-hash": "4afb7b406ec21a2c18f46fd176fc242f",
"packages": [
{
"name": "fguillot/picofeed",
@ -236,50 +236,6 @@
],
"time": "2016-10-04T00:57:04+00:00"
},
{
"name": "respect/rest",
"version": "0.6.0",
"source": {
"type": "git",
"url": "https://github.com/Respect/Rest.git",
"reference": "4f60aa199623d738a3391ca1e80bafb754bc9308"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Respect/Rest/zipball/4f60aa199623d738a3391ca1e80bafb754bc9308",
"reference": "4f60aa199623d738a3391ca1e80bafb754bc9308",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Respect\\": "library/Respect"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD Style"
],
"authors": [
{
"name": "Alexandre Gaigalas",
"email": "alexandre@gaigalas.net"
},
{
"name": "Respect/Data Contributors",
"homepage": "https://github.com/Respect/Data/graphs/contributors"
}
],
"description": "Thin controller for RESTful applications",
"homepage": "http://respect.li/",
"time": "2015-01-12T09:55:16+00:00"
},
{
"name": "webmozart/assert",
"version": "1.2.0",

47
lib/REST.php

@ -1,8 +1,53 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
use Respect\Rest\Router;
class REST {
protected $apis = [
// NextCloud News version enumerator
'ncn' => [
'match' => '/index.php/apps/news/api',
'strip' => '/index.php/apps/news/api',
'class' => REST\NextCloudNews\Versions::class,
],
// NextCloud News v1-2 https://github.com/nextcloud/news/blob/master/docs/externalapi/Legacy.md
'ncn_v1-2' => [
'match' => '/index.php/apps/news/api/v1-2/',
'strip' => '/index.php/apps/news/api/v1-2',
'class' => REST\NextCloudNews\V1_2::class,
],
// Other candidates:
// NextCloud News v2 https://github.com/nextcloud/news/blob/master/docs/externalapi/External-Api.md
// Feedbin v1 https://github.com/feedbin/feedbin-api/commit/86da10aac5f1a57531a6e17b08744e5f9e7db8a9
// Feedbin v2 https://github.com/feedbin/feedbin-api
// Tiny Tiny RSS https://tt-rss.org/gitlab/fox/tt-rss/wikis/ApiReference
// Fever https://feedafever.com/api
// NewsBlur http://www.newsblur.com/api
];
protected $data;
function __construct(RuntimeData $data) {
$this->data = $data;
}
function dispatch(REST\Request $req = null): bool {
if($req===null) $req = new REST\Request();
$api = $this->apiMatch($url, $this->apis);
$req->url = substr($url,strlen($this->apis[$api]['strip']));
$class = $this->apis[$api]['class'];
$drv = new $class($this->data);
$drv->dispatch($req);
return true;
}
function apiMatch(string $url, array $map): string {
// sort the API list so the longest URL prefixes come first
uasort($map, function($a, $b) {return (strlen($a['match']) <=> strlen($b['match'])) * -1;});
// find a match
foreach($map as $id => $api) {
if(strpos($url, $api['match'])===0) return $id;
}
// or throw an exception otherwise
throw new REST\ExceptionURL("apiNotSupported", $url);
}
}

8
lib/REST/Handler.php

@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\REST;
interface Handler {
function __construct(\JKingWeb\NewsSync\RuntimeData $data);
function dispatch(Request $req): Response;
}

11
lib/REST/NextCloudNews/Versions.php

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\REST\NextCloudNews;
class Versions implements \JKingWeb\NewsSync\REST\Handler {
function __construct(\JKingWeb\NewsSync\RuntimeData $data) {
}
function dispatch(\JKingWeb\NewsSync\REST\Request $req): \JKingWeb\NewsSync\REST\Response {
}
}

21
lib/REST/Request.php

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\REST;
class Request {
public $method;
public $url;
public $type;
public $stream;
function __construct(string $method = null, string $url = null, string $bodyStream = null, string $contentType = null) {
if(is_null($method)) $method = $_SERVER['REQUEST_METHOD'];
if(is_null($url)) $url = $_SERVER['REQUEST_URI'];
if(is_null($bodyStream)) $bodyStream = "php://input";
if(is_null($contentType)) $contentType = $_SERVER['HTTP_CONTENT_TYPE'];
$this->method = method;
$this->url = $url;
$this->stream = $bodyStream;
$this->type = $contentType;
}
}

7
lib/REST/Response.php

@ -0,0 +1,7 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\REST;
class Response {
// stub
}

5
tests/test.php

@ -3,7 +3,6 @@ namespace JKingWeb\NewsSync;
const INSTALL = true;
require_once "../bootstrap.php";
$user = "john.doe@example.com";
$pass = "secret";
$_SERVER['PHP_AUTH_USER'] = $user;
@ -14,6 +13,10 @@ $conf->userAuthPreferHTTP = true;
$data = new RuntimeData($conf);
$data->db->schemaUpdate();
(new REST($data))->dispatch("GET", "/index.php/apps/news/api/", "");
exit;
$data->user->add($user, $pass);
$data->user->auth();

Loading…
Cancel
Save