| <?php |
|
|
| namespace JsonRPC; |
|
|
| use Exception; |
| use JsonRPC\Request\RequestBuilder; |
| use JsonRPC\Response\ResponseParser; |
|
|
| |
| |
| |
| |
| |
| |
| class Client |
| { |
| |
| |
| |
| |
| |
| |
| |
| private $isNamedArguments = true; |
|
|
| |
| |
| |
| |
| |
| |
| private $returnException = false; |
|
|
| |
| |
| |
| |
| |
| |
| private $isBatch = false; |
|
|
| |
| |
| |
| |
| |
| |
| private $batch = array(); |
|
|
| |
| |
| |
| |
| |
| |
| private $httpClient; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function __construct($url = '', $returnException = false, HttpClient $httpClient = null) |
| { |
| $this->httpClient = $httpClient ?: new HttpClient($url); |
| $this->returnException = $returnException; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function withPositionalArguments() |
| { |
| $this->isNamedArguments = false; |
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getHttpClient() |
| { |
| return $this->httpClient; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function authentication($username, $password) |
| { |
| $this->httpClient |
| ->withUsername($username) |
| ->withPassword($password); |
|
|
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function __call($method, array $params) |
| { |
| if ($this->isNamedArguments && count($params) === 1 && is_array($params[0])) { |
| $params = $params[0]; |
| } |
|
|
| return $this->execute($method, $params); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function batch() |
| { |
| $this->isBatch = true; |
| $this->batch = array(); |
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function send() |
| { |
| $this->isBatch = false; |
| return $this->sendPayload('['.implode(', ', $this->batch).']'); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function execute($procedure, array $params = array(), array $reqattrs = array(), $requestId = null, array $headers = array()) |
| { |
| $payload = RequestBuilder::create() |
| ->withProcedure($procedure) |
| ->withParams($params) |
| ->withRequestAttributes($reqattrs) |
| ->withId($requestId) |
| ->build(); |
|
|
| if ($this->isBatch) { |
| $this->batch[] = $payload; |
| return $this; |
| } |
|
|
| return $this->sendPayload($payload, $headers); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private function sendPayload($payload, array $headers = array()) |
| { |
| return ResponseParser::create() |
| ->withReturnException($this->returnException) |
| ->withPayload($this->httpClient->execute($payload, $headers)) |
| ->parse(); |
| } |
| } |
|
|