Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fetch-weather-api-write-spreadsheet-http-type #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ Weather API から47都道府県ごとの天気情報を取得し、 Googleス

## category

外部連携,マーケティング,SERVER_SIDE_ACTION
Weather API,Googleスプレッドシート,Craft Functions,SERVER_SIDE_ACTION

## functionType

http
16 changes: 14 additions & 2 deletions craft-functions/fetch-weather-api-write-spreadsheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const LOCATIONS = [
{ prefecture: '広島県', location: 'hiroshima', lat: '34.2348', lon: '132.2735' },
{ prefecture: '山口県', location: 'yamaguchi', lat: '34.1109', lon: '131.2817' },
{ prefecture: '徳島県', location: 'tokushima', lat: '34.0357', lon: '134.3334' },
{ prefecture: '香川県', location: 'kagawa', lat: '34.2024', lon: '134.0236' },
{ prefecture: '香川県', location: 'kagawa', lat: '34.2024', lon: '134.0236' },
{ prefecture: '愛媛県', location: 'ehime', lat: '33.5030', lon: '132.4558' },
{ prefecture: '高知県', location: 'kochi', lat: '33.3335', lon: '133.3152' },
{ prefecture: '福岡県', location: 'fukuoka', lat: '33.3623', lon: '130.2505' },
Expand Down Expand Up @@ -92,9 +92,19 @@ async function fetchData({ location, rows, logger }) {
}

export default async function (data, { MODULES }) {
const { req, res } = data;
const { initLogger, secret } = MODULES;
const logger = initLogger({ logLevel: LOG_LEVEL });

res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

if (req.method === 'OPTIONS') {
res.status(204).end();
return;
}

// GoogleサービスアカウントのJSONキーをシークレットから取得
const secrets = await secret.get({ keys: [SERVICE_ACCOUNT_KEY_SECRET] });
const jsonKeySecrets = secrets[SERVICE_ACCOUNT_KEY_SECRET];
Expand All @@ -115,7 +125,9 @@ export default async function (data, { MODULES }) {
await Promise.all(LOCATIONS.map(location => fetchData({ location, rows, logger })));
await updateSsValues(sheets, `${SHEET_NAME}!1:${LOCATIONS.length + 1}`, rows);
logger.debug('Data fetch completed:', rows);
res.status(200).json({ message: 'Success' });
} catch (error) {
logger.debug('Data fetch Failed:', error);
res.status(500).json({ message: 'Data fetch Failed' });
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"name": "LOG_LEVEL",
"value": "WARN",
"type": "string",
"required": true,
"description": "出力するログのレベルです。ERROR/WARN/INFO/DEBUGから指定します"
},
{
"name": "WEATHER_API_KEY",
"value": "",
Expand All @@ -13,13 +20,6 @@
"required": true,
"description": "該当スプレッドシートのIDです。例:「https://docs.google.com/spreadsheets/d/#{SPREADSHEET_ID}」のSPREADSHEET_ID部分を指します。"
},
{
"name": "LOG_LEVEL",
"value": "DEBUG",
"type": "string",
"required": true,
"description": "ログレベルです。DEBUG,INFO,WARN,ERROR,NONEのいずれかを指定してください。"
},
{
"name": "SHEET_NAME",
"value": "",
Expand All @@ -32,6 +32,6 @@
"value": "",
"type": "string",
"required": true,
"description": "GoogleサービスアカウントのJSONキーを登録したシークレットの名前です。"
"description": "利用するGoogleサービスアカウントのJSONキーを登録したシークレットの名前です。"
}
]
]