/**
  * @return Request
  */
 protected function convertRequest(RequestInterface $request, array $options)
 {
     $artaxRequest = new Request();
     $artaxRequest->setProtocol($request->getProtocolVersion());
     $artaxRequest->setMethod($request->getMethod());
     $artaxRequest->setUri((string) $request->getUri());
     $artaxRequest->setAllHeaders($request->getHeaders());
     $body = $request->getBody();
     if ($body->getSize() === null || $body->getSize() > 0) {
         $body->rewind();
         $artaxRequest->setBody(new PsrStreamIterator($body));
     }
     return $artaxRequest;
 }
Пример #2
0
 /**
  * Helper method to facilitate json rpc requests using the Amp\Artax client
  *
  * @param string $method    The rpc method to call
  * @param array  $params Associative array of rpc method arguments to send in the header (not auth arguments)
  *
  * @throws HTTPException When something goes wrong with the HTTP call
  *
  * @return Response The HTTP response containing headers / body ready for validation / parsing
  */
 private function performRPCRequest($method, array $params)
 {
     $client = $this->client;
     $request = new Request();
     if (empty($this->sessionCookie)) {
         $request->setUri(sprintf('%s:%s/json', $this->connectionArgs['host'], $this->connectionArgs['port']));
         $request->setMethod('POST');
         $request->setAllHeaders(array('Content-Type' => 'application/json; charset=utf-8'));
         $request->setBody(json_encode(array('method' => self::METHOD_AUTH, 'params' => array($this->connectionArgs['password']), 'id' => rand())));
         $promise = $client->request($request);
         /** @var Amp\Artax\Response $response */
         $response = Amp\wait($promise);
         if ($response->hasHeader('Set-Cookie')) {
             $cookieHeader = $response->getHeader('Set-Cookie');
             preg_match_all('/_session_id=(.*?);/', $cookieHeader[0], $matches);
             $this->sessionCookie = isset($matches[0][0]) ? $matches[0][0] : '';
         } else {
             throw new HTTPException("Response from torrent client did not return a Set-Cookie header");
         }
     }
     $request = new Request();
     $request->setUri(sprintf('%s:%s/json', $this->connectionArgs['host'], $this->connectionArgs['port']));
     $request->setMethod('POST');
     $request->setAllHeaders(array('Content-Type' => 'application/json; charset=utf-8', 'Cookie' => $this->sessionCookie));
     $body = array('method' => $method, 'params' => $params, 'id' => rand());
     $request->setBody(json_encode($body));
     $promise = $client->request($request);
     /** @var Amp\Artax\Response $response */
     $response = Amp\wait($promise);
     if ($response->getStatus() === 200) {
         $body = $response->getBody();
         $isJson = function () use($body) {
             json_decode($body);
             return json_last_error() === JSON_ERROR_NONE;
         };
         if ($isJson()) {
             return $response;
         } else {
             throw new HTTPException(sprintf('"%s" did not get back a JSON response body, got "%s" instead', $method, print_r($response->getBody(), true)));
         }
     } else {
         throw new HTTPException(sprintf('"%s" expected 200 response, got "%s" instead, reason: "%s"', $method, $response->getStatus(), $response->getReason()));
     }
 }