]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Api/ApiResponse.php
Some more API functions moved
[friendica.git] / src / Module / Api / ApiResponse.php
index 768dacb5fb7294be8c82a5ad949dadf7a7998ee8..9521e2270a6e2938ac875fc70ee4313b717f51ce 100644 (file)
@@ -4,13 +4,10 @@ namespace Friendica\Module\Api;
 
 use Friendica\App\Arguments;
 use Friendica\Core\L10n;
-use Friendica\Core\Logger;
-use Friendica\Core\System;
-use Friendica\DI;
-use Friendica\Object\Api\Mastodon\Error;
 use Friendica\Util\Arrays;
 use Friendica\Util\HTTPInputData;
 use Friendica\Util\XML;
+use Psr\Log\LoggerInterface;
 
 /**
  * This class is used to format and return API responses
@@ -21,15 +18,42 @@ class ApiResponse
        protected $l10n;
        /** @var Arguments */
        protected $args;
+       /** @var LoggerInterface */
+       protected $logger;
 
        /**
-        * @param L10n      $l10n
-        * @param Arguments $args
+        * @param L10n            $l10n
+        * @param Arguments       $args
+        * @param LoggerInterface $logger
         */
-       public function __construct(L10n $l10n, Arguments $args)
+       public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger)
        {
-               $this->l10n = $l10n;
-               $this->args = $args;
+               $this->l10n   = $l10n;
+               $this->args   = $args;
+               $this->logger = $logger;
+       }
+
+       /**
+        * Sets header directly
+        * mainly used to override it for tests
+        *
+        * @param string $header
+        */
+       protected function setHeader(string $header)
+       {
+               header($header);
+       }
+
+       /**
+        * Prints output directly to the caller
+        * mainly used to override it for tests
+        *
+        * @param string $output
+        */
+       protected function printOutput(string $output)
+       {
+               echo $output;
+               exit;
        }
 
        /**
@@ -40,15 +64,17 @@ class ApiResponse
         *
         * @return string The XML data
         */
-       public static function createXML(array $data, string $root_element): string
+       public function createXML(array $data, string $root_element): string
        {
                $childname = key($data);
                $data2     = array_pop($data);
 
-               $namespaces = ['' => 'http://api.twitter.com',
-                       'statusnet'      => 'http://status.net/schema/api/1/',
-                       'friendica'      => 'http://friendi.ca/schema/api/1/',
-                       'georss'         => 'http://www.georss.org/georss'];
+               $namespaces = [
+                       ''          => 'http://api.twitter.com',
+                       'statusnet' => 'http://status.net/schema/api/1/',
+                       'friendica' => 'http://friendi.ca/schema/api/1/',
+                       'georss'    => 'http://www.georss.org/georss'
+               ];
 
                /// @todo Auto detection of needed namespaces
                if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) {
@@ -85,20 +111,17 @@ class ApiResponse
         *
         * @return array|string (string|array) XML data or JSON data
         */
-       public static function formatData(string $root_element, string $type, array $data)
+       public function formatData(string $root_element, string $type, array $data)
        {
                switch ($type) {
                        case 'atom':
                        case 'rss':
                        case 'xml':
-                               $ret = static::createXML($data, $root_element);
-                               break;
+                               return $this->createXML($data, $root_element);
                        case 'json':
                        default:
-                               $ret = $data;
-                               break;
+                               return $data;
                }
-               return $ret;
        }
 
        /**
@@ -133,17 +156,17 @@ class ApiResponse
         *
         * @return void
         */
-       public static function error(int $code, string $description, string $message, string $format = null)
+       public function error(int $code, string $description, string $message, string $format = null)
        {
                $error = [
                        'error'   => $message ?: $description,
                        'code'    => $code . ' ' . $description,
-                       'request' => DI::args()->getQueryString()
+                       'request' => $this->args->getQueryString()
                ];
 
-               header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
+               $this->setHeader(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
 
-               self::exit('status', ['status' => $error], $format);
+               $this->exit('status', ['status' => $error], $format);
        }
 
        /**
@@ -155,18 +178,18 @@ class ApiResponse
         *
         * @return void
         */
-       public static function exit(string $root_element, array $data, string $format = null)
+       public function exit(string $root_element, array $data, string $format = null)
        {
                $format = $format ?? 'json';
 
-               $return = static::formatData($root_element, $format, $data);
+               $return = $this->formatData($root_element, $format, $data);
 
                switch ($format) {
                        case 'xml':
-                               header('Content-Type: text/xml');
+                               $this->setHeader('Content-Type: text/xml');
                                break;
                        case 'json':
-                               header('Content-Type: application/json');
+                               $this->setHeader('Content-Type: application/json');
                                if (!empty($return)) {
                                        $json = json_encode(end($return));
                                        if (!empty($_GET['callback'])) {
@@ -176,17 +199,14 @@ class ApiResponse
                                }
                                break;
                        case 'rss':
-                               header('Content-Type: application/rss+xml');
-                               $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
+                               $this->setHeader('Content-Type: application/rss+xml');
                                break;
                        case 'atom':
-                               header('Content-Type: application/atom+xml');
-                               $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
+                               $this->setHeader('Content-Type: application/atom+xml');
                                break;
                }
 
-               echo $return;
-               exit;
+               $this->printOutput($return);
        }
 
        /**
@@ -197,19 +217,19 @@ class ApiResponse
         * @return void
         * @throws \Exception
         */
-       public static function unsupported(string $method = 'all')
+       public function unsupported(string $method = 'all')
        {
-               $path = DI::args()->getQueryString();
-               Logger::info('Unimplemented API call',
+               $path = $this->args->getQueryString();
+               $this->logger->info('Unimplemented API call',
                        [
                                'method'  => $method,
                                'path'    => $path,
                                'agent'   => $_SERVER['HTTP_USER_AGENT'] ?? '',
                                'request' => HTTPInputData::process()
                        ]);
-               $error             = DI::l10n()->t('API endpoint %s %s is not implemented', strtoupper($method), $path);
-               $error_description = DI::l10n()->t('The API endpoint is currently not implemented but might be in the future.');
-               $errorobj          = new Error($error, $error_description);
-               System::jsonError(501, $errorobj->toArray());
+               $error             = $this->l10n->t('API endpoint %s %s is not implemented', strtoupper($method), $path);
+               $error_description = $this->l10n->t('The API endpoint is currently not implemented but might be in the future.');
+
+               $this->exit('error', ['error' => ['error' => $error, 'error_description' => $error_description]]);
        }
 }