示例#1
0
 /**
  * Sends the request and handles the common errors.
  *
  * @param string $sessionId
  * @param string $requestBody
  * @return array
  * @throws CSRFException
  * @throws TransmissionException
  */
 private function sendRequest($sessionId, $requestBody)
 {
     try {
         $this->logger->debug('Request {request} sent to the Transmission RPC API.', ['request' => $requestBody]);
         $response = $this->httpClient->request('POST', '', ['body' => $requestBody, 'auth' => [$this->rpcUsername, $this->rpcPassword], 'headers' => ['X-Transmission-Session-Id' => $sessionId]]);
     } catch (ClientException $e) {
         if (409 === $e->getCode()) {
             $csrfException = new CSRFException('Invalid transmission session ID.', 0, $e);
             $csrfException->setSessionId($e->getResponse()->getHeader('X-Transmission-Session-Id')[0]);
             $this->logger->info('Invalid Transmission session ID. A new ID has been generated: {session_id}', ['session_id' => $csrfException->getSessionId()]);
             throw $csrfException;
         }
         throw $e;
     } catch (RequestException $e) {
         $this->logger->error('The Transmission RPC API returned a 500 error.', ['exception' => $e]);
         throw new TransmissionException('Transmission request error.', 0, $e);
     }
     $responseBody = json_decode($response->getBody(), true);
     if ($responseBody['result'] !== 'success') {
         $e = new TransmissionException('The Transmission RPC API returned an error: ' . $responseBody['result']);
         $e->setResult($responseBody['result']);
         $this->logger->error('The Transmission RPC API returned an error with this request: {request}. The response: {response}', ['request' => $requestBody, 'response' => $responseBody['result'], 'exception' => $e]);
         throw $e;
     }
     return $responseBody;
 }
 /**
  * Sends the request and handles the common errors.
  *
  * @param string $sessionId
  * @param string $requestBody
  * @return array
  * @throws CSRFException
  * @throws DuplicateTorrentException
  * @throws TransmissionException
  */
 private function sendRequest($sessionId, $requestBody)
 {
     try {
         $this->log('debug', 'Request sent to the Transmission RPC API.', ['request' => $requestBody]);
         $response = $this->httpClient->request('POST', '', ['body' => $requestBody, 'auth' => [$this->rpcUsername, $this->rpcPassword], 'headers' => ['X-Transmission-Session-Id' => $sessionId]]);
     } catch (ClientException $e) {
         if (409 === $e->getCode()) {
             $csrfException = new CSRFException('Invalid transmission session ID.', 0, $e);
             $csrfException->setSessionId($e->getResponse()->getHeader('X-Transmission-Session-Id')[0]);
             $this->log('info', 'Invalid Transmission session ID. A new ID has been generated.', ['session_id' => $csrfException->getSessionId()]);
             throw $csrfException;
         }
         throw $e;
     } catch (RequestException $e) {
         $this->log('error', 'The Transmission RPC API returned a 500 error.', ['exception' => $e]);
         throw new TransmissionException('Transmission request error.', 0, $e);
     }
     $responseBody = json_decode($response->getBody(), true);
     if ($responseBody['result'] !== 'success') {
         $e = new TransmissionException('The Transmission RPC API returned an error: ' . $responseBody['result']);
         $e->setResult($responseBody['result']);
         $e->setArguments($responseBody['arguments']);
         $this->log('error', 'The Transmission RPC API returned an error with this request.', ['request' => $requestBody, 'response' => $responseBody['result'], 'exception' => $e]);
         throw $e;
     }
     if (isset($responseBody['arguments']['torrent-duplicate'])) {
         $torrentDuplicateData = $responseBody['arguments']['torrent-duplicate'];
         $e = new DuplicateTorrentException();
         $e->setTorrentId($torrentDuplicateData['id']);
         $e->setTorrentName($torrentDuplicateData['name']);
         $e->setTorrentHashString($torrentDuplicateData['hashString']);
         throw $e;
     }
     return $responseBody;
 }