acquia_vwo-1.0.x-dev/modules/acquia_vwo_content/src/Service/ApiClientService.php

modules/acquia_vwo_content/src/Service/ApiClientService.php
<?php

namespace Drupal\acquia_vwo_content\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Drupal\acquia_vwo_content\Service\Helper\Util;
use Psr\Log\LoggerInterface;

/**
 * Provides a Drupal service for making API requests.
 */
class ApiClientService {

  /**
   * The Guzzle HTTP client.
   *
   * @var \GuzzleHttp\Client
   */
  protected Client $client;

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

  /**
   * The API token.
   *
   * @var string
   */
  protected string $token;

  /**
   * Constructs an ApiClientService object.
   *
   * @param \GuzzleHttp\Client $client
   *   The Guzzle HTTP client.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service.
   */
  public function __construct(Client $client, LoggerInterface $logger) {
    $this->client = $client;
    $this->logger = $logger;
    $this->token = Util::getApiKey();
  }

  /**
   * Sends an HTTP request.
   *
   * @param string $method
   *   The HTTP method (GET, POST, PATCH, DELETE).
   * @param string $url
   *   The API endpoint.
   * @param array $options
   *   The request options.
   *
   * @return mixed
   *   The response data.
   */
  protected function sendRequest(string $method, string $url, array $options = []): mixed {
    $options['headers'] = array_merge($options['headers'] ?? [], [
      'Accept' => 'application/json',
      'token' => $this->token,
    ]);

    try {
      $response = $this->client->request($method, $url, $options);
      return $this->handleResponse($response);
    }
    catch (GuzzleException $e) {
      $this->logger->error('API request failed: @message', ['@message' => $e->getMessage()]);
      return NULL;
    }
  }

  /**
   * Handles the response.
   *
   * @param \Psr\Http\Message\ResponseInterface $response
   *   The HTTP response.
   *
   * @return mixed
   *   The decoded response body.
   */
  protected function handleResponse(ResponseInterface $response): mixed {
    return json_decode($response->getBody()->getContents(), TRUE);
  }

  /**
   * Sends data using a POST request.
   */
  public function sendData(string $endpoint, array $data): mixed {
    return $this->sendRequest('POST', $endpoint, ['json' => $data]);
  }

  /**
   * Updates data using a PATCH request.
   */
  public function updateData(string $endpoint, array $data): mixed {
    return $this->sendRequest('PATCH', $endpoint, ['json' => $data]);
  }

  /**
   * Deletes data using a DELETE request.
   */
  public function deleteData(string $endpoint): mixed {
    return $this->sendRequest('DELETE', $endpoint);
  }

  /**
   * Sends data to VWO.
   */
  public function sendDataToVwo(array $data): mixed {
    return $this->sendData($this->buildVwoUri('changesets'), [
      'name' => $data['name'],
      'description' => $data['name'],
      'type' => 'custom',
      'isGlobal' => TRUE,
      'code' => $data['code'],
    ]);
  }

  /**
   * Updates data in VWO.
   */
  public function updateDataToVwo(array $data, string $vwo_id): mixed {
    return $this->updateData($this->buildVwoUri("changesets/$vwo_id"), [
      'name' => $data['name'],
      'description' => $data['name'],
      'type' => 'custom',
      'isGlobal' => TRUE,
      'code' => $data['code'],
    ]);
  }

  /**
   * Deletes data from VWO.
   */
  public function deleteDataFromVwo(string $vwo_id): mixed {
    return $this->deleteData($this->buildVwoUri("changesets/$vwo_id"));
  }

  /**
   * Builds the VWO API URI.
   */
  private function buildVwoUri(string $path): string {
    return sprintf('https://app.vwo.com/api/v2/accounts/%s/%s', Util::getAccountId(), $path);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc