ReconnectingSFAPIClient

class ReconnectingSFAPIClient extends SFAPIClient

Class ReconnectingSFAPIClient

An extension of the SalesForce API Client that will make a second authorization attempt if the first run attempt results in a 401 Error.

Constants

OBJECT_API = 'sobjects';

RETRY_STATUSES = [ 400, 404, 415, 500 ];

Properties

protected Client $_client from SFAPIClient
protected string $_token from SFAPIClient

Methods

protected __construct(Client $client, string $token, Authentication $auth) ReconnectingSFClient constructor.
public static SFAPIClient connect(Endpoint $endpoint, Authentication $auth) Constructs a new SalesForce API client with a given SalesForce API endpoint and authentication mechanism. from SFAPIClient
public static SFAPIClient connectWith(Client $client, Authentication $auth) Constructs a new SalesForce API client with a custom Http client and the given authentication mechanism. Extended from SFAPIClient::connectWith() to include $auth in the construction of the ReconnectingSFAPIClient.
public ScopedSFAPIClient scope(string $objectType) Constructs a new SalesForce API client that is scoped to a specific object type. from SFAPIClient
public SFCreationResult create(string $objectType, array $data) Performs a create operation for a given type. from SFAPIClient
public SFObjectResult get(string $objectType, string $id, array $fields = [], $timeout = 10) Fetches a single object of a given type. from SFAPIClient
public BoolResult patch(string $objectType, string $id, array $data) Updates a single object of a given type with the given id. from SFAPIClient
public BoolResult delete(string $objectType, string $id) Deletes a single object of a given type with the given id. from SFAPIClient
public SFRecordsResult query(string $query) Performs an arbitrary query against the SalesForce query endpoint and returns the results. from SFAPIClient
public SFRecordsResult fullQuery(string $query) Performs an multiple queries against the SalesForce query endpoint to capture all results for a given query. from SFAPIClient
public string o(string $objectType, string $id = '') Helper function for constructing a SalesForce Object REST uri. from SFAPIClient
public Request authenticatedRequest(Request $request) Given an arbitrary request, returns a new request with the client stored authentication information attached. from SFAPIClient
public Result run(Request $request, boolean $retry = true) Executes an arbitrary request against the SalesForce API. Extended from SFAPICLient to attempt reauthorization if the request fails due to a 401 error
protected mixed _send(Request $request) No description from SFAPIClient

Details

at line 30

__construct()

protected __construct(Client $client, string $token, Authentication $auth)

ReconnectingSFClient constructor.

Parameters

Client $client
string $token
Authentication $auth
in SFAPIClient at line 75

connect()

public static SFAPIClient connect(Endpoint $endpoint, Authentication $auth)

Constructs a new SalesForce API client with a given SalesForce API endpoint and authentication mechanism.

Parameters

Endpoint $endpoint SalesForce endpoint to make calls against
Authentication $auth SalesForce authentication mechanism

Return Value

SFAPIClient

Exceptions

FailedToAuthenticate
GuzzleException
at line 46

connectWith()

public static SFAPIClient connectWith(Client $client, Authentication $auth)

Constructs a new SalesForce API client with a custom Http client and the given authentication mechanism. Extended from SFAPIClient::connectWith() to include $auth in the construction of the ReconnectingSFAPIClient.

Parameters

Client $client Http client that will be used by the SFAPIClient to communicate with the SalesForce API
Authentication $auth SalesForce authentication mechanism

Return Value

SFAPIClient

Exceptions

FailedToAuthenticate
GuzzleException
in SFAPIClient at line 140

scope()

public ScopedSFAPIClient scope(string $objectType)

Constructs a new SalesForce API client that is scoped to a specific object type.

Note that this re-uses the Http client of the SalesForce API client that was used to create the scoped client. Object types passed to the method are not validated in any way.

$contacts = $client->scope('Contact');

Parameters

string $objectType SalesForce object type to create a client for

Return Value

ScopedSFAPIClient
in SFAPIClient at line 151

create()

public SFCreationResult create(string $objectType, array $data)

Performs a create operation for a given type.

Parameters

string $objectType SalesForce object type to create
array $data Field data to create the object with

Return Value

SFCreationResult
in SFAPIClient at line 186

get()

public SFObjectResult get(string $objectType, string $id, array $fields = [], $timeout = 10)

Fetches a single object of a given type.

The optional $fields argument may be used to restrict the fields that are returned. By default all fields are returned.

Parameters

string $objectType
string $id
array $fields
$timeout

Return Value

SFObjectResult

Exceptions

Exception
in SFAPIClient at line 223

patch()

public BoolResult patch(string $objectType, string $id, array $data)

Updates a single object of a given type with the given id.

Parameters

