YS's develop story

Open API ํ™œ์šฉํ•˜์—ฌ ๊ณตํœด์ผ ์ •๋ณด DB์— ์ €์žฅ ๋ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ with request module ๐Ÿ€ ๋ณธ๋ฌธ

๊ธฐํƒ€

Open API ํ™œ์šฉํ•˜์—ฌ ๊ณตํœด์ผ ์ •๋ณด DB์— ์ €์žฅ ๋ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ with request module ๐Ÿ€

Yusang 2021. 7. 21. 09:46

๐Ÿ€ Open API ํ™œ์šฉํ•˜์—ฌ ๊ณตํœด์ผ ์ •๋ณด ์ €์žฅ ๋ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ with request module 


โŒ› Problem

์‚ฌ์šฉ์ž์—๊ฒŒ ์˜ˆ์•ฝ ๊ด€๋ จ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋ ค๊ณ  ํ•  ๋•Œ, ๋ชจ๋“  ๊ณตํœด์ผ ๋‚ ์งœ๋ฅผ ์•Œ๊ณ  ์žˆ์–ด์•ผ๋งŒ

๊ณตํœด์ผ์— ์˜ˆ์•ฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ชจ๋“  ๊ณตํœด์ผ ๋‚ ์งœ๋ฅผ DB์— ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?
์ธํ„ฐ๋„ท์—์„œ ๋ชจ๋“  ๊ณตํœด์ผ์„ ์ฐพ์•„์„œ DB์— ํ•˜๋‚˜์”ฉ ์ž…๋ ฅํ•˜๋ฉด ๋ ๊นŒ์š”?
๋ฌผ๋ก  ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ๋„ˆ๋ฌด ๋ณ„๋กœ์ธ ๊ฑฐ ๊ฐ™์•„์š”.. ๐Ÿ˜•
๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.


๐Ÿ“œ Solution

 

ํ•œ๊ตญ์ฒœ๋ฌธ์—ฐ๊ตฌ์›_ํŠน์ผ ์ •๋ณด

(์ฒœ๋ฌธ์šฐ์ฃผ์ •๋ณด)๊ตญ๊ฒฝ์ผ์ •๋ณด, ๊ณตํœด์ผ์ •๋ณด, ๊ธฐ๋…์ผ์ •๋ณด, 24์ ˆ๊ธฐ์ •๋ณด, ์žก์ ˆ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋Š” ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.

www.data.go.kr

๊ฒ€์ƒ‰ ์ค‘ ๊ณตํœด์ผ ๊ด€๋ จ ์˜คํ”ˆ API๊ฐ€ ์žˆ์Œ์„ ์ฐพ๊ฒŒ ๋˜์—ˆ๊ณ  
์œ„ ๋งํฌ์—์„œ Open API๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์•„ API๋ฅผ Post Man์„ ํ™œ์šฉํ•ด ์š”์ฒญํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.



์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์œผ๋กœ ์—ฐ๋„์™€ ์›”์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ 

์„ค์ •ํ•œ ๊ธฐ๊ฐ„์— ๋งž๋Š” ๊ณตํœด์ผ์„ ๋ชจ๋‘ ๋ณด์—ฌ์ฃผ๋Š” ํ˜•์‹์˜ API์˜€์Šต๋‹ˆ๋‹ค.

 

 

request

Simplified HTTP request client.

www.npmjs.com

์ €๋Š” ์ด๋ฅผ request ๋ชจ๋“ˆ์„ ํ™œ์šฉํ•˜์—ฌ ์š”์ฒญ๋ฐ›์€ ๊ฐ’์„ ํŒŒ์‹ฑ ํ•˜์—ฌ

๊ณตํœด์ผ ๋‚ ์งœ๋ฅผ ์ „๋ถ€ DB์— ์ €์žฅํ•˜๊ณ ์ž ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์œ„ ๋งํฌ๋Š” request ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.

 

 

npm i request

request ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด request๋ชจ๋“ˆ์„ ์„ค์น˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

 

        const {solYear, solMonth} = req.query;       

        .....    
        ~

        let request = require('request'); //request๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž„์„ ๋ช…์‹œํ•ด ์ค๋‹ˆ๋‹ค.
        let options = {
            'method' : 'GET',
            'url'    : 'http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?solYear=' + solYear + '&solMonth=' + solMonth + '&_type=json&ServiceKey=' + secret_config.holidayOpenAPISecretKey,
            'headers': {}
        };
        
        let holidayList = [];
        request(options, async function (error, response, body) {
            if (error) {
                throw new Error(error);
            }
            let info = JSON.parse(body);

            if (solMonth == '01' || solMonth == '02' || solMonth == '05' || solMonth == '09' || solMonth == '10') {
                for (i in info['response']['body']['items']['item']) {
                    let holidayName = info['response']['body']['items']['item'][i]['dateName'];
                    let holidayDate = info['response']['body']['items']['item'][i]['locdate'];
                    holidayList.push([holidayName, holidayDate])
                }
            } else {
                let holidayName = info['response']['body']['items']['item']['dateName'];
                let holidayDate = info['response']['body']['items']['item']['locdate'];
                holidayList.push([holidayName, holidayDate])
            }

            if (holidayList.length > 0) {
                await reservationDao.postHoliday(connection, [holidayList]);
            }
        });
async function postHoliday(connection, [values]) {
    const postHolidayQuery = `
        INSERT INTO Holiday (holidayName, holidayDate)
        VALUES ?
    `;
    const [postHolidayRow] = await connection.query(
        postHolidayQuery,
        [values]
    );
    return postHolidayRow;
}

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์—ฐ๋„์™€ ์›”์„ ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์œผ๋กœ ์ž…๋ ฅ๋ฐ›์•„

ํ•ด๋‹น ์›”์˜ ๋ชจ๋“  ๊ณตํœด์ผ์„ DB์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก API๋ฅผ ์„ค๊ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์‹ค์ œ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋“ค์ž…๋‹ˆ๋‹ค. 2021๋…„ 8์›”๋ถ€ํ„ฐ 2022๋…„ 12์›”๊นŒ์ง€์˜ ๋ชจ๋“  ๊ณตํœด์ผ๋“ค์„ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ณตํœด์ผ๋กœ ๋“ฑ๋ก๋œ ๋Œ€ํ†ต๋ น ์„ ๊ฑฐ์™€ ์ „๊ตญ ๋™์‹œ ์ง€๋ฐฉ์„ ๊ฑฐ๊ฐ€ ๋ˆˆ์— ๋„๋„ค์š”.

์ด์ œ ์•ž์œผ๋กœ ์ €๋Š” ๊ณตํœด์ผ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์œ„์ฒ˜๋Ÿผ ์ง์ ‘ ์„ค๊ณ„ํ•œ API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.



๐Ÿ˜„ ์ถ”๊ฐ€์ ์ธ ํ™œ์šฉ๋ฒ•

SELECT holidayName, DATE_FORMAT(holidayDate, "%Y-%m-%d") AS holiday
FROM Holiday
WHERE DATE_FORMAT(holidayDate, "%Y-%m") = ?


์ด๋ ‡๊ฒŒ ์ €์žฅ๋œ ๊ณตํœด์ผ ๋‚ ์งœ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์˜ˆ์•ฝ ๊ธฐ๋Šฅ API๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ
ํด๋ผ์ด์–ธํŠธ๊ฐ€ 'ํŠน์ • ์›”์˜ ๊ณตํœด์ผ์„ ์•Œ๋ ค์ค˜'๋ผ๊ณ  ์š”์ฒญํ•œ๋‹ค๋ฉด

์œ„์ฒ˜๋Ÿผ ์ฟผ๋ฆฌ๋ฅผ ์„ค๊ณ„ํ•˜์—ฌ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ํŠน์ • ์›”์˜ ๊ณตํœด์ผ ๋‚ ์งœ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๋‚ ์งœ๋ฅผ ์ œ๊ณตํ•œ๋‹ค๋ฉด ํ”„๋ก ํŠธ ์ชฝ์—์„œ ์ด ๋‚ ์งœ๋Š” ์˜ˆ์•ฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋„๋ก ์†์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”.

 

 

Comments