diff --git a/public_html/media/css/static-api.css b/public_html/media/css/static-api.css new file mode 100644 index 00000000..f3ac8d7b --- /dev/null +++ b/public_html/media/css/static-api.css @@ -0,0 +1,5 @@ +pre { + background: ghostwhite; + padding: 0.5em; + border-left: 0.2em solid silver; +} diff --git a/src/Api/Api.php b/src/Api/Api.php index 05fe4fd2..03419e5c 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -1,6 +1,11 @@ subJobs []= $subJob; + } + + public function getSubJobs() + { + return $this->subJobs; + } + public function getRequiredPrivileges() { return false; diff --git a/src/Api/Jobs/PostJobs/AddPostJob.php b/src/Api/Jobs/PostJobs/AddPostJob.php index 70013921..017e1371 100644 --- a/src/Api/Jobs/PostJobs/AddPostJob.php +++ b/src/Api/Jobs/PostJobs/AddPostJob.php @@ -1,6 +1,11 @@ addSubJob(new EditPostJob()); + } + public function execute() { $post = PostModel::spawn(); @@ -20,7 +25,7 @@ class AddPostJob extends AbstractJob Logger::bufferChanges(); try { - $job = new EditPostJob(); + $job = $this->getSubJobs()[0]; $job->setContext(AbstractJob::CONTEXT_BATCH_ADD); Api::run($job, $arguments); } diff --git a/src/Api/Jobs/PostJobs/EditPostJob.php b/src/Api/Jobs/PostJobs/EditPostJob.php index 3c24662d..bf68e2ae 100644 --- a/src/Api/Jobs/PostJobs/EditPostJob.php +++ b/src/Api/Jobs/PostJobs/EditPostJob.php @@ -6,6 +6,12 @@ class EditPostJob extends AbstractJob public function __construct() { $this->postRetriever = new PostRetriever($this); + $this->addSubJob(new EditPostSafetyJob()); + $this->addSubJob(new EditPostTagsJob()); + $this->addSubJob(new EditPostSourceJob()); + $this->addSubJob(new EditPostRelationsJob()); + $this->addSubJob(new EditPostContentJob()); + $this->addSubJob(new EditPostThumbJob()); } public function execute() @@ -14,17 +20,7 @@ class EditPostJob extends AbstractJob Logger::bufferChanges(); - $subJobs = - [ - new EditPostSafetyJob(), - new EditPostTagsJob(), - new EditPostSourceJob(), - new EditPostRelationsJob(), - new EditPostContentJob(), - new EditPostThumbJob(), - ]; - - foreach ($subJobs as $subJob) + foreach ($this->getSubJobs() as $subJob) { $subJob->setContext($this->getContext() == self::CONTEXT_BATCH_ADD ? self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/UserJobs/AddUserJob.php b/src/Api/Jobs/UserJobs/AddUserJob.php index bc36172c..b4029563 100644 --- a/src/Api/Jobs/UserJobs/AddUserJob.php +++ b/src/Api/Jobs/UserJobs/AddUserJob.php @@ -1,6 +1,11 @@ addSubJob(new EditUserJob()); + } + public function execute() { $firstUser = UserModel::getCount() == 0; @@ -25,7 +30,7 @@ class AddUserJob extends AbstractJob Logger::bufferChanges(); try { - $job = new EditUserJob(); + $job = $this->getSubJobs()[0]; $job->setContext(self::CONTEXT_BATCH_ADD); Api::run($job, $arguments); } diff --git a/src/Api/Jobs/UserJobs/EditUserJob.php b/src/Api/Jobs/UserJobs/EditUserJob.php index ed494e49..cd2dd555 100644 --- a/src/Api/Jobs/UserJobs/EditUserJob.php +++ b/src/Api/Jobs/UserJobs/EditUserJob.php @@ -2,24 +2,20 @@ class EditUserJob extends AbstractJob { protected $userRetriever; - protected $subJobs; public function __construct() { $this->userRetriever = new UserRetriever($this); - $this->subJobs = - [ - new EditUserAccessRankJob(), - new EditUserNameJob(), - new EditUserPasswordJob(), - new EditUserEmailJob(), - ]; + $this->addSubJob(new EditUserAccessRankJob()); + $this->addSubJob(new EditUserNameJob()); + $this->addSubJob(new EditUserPasswordJob()); + $this->addSubJob(new EditUserEmailJob()); } public function canEditAnything($user) { $this->privileges = []; - foreach ($this->subJobs as $subJob) + foreach ($this->getSubJobs() as $subJob) { try { @@ -40,7 +36,7 @@ class EditUserJob extends AbstractJob Logger::bufferChanges(); - foreach ($this->subJobs as $subJob) + foreach ($this->getSubJobs() as $subJob) { $subJob->setContext($this->getContext() == self::CONTEXT_BATCH_ADD ? self::CONTEXT_BATCH_ADD diff --git a/src/Controllers/StaticPagesController.php b/src/Controllers/StaticPagesController.php index c50e6f92..be3b91b9 100644 --- a/src/Controllers/StaticPagesController.php +++ b/src/Controllers/StaticPagesController.php @@ -38,6 +38,11 @@ class StaticPagesController extends AbstractController $this->renderView('static-help'); } + public function apiDocsView() + { + $this->renderView('static-api'); + } + public function fatalErrorView($code = null) { throw new SimpleException('Error ' . $code . ' while retrieving ' . $_SERVER['REQUEST_URI']); diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index 879a3305..3e4c671a 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -42,9 +42,10 @@ $this->assets->addScript('core.js');
All interaction with API proceeds through just one URL: = Api::getUrl() ?>
.
One request to API means executing one so-called „job”. Most of things that can be done through web +interface, can be also done through the API.
+ +To specify job to be executed, send name
parameter. Optional job arguments can be specified with
+args
parameter. Example using curl
:
curl \
+ --data 'name=get-post' \
+ '&args[post-id]=5408' \
+ = Api::getUrl() ?>
+
+In order to make authenticated request, you need to supply request with your credentials. This can be done by
+passing auth[user]
and auth[name]
parameters:
curl \
+ --data 'auth[user]=example' \
+ '&auth[pass]=secret' \
+ '&name=get-post' \
+ '&args[post-id]=5408' \
+ = Api::getUrl() ?>
+
+In order to send files to API, you have to send multipart/form-data
request. With curl
+this can be done using -F
option to encode each parameter and prepending @
character to
+chosen file name:
curl \
+ -F 'auth[user]=example' \
+ -F 'auth[pass]=secret' \
+ -F 'name=add-post' \
+ -F 'args[new-tag-names][0]=test' \
+ -F 'args[new-tag-names][1]=test2' \
+ -F 'args[new-tag-names][2]=test3' \
+ -F 'args[new-post-content]=@./custom.png' \
+ = Api::getUrl() ?>
+
+Output is always represented in JSON array. Files are stored in JSON as well using gzip compression and base64 +encoding.
+ +When errors occur all errors are logged to message
field and changes done with request is rolled
+back.
Each job checks for some privileges depending on context it is run with.
+ +Privilege | +Minimum access rank | +
---|---|
= $privilege ?> | += $minAccessRank ?> | +
Required arguments: = $showArgs($job->getRequiredArguments()) ?>
+Requires e-mail confirmation: = $job->isConfirmedEmailRequired() ? 'yes' : 'no' ?>
+Requires authentication: = $job->isAuthenticationRequired() ? 'yes' : 'no' ?>
+ getSubJobs())): ?> +Sub jobs: = implode(', ', array_map(function($job) + { + return '' . $job->getName() . ''; + }, $job->getSubJobs())); ?>
+ + +