string $objectType SalesForce object type to update
string $id SalesForce id type to update
array $data Field data to update the object with

Return Value

BoolResult
in SFAPIClient at line 251

delete()

public BoolResult delete(string $objectType, string $id)

Deletes a single object of a given type with the given id.

Parameters

string $objectType SalesForce object type to delete
string $id SalesForce id type to delete

Return Value

BoolResult
in SFAPIClient at line 285

query()

public SFRecordsResult query(string $query)

Performs an arbitrary query against the SalesForce query endpoint and returns the results.

The results returned may not represent all of the objects in SalesForce that match the given query. Records up to the limit set by SalesForce are returned.

This method runs the provided query as-is and does not perform validation or sanitizion. Consumers of the client should handle these operations prior to passing the query to the client.

Parameters

string $query A SOQL query to run

Return Value

SFRecordsResult
in SFAPIClient at line 318

fullQuery()

public SFRecordsResult fullQuery(string $query)

Performs an multiple queries against the SalesForce query endpoint to capture all results for a given query.

This method utilizies the built in SalesForce query response pagination and should reflect the behavior of following the next page urls.

If any of the requests fail, the entire query attempt will fail and will return the erro of the failure.

This method runs the provided query as-is and does not perform validation or sanitizion. Consumers of the client should handle these operations prior to passing the query to the client.

Parameters

string $query A SOQL query to run

Return Value

SFRecordsResult
in SFAPIClient at line 359

o()

public string o(string $objectType, string $id = '')

Helper function for constructing a SalesForce Object REST uri.

Parameters

string $objectType
string $id

Return Value

string
in SFAPIClient at line 371

authenticatedRequest()

public Request authenticatedRequest(Request $request)

Given an arbitrary request, returns a new request with the client stored authentication information attached.

Parameters

Request $request

Return Value

Request
at line 71

run()

public Result run(Request $request, boolean $retry = true)

Executes an arbitrary request against the SalesForce API. Extended from SFAPICLient to attempt reauthorization if the request fails due to a 401 error

Parameters

Request $request
boolean $retry

Return Value

Result
in SFAPIClient at line 406

_send()

protected mixed _send(Request $request)

Parameters

Request $request

Return Value

mixed

Exceptions

GuzzleException

Source code

<?php

namespace SFClient\SalesForce;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use SFClient\Result\Result;
use SFClient\Auth\Authentication;
use SFClient\Exceptions\FailedToAuthenticate;

/**
 * Class ReconnectingSFAPIClient
 *
 * An extension of the SalesForce API Client that will make
 * a second authorization attempt if the first run attempt results in a
 * 401 Error.
 *
 * @package SFClient\SalesForce
 */
class ReconnectingSFAPIClient extends SFAPIClient {
  private $_auth;

  /**
   * ReconnectingSFClient constructor.
   * @param Client $client
   * @param string $token
   * @param Authentication $auth
   */
  protected function __construct(Client $client, string $token, Authentication $auth) {
    parent::__construct($client, $token);
    $this->_auth = $auth;
  }

  /**
   * Constructs a new SalesForce API client with a custom Http client and the
   * given authentication mechanism.  Extended from SFAPIClient::connectWith() to
   * include $auth in the construction of the ReconnectingSFAPIClient.
   *
   * @param Client $client Http client that will be used by the ReconnectingSFAPIClient to communicate with the SalesForce API
   * @param Authentication $auth SalesForce authentication mechanism
   * @return SFAPIClient
   * @throws FailedToAuthenticate
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public static function connectWith(Client $client, Authentication $auth): SFAPIClient {
    $token = $auth->getTokenFromResponse(
      $client->send($auth->getTokenRequest())
    );

    if ($token === null) {
      throw new FailedToAuthenticate();
    }

    return new ReconnectingSFAPIClient(
      $client,
      $token,
      $auth
    );
  }

  /**
   * Executes an arbitrary request against the SalesForce API.  Extended from
   * SFAPICLient to attempt reauthorization if the request fails due to a 401 error
   *
   * @param Request $request
   * @param boolean $retry
   * @return Result
   * @internal
   */
  public function run(Request $request, $retry = true): Result {
    try {
      return Result::ok($this->_send(
        $this->authenticatedRequest($request)
      ));
    } catch (ClientException $e) {
      if ($e->getCode() == 401 && $retry) {
        // Create new auth token and try again
        $token = $this->_auth->getTokenFromResponse(
          $this->_client->send($this->_auth->getTokenRequest())
        );

        if ($token === null) {
          return Result::err(new FailedToAuthenticate());
        }

        $this->_token = $token;
        return $this->run($request, false);
      }

      return Result::err($e);
    } catch (\Exception $e) {
      return Result::err($e);
    }
  }
}