Skip to content

Commit

Permalink
- api requests handling without data fix
Browse files Browse the repository at this point in the history
- different http methods for api endpoints
  • Loading branch information
hotrush committed Apr 15, 2017
1 parent 8582ce5 commit 1d8c187
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,7 @@ public function __construct(Registry $registry, Logger $logger, Worker $worker)

public function processRequest(Request $request, Response $response)
{
if ($request->getMethod() !== 'POST') {
$this->replyWithError(405, 'Method now allowed.', $response);

return;
}

if (!$request->hasHeader('Content-Length')) {
$this->replyWithError(400, 'No content-length header provided', $response);

return;
}

if (((int) $request->getHeader('Content-Length')[0]) === 0) {
$this->replyWithError(400, 'Content-length header saying that no payload provided', $response);

return;
}

$endpoint = substr($request->getPath(), 1).'Action';
$endpoint = substr($request->getPath(), 1).(ucfirst(strtolower($request->getMethod()))).'Action';
$data = [];

if (!method_exists($this, $endpoint)) {
Expand All @@ -67,18 +49,23 @@ public function processRequest(Request $request, Response $response)
return;
}

$this->logger->info(substr($request->getPath(), 1).' action requested');

$request->on('data', function ($requestData) use (&$data, $endpoint, $response) {
$data = json_decode($requestData, true);
if ($data === false) {
$this->replyWithError(400, 'Invalid payload.', $response);

return;
}
$this->$endpoint($data, $response);
$this->logger->info('Request payload: '.$requestData);
});
$this->logger->info($endpoint.' endpoint requested');

if ($request->hasHeader('Content-Length') && ((int) $request->getHeader('Content-Length')[0]) > 0) {
$request->on('data', function ($requestData) use (&$data, $endpoint, $response) {
$data = json_decode($requestData, true);
if ($data === false) {
$this->replyWithError(400, 'Invalid payload.', $response);

return;
}
$this->logger->info('Request payload: '.$requestData);
$this->$endpoint($response, $data);
});
} else {
$this->logger->info('No payload received');
$this->$endpoint($response);
}

$request->on('error', function () use ($response) {
$this->logger->error('Error occurred while data receiving.');
Expand All @@ -103,7 +90,7 @@ private function replyWithJson(array $data, Response $response)
$response->end($dataEncoded);
}

private function scheduleAction(array $payload, Response $response)
private function schedulePostAction(Response $response, array $payload = [])
{
if (!isset($payload['spider']) || !$this->registry->spiderExists($payload['spider'])) {
$this->replyWithError(400, 'No spider found', $response);
Expand All @@ -117,7 +104,7 @@ private function scheduleAction(array $payload, Response $response)
$this->replyWithJson(['message' => 'Job scheduled', 'job_id' => $jobId], $response);
}

private function listAction(array $payload, Response $response)
private function listGetAction(Response $response)
{
$activeJobs = [];

Expand All @@ -131,7 +118,7 @@ private function listAction(array $payload, Response $response)
$this->replyWithJson(['active_jobs' => $activeJobs], $response);
}

private function cancelAction(array $payload, Response $response)
private function cancelPostAction(Response $response, array $payload = [])
{
if (!isset($payload['id'])) {
$this->replyWithError(400, 'No job id provided.', $response);
Expand Down

0 comments on commit 1d8c187

Please sign in to comment.