Add fetchAndStoreLogPage helper for v2 pagination
This commit is contained in:
@@ -249,3 +249,40 @@ function getSign($number) {
|
||||
|
||||
return '$';
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a single page of log entries from the Torn v2 API and store them.
|
||||
*
|
||||
* Performs one HTTP request, validates the response, inserts each vault
|
||||
* log entry (idempotently via INSERT ... ON CONFLICT), and returns the
|
||||
* pagination cursor (`_metadata.links.next`) if more pages remain.
|
||||
*
|
||||
* @param PDO $pdo Database connection used to insert vault entries.
|
||||
* @param string $user The user whose log entries are being fetched.
|
||||
* @param string $url Full URL for the v2 API request.
|
||||
*
|
||||
* @throws ApiKeyMissingException If no API key is configured for the user.
|
||||
* @throws CurlErrorException If the HTTP request fails.
|
||||
* @throws JsonDataException If the response body is not valid JSON.
|
||||
* @throws ApiValidationException If the response is missing the `log` array.
|
||||
*
|
||||
* @return string|null The `_metadata.links.next` URL, or null when there are no more pages.
|
||||
*/
|
||||
function fetchAndStoreLogPage($pdo, $user, $url) {
|
||||
if (!array_key_exists($user, USER_KEYS)) {
|
||||
throw new ApiKeyMissingException("User does not have an API key configured.");
|
||||
}
|
||||
|
||||
$responseData = executeApiCall($url, USER_KEYS[$user]);
|
||||
validateApiResponse($responseData);
|
||||
|
||||
$insertStmt = $pdo->prepare(
|
||||
'INSERT INTO vault (id, user, timestamp, description, amount) '
|
||||
. 'VALUES (:id, :user, :timestamp, :description, :amount) '
|
||||
. 'ON CONFLICT(id) DO NOTHING'
|
||||
);
|
||||
|
||||
processLogEntries($responseData['log'], $user, $insertStmt);
|
||||
|
||||
return $responseData['_metadata']['links']['next'] ?? null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user