1
0
Fork 0
mirror of synced 2024-05-17 19:22:34 +12:00
appwrite/src/Appwrite/Utopia/Response.php

497 lines
17 KiB
PHP
Raw Normal View History

2020-05-16 23:28:26 +12:00
<?php
2020-06-23 00:17:14 +12:00
namespace Appwrite\Utopia;
2020-05-16 23:28:26 +12:00
use Exception;
use Utopia\Swoole\Response as SwooleResponse;
use Swoole\Http\Response as SwooleHTTPResponse;
2021-07-26 02:47:18 +12:00
use Utopia\Database\Document;
use Appwrite\Utopia\Response\Filter;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model;
2020-11-12 11:01:31 +13:00
use Appwrite\Utopia\Response\Model\None;
use Appwrite\Utopia\Response\Model\Any;
use Appwrite\Utopia\Response\Model\Attribute;
use Appwrite\Utopia\Response\Model\AttributeList;
use Appwrite\Utopia\Response\Model\AttributeString;
use Appwrite\Utopia\Response\Model\AttributeInteger;
use Appwrite\Utopia\Response\Model\AttributeFloat;
use Appwrite\Utopia\Response\Model\AttributeBoolean;
use Appwrite\Utopia\Response\Model\AttributeEmail;
use Appwrite\Utopia\Response\Model\AttributeEnum;
use Appwrite\Utopia\Response\Model\AttributeIP;
use Appwrite\Utopia\Response\Model\AttributeURL;
use Appwrite\Utopia\Response\Model\BaseList;
use Appwrite\Utopia\Response\Model\Collection;
use Appwrite\Utopia\Response\Model\Continent;
use Appwrite\Utopia\Response\Model\Country;
use Appwrite\Utopia\Response\Model\Currency;
use Appwrite\Utopia\Response\Model\Document as ModelDocument;
2022-01-18 05:25:20 +13:00
use Appwrite\Utopia\Response\Model\DocumentList;
use Appwrite\Utopia\Response\Model\Domain;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model\Error;
use Appwrite\Utopia\Response\Model\ErrorDev;
use Appwrite\Utopia\Response\Model\Execution;
use Appwrite\Utopia\Response\Model\File;
use Appwrite\Utopia\Response\Model\Func;
use Appwrite\Utopia\Response\Model\Index;
2020-12-29 05:41:38 +13:00
use Appwrite\Utopia\Response\Model\JWT;
use Appwrite\Utopia\Response\Model\Key;
use Appwrite\Utopia\Response\Model\Language;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model\User;
2020-06-24 19:35:59 +12:00
use Appwrite\Utopia\Response\Model\Session;
2020-06-24 17:14:42 +12:00
use Appwrite\Utopia\Response\Model\Team;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model\Locale;
use Appwrite\Utopia\Response\Model\Log;
use Appwrite\Utopia\Response\Model\Membership;
use Appwrite\Utopia\Response\Model\Metric;
2020-11-08 11:14:36 +13:00
use Appwrite\Utopia\Response\Model\Permissions;
use Appwrite\Utopia\Response\Model\Phone;
use Appwrite\Utopia\Response\Model\Platform;
use Appwrite\Utopia\Response\Model\Project;
use Appwrite\Utopia\Response\Model\Rule;
use Appwrite\Utopia\Response\Model\Tag;
2020-11-13 00:54:16 +13:00
use Appwrite\Utopia\Response\Model\Token;
use Appwrite\Utopia\Response\Model\Webhook;
2021-04-22 01:37:51 +12:00
use Appwrite\Utopia\Response\Model\Preferences;
2021-12-17 01:19:04 +13:00
use Appwrite\Utopia\Response\Model\HealthAntivirus;
use Appwrite\Utopia\Response\Model\HealthQueue;
use Appwrite\Utopia\Response\Model\HealthStatus;
use Appwrite\Utopia\Response\Model\HealthTime;
use Appwrite\Utopia\Response\Model\HealthVersion;
2021-03-05 19:40:29 +13:00
use Appwrite\Utopia\Response\Model\Mock; // Keep last
2021-12-10 02:02:12 +13:00
use Appwrite\Utopia\Response\Model\Runtime;
use Appwrite\Utopia\Response\Model\UsageBuckets;
use Appwrite\Utopia\Response\Model\UsageCollection;
use Appwrite\Utopia\Response\Model\UsageDatabase;
use Appwrite\Utopia\Response\Model\UsageFunctions;
use Appwrite\Utopia\Response\Model\UsageProject;
use Appwrite\Utopia\Response\Model\UsageStorage;
use Appwrite\Utopia\Response\Model\UsageUsers;
2020-05-16 23:28:26 +12:00
2020-11-01 00:06:09 +13:00
/**
* @method Response setStatusCode(int $code = 200)
2020-11-01 00:06:09 +13:00
*/
class Response extends SwooleResponse
2020-05-16 23:28:26 +12:00
{
2020-06-24 06:53:24 +12:00
// General
2020-11-12 11:01:31 +13:00
const MODEL_NONE = 'none';
const MODEL_ANY = 'any';
const MODEL_LOG = 'log';
const MODEL_LOG_LIST = 'logList';
2020-06-24 03:01:20 +12:00
const MODEL_ERROR = 'error';
2021-08-26 23:00:03 +12:00
const MODEL_METRIC = 'metric';
const MODEL_METRIC_LIST = 'metricList';
2020-06-24 03:01:20 +12:00
const MODEL_ERROR_DEV = 'errorDev';
2020-06-24 17:14:42 +12:00
const MODEL_BASE_LIST = 'baseList';
const MODEL_USAGE_DATABASE = 'usageDatabase';
const MODEL_USAGE_COLLECTION = 'usageCollection';
const MODEL_USAGE_USERS = 'usageUsers';
const MODEL_USAGE_BUCKETS = 'usageBuckets';
const MODEL_USAGE_STORAGE = 'usageStorage';
const MODEL_USAGE_FUNCTIONS = 'usageFunctions';
const MODEL_USAGE_PROJECT = 'usageProject';
2020-06-24 06:53:24 +12:00
// Database
const MODEL_COLLECTION = 'collection';
const MODEL_COLLECTION_LIST = 'collectionList';
const MODEL_INDEX = 'index';
const MODEL_INDEX_LIST = 'indexList';
const MODEL_DOCUMENT = 'document';
const MODEL_DOCUMENT_LIST = 'documentList';
// Database Attributes
const MODEL_ATTRIBUTE = 'attribute';
const MODEL_ATTRIBUTE_LIST = 'attributeList';
const MODEL_ATTRIBUTE_STRING = 'attributeString';
const MODEL_ATTRIBUTE_INTEGER = 'attributeInteger';
const MODEL_ATTRIBUTE_FLOAT = 'attributeFloat';
const MODEL_ATTRIBUTE_BOOLEAN = 'attributeBoolean';
const MODEL_ATTRIBUTE_EMAIL = 'attributeEmail';
const MODEL_ATTRIBUTE_ENUM = 'attributeEnum';
const MODEL_ATTRIBUTE_IP = 'attributeIp';
const MODEL_ATTRIBUTE_URL= 'attributeUrl';
2020-06-24 06:53:24 +12:00
// Users
2020-06-24 03:01:20 +12:00
const MODEL_USER = 'user';
const MODEL_USER_LIST = 'userList';
2020-06-24 06:53:24 +12:00
const MODEL_SESSION = 'session';
const MODEL_SESSION_LIST = 'sessionList';
2020-12-29 05:41:38 +13:00
const MODEL_TOKEN = 'token';
const MODEL_JWT = 'jwt';
2021-04-22 01:37:51 +12:00
const MODEL_PREFERENCES = 'preferences';
2020-06-24 06:53:24 +12:00
// Storage
const MODEL_FILE = 'file';
const MODEL_FILE_LIST = 'fileList';
const MODEL_BUCKET = 'bucket'; // - Missing
2020-06-24 06:53:24 +12:00
// Locale
const MODEL_LOCALE = 'locale';
const MODEL_COUNTRY = 'country';
const MODEL_COUNTRY_LIST = 'countryList';
const MODEL_CONTINENT = 'continent';
const MODEL_CONTINENT_LIST = 'continentList';
const MODEL_CURRENCY = 'currency';
const MODEL_CURRENCY_LIST = 'currencyList';
const MODEL_LANGUAGE = 'language';
const MODEL_LANGUAGE_LIST = 'languageList';
const MODEL_PHONE = 'phone';
const MODEL_PHONE_LIST = 'phoneList';
2020-06-24 06:53:24 +12:00
// Teams
const MODEL_TEAM = 'team';
2020-06-24 17:14:42 +12:00
const MODEL_TEAM_LIST = 'teamList';
2020-06-26 08:26:02 +12:00
const MODEL_MEMBERSHIP = 'membership';
const MODEL_MEMBERSHIP_LIST = 'membershipList';
2020-06-05 21:53:06 +12:00
// Functions
const MODEL_FUNCTION = 'function';
const MODEL_FUNCTION_LIST = 'functionList';
2021-12-10 02:02:12 +13:00
const MODEL_RUNTIME = 'runtime';
const MODEL_RUNTIME_LIST = 'runtimeList';
const MODEL_TAG = 'tag';
const MODEL_TAG_LIST = 'tagList';
const MODEL_EXECUTION = 'execution';
const MODEL_EXECUTION_LIST = 'executionList';
2021-10-22 23:32:38 +13:00
const MODEL_FUNC_PERMISSIONS = 'funcPermissions';
// Project
const MODEL_PROJECT = 'project';
const MODEL_PROJECT_LIST = 'projectList';
const MODEL_WEBHOOK = 'webhook';
const MODEL_WEBHOOK_LIST = 'webhookList';
const MODEL_KEY = 'key';
const MODEL_KEY_LIST = 'keyList';
const MODEL_PLATFORM = 'platform';
const MODEL_PLATFORM_LIST = 'platformList';
const MODEL_DOMAIN = 'domain';
const MODEL_DOMAIN_LIST = 'domainList';
2021-12-15 03:17:55 +13:00
// Health
const MODEL_HEALTH_STATUS = 'healthStatus';
const MODEL_HEALTH_VERSION = 'healthVersion';
const MODEL_HEALTH_QUEUE = 'healthQueue';
const MODEL_HEALTH_TIME = 'healthTime';
const MODEL_HEALTH_ANTIVIRUS = 'healthAntivirus';
2021-03-05 19:40:29 +13:00
2021-08-04 01:34:29 +12:00
// Deprecated
const MODEL_PERMISSIONS = 'permissions';
const MODEL_RULE = 'rule';
2021-08-13 20:40:01 +12:00
const MODEL_TASK = 'task';
2021-08-04 01:34:29 +12:00
2021-03-05 19:40:29 +13:00
// Tests (keep last)
const MODEL_MOCK = 'mock';
/**
* @var Filter
*/
private static $filter = null;
/**
* @var array
*/
protected $payload = [];
2020-06-27 00:27:58 +12:00
/**
* Response constructor.
*
* @param float $time
2020-06-27 00:27:58 +12:00
*/
public function __construct(SwooleHTTPResponse $response)
2020-06-05 21:53:06 +12:00
{
$this
// General
2020-11-12 11:01:31 +13:00
->setModel(new None())
->setModel(new Any())
2020-06-24 03:01:20 +12:00
->setModel(new Error())
->setModel(new ErrorDev())
// Lists
->setModel(new BaseList('Documents List', self::MODEL_DOCUMENT_LIST, 'documents', self::MODEL_DOCUMENT))
->setModel(new BaseList('Collections List', self::MODEL_COLLECTION_LIST, 'collections', self::MODEL_COLLECTION))
->setModel(new BaseList('Indexes List', self::MODEL_INDEX_LIST, 'indexes', self::MODEL_INDEX))
->setModel(new BaseList('Users List', self::MODEL_USER_LIST, 'users', self::MODEL_USER))
->setModel(new BaseList('Sessions List', self::MODEL_SESSION_LIST, 'sessions', self::MODEL_SESSION))
2021-11-17 04:06:51 +13:00
->setModel(new BaseList('Logs List', self::MODEL_LOG_LIST, 'logs', self::MODEL_LOG))
->setModel(new BaseList('Files List', self::MODEL_FILE_LIST, 'files', self::MODEL_FILE))
->setModel(new BaseList('Teams List', self::MODEL_TEAM_LIST, 'teams', self::MODEL_TEAM))
->setModel(new BaseList('Memberships List', self::MODEL_MEMBERSHIP_LIST, 'memberships', self::MODEL_MEMBERSHIP))
->setModel(new BaseList('Functions List', self::MODEL_FUNCTION_LIST, 'functions', self::MODEL_FUNCTION))
2021-12-10 02:02:12 +13:00
->setModel(new BaseList('Runtimes List', self::MODEL_RUNTIME_LIST, 'runtimes', self::MODEL_RUNTIME))
->setModel(new BaseList('Tags List', self::MODEL_TAG_LIST, 'tags', self::MODEL_TAG))
->setModel(new BaseList('Executions List', self::MODEL_EXECUTION_LIST, 'executions', self::MODEL_EXECUTION))
->setModel(new BaseList('Projects List', self::MODEL_PROJECT_LIST, 'projects', self::MODEL_PROJECT, true, false))
->setModel(new BaseList('Webhooks List', self::MODEL_WEBHOOK_LIST, 'webhooks', self::MODEL_WEBHOOK, true, false))
->setModel(new BaseList('API Keys List', self::MODEL_KEY_LIST, 'keys', self::MODEL_KEY, true, false))
->setModel(new BaseList('Platforms List', self::MODEL_PLATFORM_LIST, 'platforms', self::MODEL_PLATFORM, true, false))
->setModel(new BaseList('Domains List', self::MODEL_DOMAIN_LIST, 'domains', self::MODEL_DOMAIN, true, false))
->setModel(new BaseList('Countries List', self::MODEL_COUNTRY_LIST, 'countries', self::MODEL_COUNTRY))
->setModel(new BaseList('Continents List', self::MODEL_CONTINENT_LIST, 'continents', self::MODEL_CONTINENT))
->setModel(new BaseList('Languages List', self::MODEL_LANGUAGE_LIST, 'languages', self::MODEL_LANGUAGE))
->setModel(new BaseList('Currencies List', self::MODEL_CURRENCY_LIST, 'currencies', self::MODEL_CURRENCY))
->setModel(new BaseList('Phones List', self::MODEL_PHONE_LIST, 'phones', self::MODEL_PHONE))
->setModel(new BaseList('Metric List', self::MODEL_METRIC_LIST, 'metrics', self::MODEL_METRIC, true, false))
// Entities
->setModel(new Collection())
2021-06-12 06:07:05 +12:00
->setModel(new Attribute())
->setModel(new AttributeList())
->setModel(new AttributeString())
->setModel(new AttributeInteger())
->setModel(new AttributeFloat())
->setModel(new AttributeBoolean())
->setModel(new AttributeEmail())
->setModel(new AttributeEnum())
->setModel(new AttributeIP())
->setModel(new AttributeURL())
2021-06-12 06:07:05 +12:00
->setModel(new Index())
->setModel(new ModelDocument())
->setModel(new Log())
2020-06-24 03:01:20 +12:00
->setModel(new User())
2021-04-22 01:37:51 +12:00
->setModel(new Preferences())
2020-06-24 19:35:59 +12:00
->setModel(new Session())
2020-11-13 00:54:16 +13:00
->setModel(new Token())
2020-12-29 05:41:38 +13:00
->setModel(new JWT())
2020-06-24 03:01:20 +12:00
->setModel(new Locale())
->setModel(new File())
2020-06-24 17:14:42 +12:00
->setModel(new Team())
->setModel(new Membership())
->setModel(new Func())
2021-12-10 02:02:12 +13:00
->setModel(new Runtime())
->setModel(new Tag())
->setModel(new Execution())
->setModel(new Project())
->setModel(new Webhook())
->setModel(new Key())
->setModel(new Domain())
->setModel(new Platform())
->setModel(new Country())
->setModel(new Continent())
->setModel(new Language())
->setModel(new Currency())
->setModel(new Phone())
2021-12-17 01:19:04 +13:00
->setModel(new HealthAntivirus())
->setModel(new HealthQueue())
->setModel(new HealthStatus())
->setModel(new HealthTime())
->setModel(new HealthVersion())
->setModel(new Metric())
->setModel(new UsageDatabase())
->setModel(new UsageCollection())
->setModel(new UsageUsers())
->setModel(new UsageStorage())
->setModel(new UsageBuckets())
->setModel(new UsageFunctions())
->setModel(new UsageProject())
// Verification
// Recovery
2021-03-05 19:40:29 +13:00
// Tests (keep last)
->setModel(new Mock())
2020-06-05 21:53:06 +12:00
;
2020-07-05 06:21:42 +12:00
parent::__construct($response);
2020-06-05 21:53:06 +12:00
}
2020-05-16 23:28:26 +12:00
/**
* HTTP content types
*/
const CONTENT_TYPE_YAML = 'application/x-yaml';
2020-06-05 21:53:06 +12:00
/**
* List of defined output objects
*/
2020-06-24 03:01:20 +12:00
protected $models = [];
2020-06-05 21:53:06 +12:00
/**
2020-06-24 03:01:20 +12:00
* Set Model Object
*
2020-06-05 21:53:06 +12:00
* @return self
*/
2021-05-20 02:26:06 +12:00
public function setModel(Model $instance)
2020-05-16 23:28:26 +12:00
{
2020-06-24 03:01:20 +12:00
$this->models[$instance->getType()] = $instance;
2020-06-05 21:53:06 +12:00
return $this;
}
/**
2020-06-24 03:01:20 +12:00
* Get Model Object
*
2020-06-24 03:01:20 +12:00
* @return Model
2020-06-05 21:53:06 +12:00
*/
2020-06-24 03:01:20 +12:00
public function getModel(string $key): Model
2020-06-05 21:53:06 +12:00
{
2020-10-28 08:44:15 +13:00
if (!isset($this->models[$key])) {
2020-06-24 03:01:20 +12:00
throw new Exception('Undefined model: '.$key);
2020-06-05 21:53:06 +12:00
}
2020-06-24 03:01:20 +12:00
return $this->models[$key];
2020-06-05 21:53:06 +12:00
}
2020-11-08 11:14:36 +13:00
/**
* Get Models List
*
2020-11-08 11:14:36 +13:00
* @return Model[]
*/
public function getModels(): array
{
return $this->models;
}
2020-06-05 21:53:06 +12:00
/**
* Validate response objects and outputs
* the response according to given format type
*
2020-10-31 21:42:41 +13:00
* @param Document $document
* @param string $model
*
2020-10-31 21:42:41 +13:00
* return void
2020-06-05 21:53:06 +12:00
*/
2020-11-01 00:06:09 +13:00
public function dynamic(Document $document, string $model): void
2020-06-05 21:53:06 +12:00
{
2021-01-04 07:12:11 +13:00
$output = $this->output($document, $model);
// If filter is set, parse the output
if (self::hasFilter()) {
$output = self::getFilter()->parse($output, $model);
2021-01-04 07:12:11 +13:00
}
2021-10-06 04:39:39 +13:00
$this->json(!empty($output) ? $output : new \stdClass());
2020-10-31 21:42:41 +13:00
}
2020-06-24 18:05:43 +12:00
/**
* Generate valid response object from document data
*
2020-10-31 21:42:41 +13:00
* @param Document $document
* @param string $model
*
2020-10-31 21:42:41 +13:00
* return array
2020-06-24 18:05:43 +12:00
*/
public function output(Document $document, string $model): array
2020-06-24 18:05:43 +12:00
{
$data = $document;
2020-06-24 03:01:20 +12:00
$model = $this->getModel($model);
2020-06-05 21:53:06 +12:00
$output = [];
2022-01-18 05:25:20 +13:00
$document = $model->filter($document);
2020-10-30 11:44:01 +13:00
if ($model->isAny()) {
2020-12-01 10:41:58 +13:00
$this->payload = $document->getArrayCopy();
return $this->payload;
}
2020-10-30 11:44:01 +13:00
foreach ($model->getRules() as $key => $rule) {
if (!$document->isSet($key) && $rule['require']) { // do not set attribute in response if not required
2020-10-30 11:44:01 +13:00
if (!is_null($rule['default'])) {
2020-06-24 18:05:43 +12:00
$document->setAttribute($key, $rule['default']);
2020-10-30 11:44:01 +13:00
} else {
throw new Exception('Model '.$model->getName().' is missing response key: '.$key);
2020-06-23 02:33:37 +12:00
}
2020-06-05 21:53:06 +12:00
}
2020-10-30 11:44:01 +13:00
if ($rule['array']) {
if (!is_array($data[$key])) {
throw new Exception($key.' must be an array of type '.$rule['type']);
2020-06-24 18:05:43 +12:00
}
foreach ($data[$key] as &$item) {
2020-10-30 11:44:01 +13:00
if ($item instanceof Document) {
2021-09-14 20:26:16 +12:00
if (\is_array($rule['type'])) {
foreach ($rule['type'] as $type) {
$condition = false;
foreach ($this->getModel($type)->conditions as $attribute => $val) {
$condition = $item->getAttribute($attribute) === $val;
if(!$condition) {
break;
}
}
if ($condition) {
$ruleType = $type;
break;
}
}
} else {
$ruleType = $rule['type'];
}
if (!array_key_exists($ruleType, $this->models)) {
throw new Exception('Missing model for rule: '. $ruleType);
}
$item = $this->output($item, $ruleType);
2020-06-24 18:05:43 +12:00
}
}
2020-06-24 03:01:20 +12:00
}
2021-09-14 20:26:16 +12:00
2020-06-05 21:53:06 +12:00
$output[$key] = $data[$key];
}
$this->payload = $output;
2020-12-01 10:41:58 +13:00
return $this->payload;
2020-05-16 23:28:26 +12:00
}
/**
* YAML
*
* This helper is for sending YAML HTTP response.
* It sets relevant content type header ('application/x-yaml') and convert a PHP array ($data) to valid YAML using native yaml_parse
*
* @see https://en.wikipedia.org/wiki/YAML
*
* @param array $data
*
* @return void
2020-05-16 23:28:26 +12:00
*/
public function yaml(array $data): void
2020-05-16 23:28:26 +12:00
{
2020-10-28 08:44:15 +13:00
if (!extension_loaded('yaml')) {
2020-05-16 23:28:26 +12:00
throw new Exception('Missing yaml extension. Learn more at: https://www.php.net/manual/en/book.yaml.php');
}
$this
->setContentType(Response::CONTENT_TYPE_YAML)
->send(yaml_emit($data, YAML_UTF8_ENCODING))
;
}
/**
* @return array
*/
public function getPayload():array
{
return $this->payload;
}
/**
* Function to set a response filter
*
* @param $filter the response filter to set
*
* @return void
*/
public static function setFilter(?Filter $filter)
{
self::$filter = $filter;
}
/**
* Return the currently set filter
*
* @return Filter
*/
public static function getFilter(): ?Filter
{
return self::$filter;
}
/**
* Check if a filter has been set
*
* @return bool
*/
public static function hasFilter(): bool
{
return self::$filter != null;
}
2020-05-16 23:28:26 +12:00
}