Using the "extension" parameter

This commit is contained in:
Michael 2021-11-09 19:40:39 +00:00
parent b4fc1da045
commit a6db8df92d
5 changed files with 13 additions and 26 deletions

View File

@ -70,6 +70,6 @@ class Index extends BaseApi
];
}
self::exit('events', ['events' => $items]);
self::exit('events', ['events' => $items], $parameters['extension'] ?? null);
}
}

View File

@ -30,6 +30,6 @@ class Version extends BaseApi
{
public static function rawContent(array $parameters = [])
{
self::exit('version', ['version' => '0.9.7']);
self::exit('version', ['version' => '0.9.7'], $parameters['extension'] ?? null);
}
}

View File

@ -30,12 +30,12 @@ class Test extends BaseApi
{
public static function rawContent(array $parameters = [])
{
if (self::$format == 'xml') {
if (!empty($parameters['extension']) && ($parameters['extension'] == 'xml')) {
$ok = 'true';
} else {
$ok = 'ok';
}
self::exit('ok', ['ok' => $ok]);
self::exit('ok', ['ok' => $ok], $parameters['extension'] ?? null);
}
}

View File

@ -50,7 +50,7 @@ class Show extends BaseApi
$profile = self::formatProfile($profile, $profileFields);
$profiles = [];
if (self::$format == 'xml') {
if (!empty($parameters['extension']) && ($parameters['extension'] == 'xml')) {
$profiles['0:profile'] = $profile;
} else {
$profiles[] = $profile;
@ -66,7 +66,7 @@ class Show extends BaseApi
'profiles' => $profiles
];
self::exit('friendica_profiles', ['$result' => $result]);
self::exit('friendica_profiles', ['$result' => $result], $parameters['extension'] ?? null);
}
/**

View File

@ -43,11 +43,6 @@ class BaseApi extends BaseModule
const SCOPE_FOLLOW = 'follow';
const SCOPE_PUSH = 'push';
/**
* @var string json|xml|rss|atom
*/
protected static $format = 'json';
/**
* @var array
*/
@ -60,17 +55,6 @@ class BaseApi extends BaseModule
public static function init(array $parameters = [])
{
$arguments = DI::args();
if (substr($arguments->getCommand(), -4) === '.xml') {
self::$format = 'xml';
}
if (substr($arguments->getCommand(), -4) === '.rss') {
self::$format = 'rss';
}
if (substr($arguments->getCommand(), -4) === '.atom') {
self::$format = 'atom';
}
}
public static function delete(array $parameters = [])
@ -351,14 +335,17 @@ class BaseApi extends BaseModule
* Outputs formatted data according to the data type and then exits the execution.
*
* @param string $root_element
* @param array $data An array with a single element containing the returned result
* @param array $data An array with a single element containing the returned result
* @param string $format Output format (xml, json, rss, atom)
* @return false|string
*/
protected static function exit(string $root_element, array $data)
protected static function exit(string $root_element, array $data, string $format = null)
{
$return = self::formatData($root_element, self::$format, $data);
$format = $format ?? 'json';
switch (self::$format) {
$return = self::formatData($root_element, $format, $data);
switch ($format) {
case 'xml':
header('Content-Type: text/xml');
break;