Skip to content

Commit 9d5a78a

Browse files
committed
improved request handling to prevent double-sending requests with expired tokens
1 parent c4aafaf commit 9d5a78a

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

src/Services/FileMakerConnection.php

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected function getLayoutUrl($layout = null)
118118
/**
119119
* @throws FileMakerDataApiException
120120
*/
121-
protected function checkResponseForErrors($response)
121+
protected function checkResponseForErrors($response): void
122122
{
123123
$messages = Arr::get($response, 'messages', []);
124124

@@ -128,10 +128,6 @@ protected function checkResponseForErrors($response)
128128

129129
if ($code !== 0) {
130130
switch ($code) {
131-
case 952:
132-
// API token is expired. We should expire it in the cache so it isn't used again.
133-
$this->forgetSessionToken();
134-
break;
135131
case 105:
136132
// Layout is missing error
137133
// Add the layout name to the message for clarity
@@ -234,9 +230,6 @@ public function performFind(FMBaseBuilder $query)
234230

235231
$response = $this->makeRequest('post', $url, $postData);
236232

237-
// Check for errors
238-
$this->checkResponseForErrors($response);
239-
240233
return $response;
241234
}
242235

@@ -632,24 +625,46 @@ public function setGlobalFields(array $globalFields)
632625
return $response;
633626
}
634627

635-
/**
636-
* @throws FileMakerDataApiException
637-
*/
638-
protected function makeRequest($method, $url, $params = [], ?PendingRequest $request = null)
628+
protected function prepareRequestForSending($request)
639629
{
640-
$this->login();
641-
642630
if ($request instanceof PendingRequest) {
643631
$request->withToken($this->sessionToken);
644632
} else {
645633
$request = Http::withToken($this->sessionToken);
646634
}
647635

648-
$response = $request->withMiddleware($this->retryMiddleware())
649-
->{$method}($url, $params);
636+
$request->withMiddleware($this->retryMiddleware());
650637

638+
return $request;
639+
}
640+
641+
/**
642+
* @throws FileMakerDataApiException
643+
*/
644+
protected function makeRequest($method, $url, $params = [], ?PendingRequest $request = null)
645+
{
646+
$this->login();
647+
648+
$request = $this->prepareRequestForSending($request);
649+
650+
// make the request
651+
$response = $request->{$method}($url, $params);
651652
// Check for errors
653+
try {
652654
$this->checkResponseForErrors($response);
655+
} catch (FileMakerDataApiException $e) {
656+
if ($e->getCode() === 952) {
657+
// the session expired, so we should forget the token and re-login
658+
$this->forgetSessionToken();
659+
$this->login();
660+
661+
// try the request again with refreshed credentials
662+
$request = $this->prepareRequestForSending($request);
663+
$response = $request->{$method}($url, $params);
664+
} else {
665+
throw $e;
666+
}
667+
}
653668

654669
// Return the JSON response
655670
$json = $response->json();
@@ -678,7 +693,6 @@ protected function retryMiddleware()
678693
}
679694

680695
$should_retry = false;
681-
$refresh = false;
682696
$log_message = null;
683697

684698
// Retry connection exceptions
@@ -687,27 +701,10 @@ protected function retryMiddleware()
687701
$log_message = 'Connection Error: ' . $exception->getMessage();
688702
}
689703

690-
if ($response) {
691-
$contents = $response->getBody()->getContents();
692-
$contents = json_decode($contents, true);
693-
if ($response->getStatusCode() !== 200 && $contents !== null) {
694-
$code = (int) Arr::first(Arr::pluck(Arr::get($contents, 'messages'), 'code'));
695-
if ($code === 952 && $retries <= 1) {
696-
$refresh = true;
697-
$should_retry = true;
698-
}
699-
}
700-
}
701-
702704
if ($log_message) {
703705
error_log($log_message, 0);
704706
}
705707

706-
if ($refresh) {
707-
error_log('Refreshing Access Token…');
708-
$this->login();
709-
}
710-
711708
if ($should_retry) {
712709
if ($retries > 0) {
713710
error_log('Retry ' . $retries . '…', 0);

0 commit comments

Comments
 (0)