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.
OBJECT_API = 'sobjects'; |
|
RETRY_STATUSES = [ 400, 404, 415, 500 ]; |
|
protected Client
|
$_client |
from SFAPIClient | |
protected string
|
$_token |
from SFAPIClient |
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 |
__construct()
protected __construct(Client $client, string $token, Authentication $auth)
connect()
public static SFAPIClient connect(Endpoint $endpoint, Authentication $auth)
Constructs a new SalesForce API client with a given SalesForce API endpoint and authentication mechanism.
Endpoint |
$endpoint |
SalesForce endpoint to make calls against |
Authentication |
$auth |
SalesForce authentication mechanism |
SFAPIClient |
FailedToAuthenticate | |
GuzzleException |
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.
Client |
$client |
Http client that will be used by the SFAPIClient to communicate with the SalesForce API |
Authentication |
$auth |
SalesForce authentication mechanism |
SFAPIClient |
FailedToAuthenticate | |
GuzzleException |
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');
string |
$objectType |
SalesForce object type to create a client for |
ScopedSFAPIClient |
create()
public SFCreationResult create(string $objectType, array $data)
Performs a create operation for a given type.
string |
$objectType |
SalesForce object type to create |
array |
$data |
Field data to create the object with |
SFCreationResult |
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.
string |
$objectType |
|
string |
$id |
|
array |
$fields |
|
$timeout |
SFObjectResult |
Exception |
patch()
public BoolResult patch(string $objectType, string $id, array $data)
Updates a single object of a given type with the given id.
string |
$objectType |
SalesForce object type to update |
string |
$id |
SalesForce id type to update |
array |
$data |
Field data to update the object with |
BoolResult |
delete()
public BoolResult delete(string $objectType, string $id)
Deletes a single object of a given type with the given id.
string |
$objectType |
SalesForce object type to delete |
string |
$id |
SalesForce id type to delete |
BoolResult |
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.
string |
$query |
A SOQL query to run |
SFRecordsResult |
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.
string |
$query |
A SOQL query to run |
SFRecordsResult |
o()
public string o(string $objectType, string $id = '')
Helper function for constructing a SalesForce Object REST uri.
string |
$objectType |
|
string |
$id |
string |
authenticatedRequest()
public Request authenticatedRequest(Request $request)
Given an arbitrary request, returns a new request with the client stored authentication information attached.
Request |
$request |
Request |
Executes an arbitrary request against the SalesForce API. Extended from SFAPICLient to attempt reauthorization if the request fails due to a 401 error
Request |
$request |
|
boolean |
$retry |
Result |
_send()
protected mixed _send(Request $request)
Request |
$request |
mixed |
GuzzleException |
<?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);
}
}
}