fetch-app
UrlFetchApp with retries and more
AI Summary
FetchApp
Extends GAS UrlFetchApp with retries, delays, and callbacks.
Target Users
Intermediate GAS developers building Web API integrations needing reliable HTTP requests.
Problems Solved
UrlFetchApp lacks built-in retries, delay strategies, and custom callbacks for reliable HTTP handling.
Script ID
- In GAS Editor: Click "Libraries +" → Paste into "Script ID" field → Click "Look up"
- "fetch-app" will appear in the search results
- Select the latest version (highest number) from "Version" dropdown
- Click "Add"
Tags
Main Features
1
Auto Retries
Retries up to maxRetries based on successCodes or retryCodes in responses.
2
Delay Control
Linear or exponential delays via delay and delayFactor, capped by maxDelay.
3
Custom Callbacks
onRequestFailure and onAllRequestsFailure for custom logic on failures.
Examples
Main Functions
| Function | Description |
|---|---|
| FetchApp.fetch | HTTP fetch with retries (23 chars) |
| FetchApp.getClient | Get reusable client (17 chars) |
Examples
Drop-in UrlFetchApp Replacement
/**
* UrlFetchApp直接置換
* ・デフォルトmuteHttpExceptions: true: FetchApp.fetch(url, params)は自動設定
* ・configでリトライ指定: const config = { maxRetries: 5, successCodes: [200], delay: 500 }
* ・パラメータなし時: FetchApp.fetch(url, {}, config)
*/
function dropInReplacement() {
const config = {
maxRetries: 5,
successCodes: [200],
delay: 500,
};
const response1 = FetchApp.fetch(url, params, config);
const response2 = FetchApp.fetch(url, {}, config);
}
Configurable Client Usage
/**
* クライアント再利用
* ・クライアント作成: const client = FetchApp.getClient(config)
* ・config適用fetch: client.fetch(url, params)
* ・部分オーバーライド: client.fetch(url, params, { successCodes: [200] })
*/
function useConfigurableClient() {
const config = {
maxRetries: 5,
retryCodes: [500, 502, 503, 504],
delay: 500,
};
const client = FetchApp.getClient(config);
const response1 = client.fetch(url, params);
const response2 = client.fetch(url, params, { successCodes: [200] });
}
Callbacks for Exceptions
/**
* コールバック使用
* ・コールバック定義: const stopRequests = ({ url, response }) => { ... throw new Error(...) }
* ・configに設定: onRequestFailure: stopRequests
* ・指定コードで例外: if ([401, 403].includes(responseCode))
*/
function useCallbacks() {
const stopRequests = ({ url, response }) => {
const responseCode = response.getResponseCode();
if ([401, 403].includes(responseCode)) {
throw new Error(`Received ${responseCode} when accessing ${url}`);
}
};
const config = {
successCodes: [200],
maxRetries: 5,
delay: 500,
onRequestFailure: stopRequests,
};
const response = FetchApp.fetch(url, params, config);
}
