You are here

protected function HttpServiceApiHandler::buildServicesApiYaml in HTTP Client Manager 8.2

Same name and namespace in other branches
  1. 8 src/HttpServiceApiHandler.php \Drupal\http_client_manager\HttpServiceApiHandler::buildServicesApiYaml()

Builds all services api provided by .http_services_api.yml files.

Each service api is an array with the following keys:

  • id: The machine name of the Service Api.
  • title: The human-readable name of the API.
  • api_path: The Guzzle description path (relative to module directory).
  • provider: The provider module of the Service Api.
  • source: The absolute path to the Service API description file.
  • config: An array of additional configurations for the HttpClient class.

example_service:
  title: "Example Service"
  api_path: src/HttpService/example_service.json
  config:
    base_uri: "http://www.example.com/api/v1"
    timeout: 4
    connect_timeout: 3
    auth: ['username', 'secretPassword', 'Basic']
1 call to HttpServiceApiHandler::buildServicesApiYaml()
HttpServiceApiHandler::getServicesApi in src/HttpServiceApiHandler.php
Gets all available services Api.

File

src/HttpServiceApiHandler.php, line 158

Class

HttpServiceApiHandler
Class HttpServiceApiHandler.

Namespace

Drupal\http_client_manager

Code

protected function buildServicesApiYaml() {
  $this->servicesApi = [];
  $items = $this
    ->getYamlDiscovery()
    ->findAll();
  $extensions = [];
  foreach ($items as $provider => $servicesApi) {
    $module_path = $this->moduleHandler
      ->getModule($provider)
      ->getPath();
    foreach ($servicesApi as $id => $serviceApi) {
      if (!empty($serviceApi['parent'])) {
        $serviceApi['id'] = $id;
        $serviceApi['provider'] = $provider;
        $serviceApi['source'] = $this->root . '/' . $module_path . '/' . $serviceApi['api_path'];
        $extensions[] = $serviceApi;
        continue;
      }
      $this
        ->overrideServiceApiDefinition($id, $serviceApi);
      $this
        ->validateServiceApiDefinition($id, $serviceApi);
      $default = [
        'id' => $id,
        'provider' => $provider,
        'source' => $this->root . '/' . $module_path . '/' . $serviceApi['api_path'],
        'config' => [],
      ];
      $serviceApi = array_merge($default, $serviceApi);
      if (!empty($serviceApi[$id]['orig'])) {
        $serviceApi[$id]['orig'] = array_merge($default, $serviceApi[$id]['orig']);
      }
      $this->servicesApi[$id] = $serviceApi;
    }
  }

  // Let's process extensions so that we're sure we'll have parent api.
  foreach ($extensions as $serviceApi) {
    $id = $serviceApi['id'];
    $parent = $this
      ->load($serviceApi['parent']);
    $serviceApi = $serviceApi + $parent;
    $serviceApi['parent_title'] = $parent['title'];
    $this
      ->overrideServiceApiDefinition($id, $serviceApi);
    $this
      ->validateServiceApiDefinition($id, $serviceApi);
    $this->servicesApi[$id] = $serviceApi;
  }
}