You are here

class NewRelicApiClient in New Relic 8

Same name and namespace in other branches
  1. 2.x src/Client/NewRelicApiClient.php \Drupal\new_relic_rpm\Client\NewRelicApiClient
  2. 2.0.x src/Client/NewRelicApiClient.php \Drupal\new_relic_rpm\Client\NewRelicApiClient

Controls the interaction between us and newrelic rest API v2.

Hierarchy

Expanded class hierarchy of NewRelicApiClient

2 files declare their use of NewRelicApiClient
NewRelicRpmCommands.php in src/Commands/NewRelicRpmCommands.php
NewRelicRpmDeploy.php in src/Form/NewRelicRpmDeploy.php
1 string reference to 'NewRelicApiClient'
new_relic_rpm.services.yml in ./new_relic_rpm.services.yml
new_relic_rpm.services.yml
1 service uses NewRelicApiClient
new_relic_rpm.client in ./new_relic_rpm.services.yml
Drupal\new_relic_rpm\Client\NewRelicApiClient

File

src/Client/NewRelicApiClient.php, line 16

Namespace

Drupal\new_relic_rpm\Client
View source
class NewRelicApiClient {
  use StringTranslationTrait;
  const API_URL = 'https://api.newrelic.com/v2';

  /**
   * The API key to connect to newrelic.
   *
   * @var string
   */
  protected $apiKey;

  /**
   * The serialisation JSON service.
   *
   * @var \Drupal\Component\Serialization\Json
   */
  protected $parser;

  /**
   * The application ID in newrelic.
   *
   * @var int
   */
  private $appId;

  /**
   * The application name being used for this instance.
   *
   * @var string
   */
  private $appName;

  /**
   * The http_client service.
   *
   * @var \GuzzleHttp\Client
   */
  protected $httpClient;

  /**
   * A loaded config object for new_relic_rpm.settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * A logger service instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a new NewRelicApiClient.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory so we can load config that we need.
   * @param \GuzzleHttp\Client $http_client
   *   The http client to send requests to newrelic.
   * @param \Drupal\Component\Serialization\Json $serialization_json
   *   Decoding the returned result from newrelic.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   For logging notifications to Drupal.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, Client $http_client, Json $serialization_json, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation) {
    $this->config = $config_factory
      ->get('new_relic_rpm.settings');
    $this->httpClient = $http_client;
    $this->parser = $serialization_json;
    $this->logger = $logger_factory
      ->get('new_relic_rpm');
    $this
      ->setStringTranslation($string_translation);
    $this->apiKey = $this->config
      ->get('api_key');
    $app_name = ini_get('newrelic.appname');
    if (!empty($app_name)) {
      $this
        ->setAppName($app_name);
    }
  }

  /**
   * Change the application name used for requests.
   *
   * Defaults to the newrelic.appname php configuration value.
   *
   * @param string $name
   *   The name to use.
   */
  public function setAppName($name) {
    $this->appName = $name;
    $this->appId = NULL;
  }

  /**
   * Lazy load the application ID from newrelic.
   *
   * @return int|null
   *   The application ID or null if it failed.
   */
  public function getAppId() {
    if (empty($this->appName)) {
      return NULL;
    }
    if (empty($this->appId)) {

      // Populate the appId from the name.
      try {
        $applications = $this
          ->listApplications($this->appName);
      } catch (GuzzleException $e) {
        return NULL;
      }
      foreach ($applications as $application_details) {
        if ($application_details['name'] === $this->appName) {
          $this->appId = $application_details['id'];
          break;
        }
      }
      if (empty($this->appId)) {
        $this->logger
          ->error('Unable to get appId for :name', [
          ':name' => $this->appName,
        ]);
      }
    }
    return $this->appId;
  }

