You are here

private function ApiClient::sendToRocketChat in Rocket.Chat 8

Same name and namespace in other branches
  1. 8.2 modules/rocket_chat_api/src/RocketChat/ApiClient.php \Drupal\rocket_chat_api\RocketChat\ApiClient::sendToRocketChat()

Simple low level helper to GET or POST to the rocketchat.

Parameters

string $httpVerb: one of the HTTP_* Verbs to use for this call.

string $method: The method to call (so the part after '/api/v1/').

array $options: Optional Data payload. for HTTP_POST calls.

Return value

array Result array.

2 calls to ApiClient::sendToRocketChat()
ApiClient::getFromRocketChat in modules/rocket_chat_api/src/RocketChat/ApiClient.php
Low Level GET request to the rocketchat.
ApiClient::postToRocketChat in modules/rocket_chat_api/src/RocketChat/ApiClient.php
Send message to Rocket Chat.

File

modules/rocket_chat_api/src/RocketChat/ApiClient.php, line 197

Class

ApiClient
Class ApiClient.

Namespace

Drupal\rocket_chat_api\RocketChat

Code

private function sendToRocketChat($httpVerb = ApiClient::HTTP_GET, $method = "info", array $options = []) {
  $result = new \stdClass();
  try {
    switch ($httpVerb) {
      case ApiClient::HTTP_GET:
        $result = $this->client
          ->get($method, $options);
        break;
      case ApiClient::HTTP_POST:
        $result = $this->client
          ->post($method, $options);
        break;
      default:
        throw new ClientException("HTTP Verb is unsupported", NULL, NULL, NULL, NULL);
    }
    $resultString = (string) $result
      ->getBody();

    // HTTP Headers.
    $resultHeader = $result
      ->getHeaders();

    // HTTP Response Code (like 200).
    $resultCode = $result
      ->getStatusCode();

    // HTTP Response String (like OK).
    $resultStatus = $result
      ->getReasonPhrase();
  } catch (ClientException $e) {
    $resultStatus = $e
      ->getMessage();
    $resultCode = $e
      ->getCode();
    $resultString = [];
    $resultString['status'] = 'failed';
    $resultString['response'] = $e
      ->getResponse();
    $resultHeader['content-type'][0] = "Error";
  }
  if (isset($resultHeader['content-type']) && !isset($resultHeader['Content-Type'])) {

    // Quick fix to prevent errors due to capitalization of content-type
    // in the header.
    $resultHeader['Content-Type'] = $resultHeader['content-type'];
  }
  if ($resultHeader['Content-Type'][0] == 'application/json') {
    $jsonDecoder = $this->config
      ->getJsonDecoder();
    $resultString = $jsonDecoder($resultString);
  }
  $returnValue = [];
  $returnValue['result'] = $result;
  $returnValue['body'] = $resultString;
  $returnValue['status'] = $resultStatus;
  $returnValue['code'] = $resultCode;
  return $returnValue;
}