Пример #1
0
 /**
  * Create Request object from Raw Request
  *
  * @param string $string
  * @return Collection\Request | Collection\Response on error
  */
 public function createRequestFromRawRequest($string)
 {
     // Process through the layer before processing
     $string = $this->protocolLayerStack->handleRequest($string);
     $collection = new Collection\Request();
     try {
         // Decode the data
         $data = @json_decode($string, true);
         // No array means decoding failed
         if (is_array($data) === false) {
             throw new Exception\InvalidRequest('Parse error', static::PARSE_ERROR);
         }
         // Make it a batch request
         if (Common\ArrayUtils::isAssociative($data)) {
             $data = array($data);
         }
         foreach ($data as $requestData) {
             // Always hace request object
             $request = new Entity\Request();
             // Append the request object
             $collection->append($request);
             // Next checks expects an array
             if (is_array($requestData) === false) {
                 $requestData = array();
             }
             // Get the available arguments
             $jsonRpc = $this->getArrayItem('jsonrpc', $requestData, null);
             $method = $this->getArrayItem('method', $requestData, null);
             $params = $this->getArrayItem('params', $requestData, array());
             $id = $this->getArrayItem('id', $requestData, null);
             // Set the values on the request object
             $request->setJsonrpc($jsonRpc)->setId($id)->setMethod($method);
             // Set all the parameters
             foreach ($params as $offset => $value) {
                 $request->addParam($value, $offset);
             }
         }
     } catch (\Exception $e) {
         // Error occured and create
         $response = $this->createResponseFromException(new Entity\Request(), $e);
         // Create collection response object
         $responseCollection = new Collection\Response();
         // Attach the single response error (parse error);
         $responseCollection->append($response);
         // Return the response collection
         return $responseCollection;
     }
     return $collection;
 }
Пример #2
0
 /**
  * Send the request(s)
  *
  * @param  Collection\Request $request
  * @return Collection\Response
  */
 public function sendRequest(Collection\Request $request)
 {
     $rawPost = $this->prepareRequest($request);
     // First go through the layer stack
     $rawPost = $this->protocolLayerStack->handleRequest($rawPost);
     $httpRequest = $this->getHttpRequest($rawPost);
     // Perform request
     $result = $httpRequest->execute();
     // Retrieve request information
     $info = $httpRequest->getInfo();
     // Close the connection
     $httpRequest->close();
     // Notify action no content available
     if ($info['http_code'] === 204) {
         return null;
     }
     // 404 is not good
     if ($info['http_code'] === 404) {
         throw new Exception\InvalidRequest('Host ' . $this->getUrl() . ' not found', 2);
     }
     // parse raw response and return response collection
     return $this->parseResponse($request, $result, $info);
 }
 public function testHandleRequest()
 {
     $stack = new ProtocolLayerStack();
     $layerA = new \ClientProtocolLayer();
     $layerB = new \ClientProtocolLayer();
     $stack->addLayer($layerA, ProtocolLayerStack::PLACEMENT_TOP);
     $stack->addLayer($layerB, ProtocolLayerStack::PLACEMENT_TOP);
     $resultA = $stack->handleRequest('hello');
     $resultB = $stack->handleRequest('something');
     $this->assertEquals('world', $resultA);
     $this->assertEquals('something', $resultB);
 }