  /**
   * Get a list of applications available for this API key.
   *
   * @param string $name
   *   Optional filter by application name.
   *
   * @return array
   *   An array of application details.
   *
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function listApplications($name = NULL) {
    $filters = [];
    if (!empty($name)) {
      $filters['name'] = $name;
    }
    $response = $this
      ->request('/applications', [
      'filters' => $filters,
    ]);
    $result = $this->parser
      ->decode($response
      ->getBody()
      ->getContents());
    return $result['applications'];
  }

  /**
   * Create a deployment marker in newrelic.
   *
   * @param string $revision
   *   The revision to deploy.
   * @param string $description
   *   A description for the deployment.
   * @param string $user
   *   The user to deploy as.
   * @param string $changelog
   *   The changelog information.
   *
   * @return bool
   *   Whether the deployment marker was successful or not.
   */
  public function createDeployment($revision, $description = NULL, $user = NULL, $changelog = NULL) {
    $app_id = $this
      ->getAppId();
    if (empty($app_id)) {
      return FALSE;
    }
    $post_vars = [
      'deployment' => [
        'revision' => $revision,
      ],
    ];
    if (isset($description)) {
      $post_vars['deployment']['description'] = $description;
    }
    if (isset($user)) {
      $post_vars['deployment']['user'] = $user;
    }
    if (isset($changelog)) {
      $post_vars['deployment']['changelog'] = $changelog;
    }
    $uri = '/applications/' . $app_id . '/deployments';
    try {
      $this
        ->request($uri, [
        'form_params' => $post_vars,
      ], 'POST');
      return TRUE;
    } catch (GuzzleException $e) {
      return FALSE;
    }
  }

  /**
   * Build the URL for the API.
   *
   * @param string $uri
   *   The base path.
   * @param array $filters
   *   Request filters.
   *
   * @return string
   *   The full URL.
   */
  public function buildUrl($uri, array $filters = []) {
    $url = static::API_URL . $uri . '.json';
    if (empty($filters)) {
      return $url;
    }
    $params = [];
    foreach ($filters as $name => $value) {
      $params['filter[' . $name . ']'] = $value;
    }
    return $url . '?' . http_build_query($params);
  }

  /**
   * Make the request to newrelic.
   *
   * @param string $uri
   *   The API path to query.
   * @param array $options
   *   The options to send for the request.
   * @param string $method
   *   The request method.
   *
   * @return \Psr\Http\Message\ResponseInterface
   *   A response from the API request.
   *
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function request($uri, array $options = [], $method = 'GET') {
    $options = array_merge([
      'headers' => [
        'X-Api-Key' => $this->apiKey,
      ],
      'filters' => [],
    ], $options);
    $url = $this
      ->buildUrl($uri, $options['filters']);
    unset($options['filters']);
    try {
      $response = $this->httpClient
        ->request($method, $url, $options);
      return $response;
    } catch (GuzzleException $e) {
      watchdog_exception('new_relic_rpm', $e);
      throw $e;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NewRelicApiClient::$apiKey protected property The API key to connect to newrelic.
NewRelicApiClient::$appId private property The application ID in newrelic.
NewRelicApiClient::$appName private property The application name being used for this instance.
NewRelicApiClient::$config protected property A loaded config object for new_relic_rpm.settings.
NewRelicApiClient::$httpClient protected property The http_client service.
NewRelicApiClient::$logger protected property A logger service instance.
NewRelicApiClient::$parser protected property The serialisation JSON service.
NewRelicApiClient::API_URL constant
NewRelicApiClient::buildUrl public function Build the URL for the API.
NewRelicApiClient::createDeployment public function Create a deployment marker in newrelic.
NewRelicApiClient::getAppId public function Lazy load the application ID from newrelic.
NewRelicApiClient::listApplications public function Get a list of applications available for this API key.
NewRelicApiClient::request public function Make the request to newrelic.
NewRelicApiClient::setAppName public function Change the application name used for requests.
NewRelicApiClient::__construct public function Constructs a new NewRelicApiClient.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.