Skip to content
Snippets Groups Projects
Commit 708b5a60 authored by Alexander Rhodes's avatar Alexander Rhodes
Browse files

Converts Json service to injected service in RestClient

parent 0c920af1
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@ Additional details:
- replace strings with consistent, drupal-wide text
e.g. "-- Select --", "--" . t('Select') . "--", "Select Object Type", etc.
Nobody wants to translate all those.
- Replace hooks with interfaces, plugins, event subscribers, etc.
- Automatically pre-add all required Salesforce fields to mappings
......@@ -40,17 +40,17 @@ Additional details:
- easier for other modules to extend, alter behavior
- standardized, consolidated approach
- Since Drupal Queue Interface is a properly OOP now, we should create a
- Since Drupal Queue Interface is a properly OOP now, we should create a
Salesforce Queue implementation that unlocks the full potential of Salesforce API
Whether or not this is dependent on SOAP is somewhat irrelevant, as REST and SOAP
could use the same queue interface, regardless of whether REST can leverage
could use the same queue interface, regardless of whether REST can leverage
the advanced features of SOAP.
- Conversions to do when https://drupal.org/node/1972304 lands
- Migration paths for field mappings
-- wait for dust to settle on field mapping schema
- Migration paths for mapping object
-- wait for dust to settle on mapping object schema
......@@ -203,4 +203,3 @@ salesforce.routing.yml
src/Rest/RestClient.php
58: * @TODO: Consider making a test API call.
149: // @TODO: convert this into Dependency Injection
services:
salesforce.client:
class: Drupal\salesforce\Rest\RestClient
arguments: ['@http_client', '@config.factory', '@state', '@cache.default']
arguments: ['@http_client', '@config.factory', '@state', '@cache.default', '@serialization.json']
......@@ -23,32 +23,82 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class RestClient implements RestClientInterface {
/**
* Reponse object.
*
* @var \GuzzleHttp\Psr7\Response
*/
public $response;
/**
* GuzzleHttp client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Salesforce API URL.
*
* @var Drupal\Core\Url
*/
protected $url;
/**
* Salesforce config entity.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
private $config;
/**
* editable version of config entity.
*
* @var \Drupal\Core\Config\Config
*/
private $configEditable;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface $state
*/
private $state;
/**
* The cache service.
*
* @var Drupal\Core\Cache\CacheBackendInterface cache
*/
protected $cache;
/**
* The JSON serializer service.
*
* @var \Drupal\Component\Serialization\Json $json
*/
protected $json;
const CACHE_LIFETIME = 300;
/**
* Constructor which initializes the consumer.
*
* @param \Drupal\Core\Http\Client $http_client
* The config factory.
* @param \Guzzle\Http\ClientInterface $http_client
* The config factory.
* {@inheritdoc}
*/
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache) {
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json) {
$this->configFactory = $config_factory;
$this->httpClient = $http_client;
$this->config = $this->configFactory->get('salesforce.settings');
$this->configEditable = $this->configFactory->getEditable('salesforce.settings');
$this->state = $state;
$this->cache = $cache;
$this->json = $json;
return $this;
}
......@@ -62,22 +112,7 @@ class RestClient implements RestClientInterface {
}
/**
* Make a call to the Salesforce REST API.
*
* @param string $path
* Path to resource.
* @param array $params
* Parameters to provide.
* @param string $method
* Method to initiate the call, such as GET or POST. Defaults to GET.
* @param bool $returnObject
* If true, return a Drupal\salesforce\Rest\RestResponse;
* Otherwise, return json-decoded response body only.
* Defaults to FALSE for backwards compatibility.
*
* @return mixed
*
* @throws GuzzleHttp\Exception\RequestException
* {@inheritdoc}
*/
public function apiCall($path, array $params = [], $method = 'GET', $returnObject = FALSE) {
if (!$this->getAccessToken()) {
......@@ -148,8 +183,7 @@ class RestClient implements RestClientInterface {
];
$data = NULL;
if (!empty($params)) {
// @TODO: convert this into Dependency Injection
$data = Json::encode($params);
$data = $this->json->encode($params);
}
return $this->httpRequest($url, $data, $headers, $method);
}
......@@ -188,7 +222,7 @@ class RestClient implements RestClientInterface {
protected function getErrorData(RequestException $e) {
$response = $e->getResponse();
$response_body = $response->getBody()->getContents();
$data = Json::decode($response_body);
$data = $this->json->decode($response_body);
if (!empty($data[0])) {
$data = $data[0];
}
......
......@@ -27,11 +27,17 @@ interface RestClientInterface {
* Constructor which initializes the consumer.
*
* @param \Drupal\Core\Http\Client $http_client
* The config factory.
* The HTTP Client.
* @param \Guzzle\Http\ClientInterface $http_client
* The config factory.
*/
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache);
* The Guzzle Client.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Core\Cache\CacheBackendInterface cache
* The cache service.
* @param \Drupal\Component\Serialization\Json $json
* The JSON serializer service.
*/
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json);
/**
* Determine if this SF instance is fully configured.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment