Transform ApiResponse::exit() into dynamic method

This commit is contained in:
Philipp Holzer 2021-11-12 20:03:27 +01:00
parent 4309f814e0
commit ea0f41ecf0
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
13 changed files with 27 additions and 30 deletions

View file

@ -145,7 +145,7 @@ class ApiResponse
header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
self::exit('status', ['status' => $error], $format);
DI::apiResponse()->exit('status', ['status' => $error], $format);
}
/**
@ -157,11 +157,11 @@ 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 = DI::apiResponse()->formatData($root_element, $format, $data);
$return = $this->formatData($root_element, $format, $data);
switch ($format) {
case 'xml':

View file

@ -21,6 +21,7 @@
namespace Friendica\Module\Api\Friendica;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
@ -57,7 +58,7 @@ class Activity extends BaseApi
} else {
$ok = 'ok';
}
ApiResponse::exit('ok', ['ok' => $ok], $parameters['extension'] ?? null);
DI::apiResponse()->exit('ok', ['ok' => $ok], $parameters['extension'] ?? null);
} else {
ApiResponse::error(500, 'Error adding activity', '', $parameters['extension'] ?? null);
}

View file

@ -22,7 +22,7 @@
namespace Friendica\Module\Api\Friendica\DirectMessages;
use Friendica\Database\DBA;
use Friendica\Module\Api\ApiResponse;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
@ -42,13 +42,13 @@ class Setseen extends BaseApi
// return error if id is zero
if (empty($request['id'])) {
$answer = ['result' => 'error', 'message' => 'message id not specified'];
ApiResponse::exit('direct_messages_setseen', ['$result' => $answer], $parameters['extension'] ?? null);
DI::apiResponse()->exit('direct_messages_setseen', ['$result' => $answer], $parameters['extension'] ?? null);
}
// error message if specified id is not in database
if (!DBA::exists('mail', ['id' => $request['id'], 'uid' => $uid])) {
$answer = ['result' => 'error', 'message' => 'message id not in database'];
ApiResponse::exit('direct_messages_setseen', ['$result' => $answer], $parameters['extension'] ?? null);
DI::apiResponse()->exit('direct_messages_setseen', ['$result' => $answer], $parameters['extension'] ?? null);
}
// update seen indicator
@ -58,6 +58,6 @@ class Setseen extends BaseApi
$answer = ['result' => 'error', 'message' => 'unknown error'];
}
ApiResponse::exit('direct_messages_setseen', ['$result' => $answer], $parameters['extension'] ?? null);
DI::apiResponse()->exit('direct_messages_setseen', ['$result' => $answer], $parameters['extension'] ?? null);
}
}

View file

@ -23,9 +23,8 @@ namespace Friendica\Module\Api\Friendica\Events;
use Friendica\Content\Text\BBCode;
use Friendica\Database\DBA;
use Friendica\Module\Api\ApiResponse;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
/**
* api/friendica/events
@ -71,6 +70,6 @@ class Index extends BaseApi
];
}
ApiResponse::exit('events', ['events' => $items], $parameters['extension'] ?? null);
DI::apiResponse()->exit('events', ['events' => $items], $parameters['extension'] ?? null);
}
}

View file

@ -23,7 +23,6 @@ namespace Friendica\Module\Api\Friendica;
use Friendica\Collection\Api\Notifications as ApiNotifications;
use Friendica\DI;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
use Friendica\Object\Api\Friendica\Notification as ApiNotification;
@ -57,6 +56,6 @@ class Notification extends BaseApi
$result = false;
}
ApiResponse::exit('notes', ['note' => $result], $parameters['extension'] ?? null);
DI::apiResponse()->exit('notes', ['note' => $result], $parameters['extension'] ?? null);
}
}

View file

@ -21,9 +21,9 @@
namespace Friendica\Module\Api\Friendica\Photo;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\InternalServerErrorException;
@ -64,7 +64,7 @@ class Delete extends BaseApi
Item::deleteForUser($condition, $uid);
$result = ['result' => 'deleted', 'message' => 'photo with id `' . $request['photo_id'] . '` has been deleted from server.'];
ApiResponse::exit('photo_delete', ['$result' => $result], $parameters['extension'] ?? null);
DI::apiResponse()->exit('photo_delete', ['$result' => $result], $parameters['extension'] ?? null);
} else {
throw new InternalServerErrorException("unknown error on deleting photo from database table");
}

View file

@ -22,9 +22,9 @@
namespace Friendica\Module\Api\Friendica\Photoalbum;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\InternalServerErrorException;
@ -67,7 +67,7 @@ class Delete extends BaseApi
// return success of deletion or error message
if ($result) {
$answer = ['result' => 'deleted', 'message' => 'album `' . $request['album'] . '` with all containing photos has been deleted.'];
ApiResponse::exit('photoalbum_delete', ['$result' => $answer], $parameters['extension'] ?? null);
DI::apiResponse()->exit('photoalbum_delete', ['$result' => $answer], $parameters['extension'] ?? null);
} else {
throw new InternalServerErrorException("unknown error - deleting from database failed");
}

View file

@ -21,8 +21,8 @@
namespace Friendica\Module\Api\Friendica\Photoalbum;
use Friendica\DI;
use Friendica\Model\Photo;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\InternalServerErrorException;
@ -59,7 +59,7 @@ class Update extends BaseApi
// return success of updating or error message
if ($result) {
$answer = ['result' => 'updated', 'message' => 'album `' . $request['album'] . '` with all containing photos has been renamed to `' . $request['album_new'] . '`.'];
ApiResponse::exit('photoalbum_update', ['$result' => $answer], $parameters['extension'] ?? null);
DI::apiResponse()->exit('photoalbum_update', ['$result' => $answer], $parameters['extension'] ?? null);
} else {
throw new InternalServerErrorException("unknown error - updating in database failed");
}

View file

@ -21,7 +21,6 @@
namespace Friendica\Module\Api\Friendica\Profile;
use Friendica\Module\Api\ApiResponse;
use Friendica\Profile\ProfileField\Collection\ProfileFields;
use Friendica\Content\Text\BBCode;
use Friendica\DI;
@ -29,7 +28,6 @@ use Friendica\Model\Contact;
use Friendica\Model\Profile;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
use Friendica\Security\PermissionSet\Repository\PermissionSet;
/**
* API endpoint: /api/friendica/profile/show
@ -67,7 +65,7 @@ class Show extends BaseApi
'profiles' => $profiles
];
ApiResponse::exit('friendica_profiles', ['$result' => $result], $parameters['extension'] ?? null);
DI::apiResponse()->exit('friendica_profiles', ['$result' => $result], $parameters['extension'] ?? null);
}
/**

View file

@ -21,8 +21,8 @@
namespace Friendica\Module\Api\GNUSocial\GNUSocial;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
use Friendica\DI;
/**
* API endpoint: /api/gnusocial/version, /api/statusnet/version
@ -31,6 +31,6 @@ class Version extends BaseApi
{
public static function rawContent(array $parameters = [])
{
ApiResponse::exit('version', ['version' => '0.9.7'], $parameters['extension'] ?? null);
DI::apiResponse()->exit('version', ['version' => '0.9.7'], $parameters['extension'] ?? null);
}
}

View file

@ -21,8 +21,8 @@
namespace Friendica\Module\Api\GNUSocial\Help;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
use Friendica\DI;
/**
* API endpoint: /api/help/test
@ -37,6 +37,6 @@ class Test extends BaseApi
$ok = 'ok';
}
ApiResponse::exit('ok', ['ok' => $ok], $parameters['extension'] ?? null);
DI::apiResponse()->exit('ok', ['ok' => $ok], $parameters['extension'] ?? null);
}
}

View file

@ -21,8 +21,8 @@
namespace Friendica\Module\Api\Twitter\Account;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi;
use Friendica\DI;
use Friendica\Util\DateTimeFormat;
/**
@ -52,6 +52,6 @@ class RateLimitStatus extends BaseApi
];
}
ApiResponse::exit('hash', ['hash' => $hash], $parameters['extension'] ?? null);
DI::apiResponse()->exit('hash', ['hash' => $hash], $parameters['extension'] ?? null);
}
}

View file

@ -22,7 +22,7 @@
namespace Friendica\Module\Api\Twitter;
use Friendica\Database\DBA;
use Friendica\Module\Api\ApiResponse;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
@ -45,6 +45,6 @@ class SavedSearches extends BaseApi
DBA::close($terms);
ApiResponse::exit('terms', ['terms' => $result], $parameters['extension'] ?? null);
DI::apiResponse()->exit('terms', ['terms' => $result], $parameters['extension'] ?? null);
}
}