J. King
7 years ago
8 changed files with 98 additions and 48 deletions
@ -1,8 +1,53 @@ |
|||||
<?php |
<?php |
||||
declare(strict_types=1); |
declare(strict_types=1); |
||||
namespace JKingWeb\NewsSync; |
namespace JKingWeb\NewsSync; |
||||
use Respect\Rest\Router; |
|
||||
|
|
||||
class REST { |
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); |
||||
|
} |
||||
} |
} |
@ -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; |
||||
|
} |
@ -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 { |
||||
|
} |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\NewsSync\REST; |
||||
|
|
||||
|
class Response { |
||||
|
// stub |
||||
|
} |
Loading…
Reference in new issue