Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naming params added custom response code #3

Open
wants to merge 4 commits into
base: naming_params
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
phpunit.xml
vendor
composer.lock
composer.lock
.idea/
39 changes: 39 additions & 0 deletions Exception/RpcException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class RpcException extends Exception
{
private $data;

/**
* @var int
*/
private $httpStatusCode;


/**
* Set additional information about the error.
*
Expand All @@ -37,4 +43,37 @@ public function getData()
{
return $this->data;
}

/**
* Get response http status code
*
* @return int
*/
public function getHttpStatusCode()
{
return $this->httpStatusCode;
}

/**
* Set response http status code
*
* @param int $httpStatusCode
*/
public function setHttpStatusCode($httpStatusCode)
{
$this->httpStatusCode = $httpStatusCode;
}

/**
* Throw a new exception.
*
* @param string $message Error message
* @param int $code Error code (default = 0)
* @param int $httpStatusCode HTTP status code to send (default = 400)
*/
public function __construct($message, $code = 0, $httpStatusCode = 400)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __construct must always be the first method to increase readability according to https://symfony.com/doc/current/contributing/code/standards.html

{
parent::__construct($message, $code);
$this->httpStatusCode = $httpStatusCode;
}
}
15 changes: 8 additions & 7 deletions JsonRpc/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ public function createMethodCall(Request $request)
{
$content = $request->getContent();
if (empty($content)) {
throw new InvalidJsonRpcContent('The JSON-RPC request is empty', self::ERROR_INVALID_REQUEST);
throw new InvalidJsonRpcContent('The JSON-RPC request is empty', self::ERROR_INVALID_REQUEST, self::BAD_REQUEST_STATUS_CODE);
}

$data = json_decode($content, true);

if (is_null($data)) {
throw new InvalidJsonRpcContent('The JSON-RPC call is not valid', self::ERROR_PARSING);
throw new InvalidJsonRpcContent('The JSON-RPC call is not valid', self::ERROR_PARSING, self::ERROR_STATUS_CODE);
}

if (empty($data['jsonrpc']) || version_compare($data['jsonrpc'], '2.0', '<')) {
throw new InvalidJsonRpcVersion('The JSON-RPC call version is not supported', self::ERROR_SERVER_ERROR);
throw new InvalidJsonRpcVersion('The JSON-RPC call version is not supported', self::ERROR_SERVER_ERROR, self::ERROR_INVALID_REQUEST);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self::ERROR_INVALID_REQUEST is not a status code.

}

if (empty($data['method']) || !isset($data['params'])) {
Expand All @@ -71,11 +71,12 @@ public function createMethodCall(Request $request)
/**
* @param MethodResponse $response
*
* @param int $statusCode
* @return Response
*
* @throws \Seven\RpcBundle\Exception\UnknownMethodResponse
* @throws UnknownMethodResponse
*/
public function createHttpResponse(MethodResponse $response)
public function createHttpResponse(MethodResponse $response, $statusCode = Server::HTTP_SUCCESS_STATUS)
{
$data = array(
'jsonrpc' => '2.0',
Expand Down Expand Up @@ -116,7 +117,7 @@ public function createHttpResponse(MethodResponse $response)
$data['id'] = $response->getCallId();
}

return new Response(json_encode($data), 200, array('content-type' => 'application/json'));
return new Response(json_encode($data), $statusCode, array('content-type' => 'application/json'));
}

/**
Expand Down Expand Up @@ -155,7 +156,7 @@ public function createMethodResponse(Response $response)
throw new InvalidJsonRpcContent('The JSON-RPC fault code is not passed');
}

$exception = new Fault($data['error']['message'], $data['error']['code']);
$exception = new Fault($data['error']['message'], $data['error']['code'], $response->getStatusCode());
if (isset($data['error']['data'])) {
$exception->setData($data['error']['data']);
}
Expand Down
9 changes: 7 additions & 2 deletions Rpc/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
abstract class Implementation
{

const ERROR_STATUS_CODE = 500;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const BAD_REQUEST_STATUS_CODE = 400;
const NON_EXIST_STATUS_CODE = 404;

/**
* @param Request $request
* @return mixed
Expand All @@ -28,18 +32,19 @@ abstract class Implementation
abstract public function createMethodCall(Request $request);

/**
* @param Response $response
* @param Response $response
* @return MethodResponse
*/

abstract public function createMethodResponse(Response $response);

/**
* @param MethodResponse $response
* @param int $statusCode
* @return Response
*/

abstract public function createHttpResponse(MethodResponse $response);
abstract public function createHttpResponse(MethodResponse $response, $statusCode = Server::HTTP_SUCCESS_STATUS);

/**
* @param MethodCall $call
Expand Down
14 changes: 10 additions & 4 deletions Rpc/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

class Server implements ServerInterface
{
const HTTP_SUCCESS_STATUS = 200;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Symfony's response constants and remove this.


protected $impl;
protected $handlers;

Expand Down Expand Up @@ -61,6 +63,7 @@ public function handle(Request $request)
try {
$methodCall = $this->impl->createMethodCall($request);
$methodResponse = $this->_handle($methodCall);
$exceptionStatus = self::HTTP_SUCCESS_STATUS;
} catch (\Exception $e) {

// log exception
Expand All @@ -69,15 +72,17 @@ public function handle(Request $request)
}

$methodResponse = new MethodFault($e);
$exceptionStatus = method_exists($e, 'getHttpStatusCode') ? $e->getHttpStatusCode() : Implementation::BAD_REQUEST_STATUS_CODE;
}

return $this->impl->createHttpResponse($methodResponse);
return $this->impl->createHttpResponse($methodResponse, $exceptionStatus);
}

/**
* @param MethodCall $methodCall
*
* @return MethodResponse
* @throws MethodNotExists
* @throws InvalidParameters
*/
protected function _handle(MethodCall $methodCall)
{
Expand All @@ -97,6 +102,7 @@ protected function _handle(MethodCall $methodCall)
* @throws MethodNotExists
*
* @return mixed
* @throws InvalidParameters
*/
public function call($method, $parameters)
{
Expand Down Expand Up @@ -129,7 +135,7 @@ protected function isAssociative(array $arr)
* Prepare parameters.
*
* @param callable $callback
* @param array $parameters
* @param array $parameters
*
* @return array
*
Expand All @@ -151,7 +157,7 @@ protected function prepareParameters($callback, array $parameters)
) {
throw new InvalidParameters(
sprintf('Invalid number of parameters. %d given but %d are required of %d total.',
$paramCount, $refl->getNumberOfRequiredParameters(), $refl->getNumberOfParameters())
$paramCount, $refl->getNumberOfRequiredParameters(), $refl->getNumberOfParameters())
);
}

Expand Down
9 changes: 5 additions & 4 deletions XmlRpc/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ protected function getSchema()
}

/**
* @param MethodResponse $response
* @throws \Seven\RpcBundle\Exception\UnknownMethodResponse
* @param MethodResponse $response
* @param int $statusCode
* @return Response
* @throws UnknownMethodResponse
*/

public function createHttpResponse(MethodResponse $response)
public function createHttpResponse(MethodResponse $response, $statusCode = Server::HTTP_SUCCESS_STATUS)
{
$document = new \DOMDocument("1.0", "UTF-8");
$document->appendChild($responseEl = $document->createElement("methodResponse"));
Expand All @@ -140,7 +141,7 @@ public function createHttpResponse(MethodResponse $response)
throw new UnknownMethodResponse("Unknown MethodResponse instance");
}

return new Response($document->saveXML(), 200, array('content-type' => 'text/xml'));
return new Response($document->saveXML(), $statusCode, array('content-type' => 'text/xml'));
}

/**
Expand Down