You are here

public function WSClientRESTEndpoint::client in Web service client 7

Return value

HttpClient

1 call to WSClientRESTEndpoint::client()
WSClientRESTEndpoint::call in wsclient_rest/wsclient_rest.inc
Calls the REST service.

File

wsclient_rest/wsclient_rest.inc, line 27
Web service client REST - include file.

Class

WSClientRESTEndpoint
A remote endpoint type for invoking REST services.

Code

public function client() {
  if (!isset($this->client)) {
    $settings = $this->service->settings;

    // Determine which type of authentication to use.
    $authentication = NULL;
    if (isset($settings['authentication'])) {

      // Handle each type of authentication.
      foreach ($settings['authentication'] as $auth => $auth_settings) {

        // @todo Remove this check once multiple authentications are
        // implemented.
        if ($auth == 'type') {
          continue;
        }

        // @todo Remove this check once multiple authentications are
        // implemeted. For now the switch would cause the last configured
        // authentication type to be taken.
        if ($auth == $settings['authentication']['type']) {

          // @todo Allow multiple authentications by implementing and using
          // HttpClientCompositeAuth.
          switch ($auth) {

            // OAuth v2 support using the http_client module.
            case 'oauth2':
              $authentication = new HttpClientOAuth2($auth_settings);
              break;

            // Default authentication options from the http_client module.
            case 'basic':
            case 'wss':
            default:
              $username = $auth_settings['username'];
              $password = $auth_settings['password'];
              $authentication = new HttpClientBasicAuth($username, $password);
              break;
          }
        }
      }
    }
    if (wsclient_rest_has_old_formatter($settings)) {
      $formatter = new $settings['formatter']();
      $this->client = new HttpClient($authentication, $formatter, $this);
    }
    else {
      $formatters = array();
      foreach (array(
        'send_formatter',
        'receive_formatter',
      ) as $formatter_type) {

        // Settings could be missing.
        $formatter_settings = !empty($settings[$formatter_type]['settings']) ? $settings[$formatter_type]['settings'] : array();
        $formatters[$formatter_type] = new $settings[$formatter_type]['class']($formatter_settings);
      }
      $formatter = new HttpClientCompositeFormatter($formatters['send_formatter'], $formatters['receive_formatter']);
      $this->client = new HttpClient($authentication, $formatter, $this);
    }

    // Pass through additional curl options.
    if (!empty($settings['curl options'])) {
      $this->client->options['curlopts'] = $settings['curl options'];
    }
  }
  return $this->client;
}