๋ฐ˜์‘ํ˜•

์†Œ๊ฐœ

์ €๋Š” ๋จธ์‹  ๋น„์ „ ๋ถ„์•ผ์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๊ณ , ์ฃผ๋กœ ๋น„์ „ ๊ด€๋ จ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋งค์ผ ํ•˜๋˜ ๊ฒƒ๋งŒ ๊ฐœ๋ฐœํ•˜๋‹ค ๋ณด๋‹ˆ ์ง€๋ฃจํ•˜๊ธฐ๋„ ํ•˜๊ณ , ๊ฐœ๋ฐœ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ๋„ˆ๋ฌด ํญ์ด ์ข์€ ๋Š๋‚Œ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ… ๋ฐ ์ทจ๋ฏธ๋กœ ์ด๊ฒƒ์ €๊ฒƒ ๊ฐœ๋ฐœํ•˜๋‹ค ๋ณด๋‹ˆ, ์ตœ๊ทผ์—๋Š” Open API๊นŒ์ง€ ๊ฑด๋“œ๋ฆฌ๊ฒŒ ๋์Šต๋‹ˆ๋‹ค.

์˜ˆ์ „์—๋„ Open API ๋ผ๋“ ๊ฐ€ REST API๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋“ค์–ด๋ณด๊ธด ํ–ˆ์ง€๋งŒ, ์‚ฌ์šฉํ•ด๋ณผ ๊ธฐํšŒ๋„ ์—†๊ณ  ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋Š”์ง€๋„ ๋ชฐ๋ž์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ง‰์ƒ ๋„์ „ํ•ด๋ณด๋‹ˆ ํฐ ์–ด๋ ค์›€์ด ์—†๋”๊ตฐ์š”. ์˜คํžˆ๋ ค ๊ฐ€์ด๋“œ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ง‰ํžˆ๋Š” ๊ฒƒ ์—†์ด ๋ฐ”๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์ €์ฒ˜๋Ÿผ ์ฒ˜์Œ Open API๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ถ„๋“ค์—๊ฒŒ ์ข‹์€ ๊ฐ€์ด๋“œ๊ฐ€ ๋˜๊ธธ ๋ฐ”๋ผ๋ฉด์„œ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์˜ ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ๋Š” Open API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ณ ์†๋„๋กœ ๊ตํ†ต์˜ˆ๋ณด ํ˜„ํ™ฉ ์ •๋ณด๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

REST, REST API, Open API?

Open API๋ฅผ ๋‹ค๋ค„๋ณด๋ฉด์„œ ์ž์ฃผ ์ ‘ํ–ˆ๋˜ ๋‹จ์–ด๊ฐ€ RESTful ๋ฐ REST API ์˜€์Šต๋‹ˆ๋‹ค. ์ธํ„ฐ๋„ท์— ๊ฒ€์ƒ‰ํ•ด๋ณด๋ฉด, ๊ฐ™์€ ์˜๋ฏธ๋ผ๋Š” ๊ธ€๋„ ์žˆ๊ณ  ๊ฒฐ๊ตญ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ํ˜•์‹์ด๋ผ๊ณ  ํ•˜๋”๊ตฐ์š”.

์œ„ํ‚คํ”ผ๋””์•„์—๋Š” REST๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

REST(Representational State Transfer)๋Š” WWW(World Wide Web)๊ณผ ๊ฐ™์€ ๋ถ„์‚ฐ ํ•˜์ดํผ๋ฏธ๋””์–ด ์‹œ์Šคํ…œ์„ ์œ„ํ•œ ์†Œํ”„ํŠธ์›จ์–ด ์•„ํ‚คํ…์ฒ˜์˜ ํ•œ ํ˜•์‹์ž…๋‹ˆ๋‹ค.

์ฆ‰, ์–ด๋– ํ•œ ์ž์›(๋ฌธ์„œ, ๋ฐ์ดํ„ฐ ๋“ฑ)์„ ์ด๋ฆ„์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฃผ๊ณ ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ JSON ๋˜๋Š” XML ํ˜•์‹์„ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ž‘๋…„์— ์ฝ”๋กœ๋‚˜ ๋•Œ๋ฌธ์— ๋งˆ์Šคํฌ ํ’ˆ๊ท€ ํ˜„์ƒ์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ, ์•ฝ๊ตญ๋งˆ๋‹ค ๋งˆ์Šคํฌ ๋ณด์œ  ์ˆ˜๋Ÿ‰์ด ์–ผ๋งˆ๋‚˜ ์žˆ๋Š”์ง€ Open API๋กœ ์ œ๊ณตํ–ˆ์—ˆ๋Š”๋ฐ์š”. ์ด๋ ‡๊ฒŒ ๊ตญ๊ฐ€์—์„œ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐœ๋ฐฉํ•˜์—ฌ ๊ตญ๋ฏผ๋“ค์ด ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด Open API์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๊ธฐ์—…์—์„œ ์ œ๊ณตํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ ํ•˜๊ธฐ

์šฐ์„ , ํ•œ๊ตญ ๋„๋กœ๊ณต์‚ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ณ ์†๋„๋กœ ๊ณต๊ณต๋ฐ์ดํ„ฐ ํฌํ„ธ ์‚ฌ์ดํŠธ๋ฅผ ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.

๋งํฌ: data.ex.co.kr/openapi/basicinfo/openApiInfoM?apiId=0303&serviceType=OPENAPI&keyWord=&searchDayFrom=2014.12.01&searchDayTo=2019.10.21&CATEGORY=&GROUP_TR=

 

๊ณ ์†๋„๋กœ ๊ณต๊ณต๋ฐ์ดํ„ฐ ํฌํ„ธ

 

data.ex.co.kr

์—ฌ๊ธฐ์„œ ๋ˆˆ์—ฌ๊ฒจ๋ณผ ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. HTTP GET๊ณผ HTTP POST๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ณ , ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋Š” XML ๋˜๋Š” JSON์ž…๋‹ˆ๋‹ค.

ํ•ญ๋ชฉ๋ช… ๊ฐ’
Request URL http://data.ex.co.kr/openapi/safeDriving/forecast
ํ˜ธ์ถœ ๋ฐฉ๋ฒ• HTTP GET, HTTP POST
๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํฌ๋งท XML, JSON

๊ทธ๋ฆฌ๊ณ  ์š”์ฒญ ๋ณ€์ˆ˜๋Š” key์™€ type ๋‘ ๊ฐœ์˜ string์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์š”์ฒญ ๋ณ€์ˆ˜ ๊ฐ’ ์กฐ๊ฑด ์„ค๋ช…
key string ํ•„์ˆ˜ ๋ฐœ๊ธ‰๋ฐ›์€ ์ธ์ฆํ‚ค
type string ํ•„์ˆ˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํฌ๋งท

ํŽ˜์ด์ง€์˜ ํ•˜๋‹จ์—๋Š” ์˜ˆ์ œ ์‹คํ–‰ํ•˜๊ธฐ ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์‹คํ–‰ํ•˜๊ธฐ ๋ฒ„ํŠผ

key๊ฐ€ ๊ฐ’์ด test์ด๋ฉด ๋ง ๊ทธ๋Œ€๋กœ ํ…Œ์ŠคํŠธ ์šฉ์ž…๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์‹ค์ œ ์„œ๋น„์Šค๋ฅผ ์šด์˜ํ•œ๋‹ค๋ฉด ์ธ์ฆํ‚ค ๋ฐœ๊ธ‰์„ ์š”์ฒญํ•˜๊ณ , ๊ทธ ํ‚ค๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. type์€ xml ๋˜๋Š” json ํŽธํ•œ ๊ฒƒ์œผ๋กœ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์„ ํƒ๋œ ํ•ญ๋ชฉ์— ๋”ฐ๋ผ URL์ด ์ƒ์„ฑ๋˜๊ณ , ์ด URL์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋ฉด ์ตœํ•˜๋‹จ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํšŒ์‹ ๋ฉ๋‹ˆ๋‹ค.

key์™€ type์— ๋”ฐ๋ฅธ URL ๋ฐ ๊ฒฐ๊ณผ

์ด์ œ C# ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จํžˆ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด C# Console๋กœ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค.

C# ์ฝ˜์†” ์•ฑ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ํ›„ ๋„๊ตฌ > NuGet ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž > ์†”๋ฃจ์…˜์šฉ NuGet ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ... ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Newtonsoft.Json์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

NuGet ํŒจํ‚ค์ง€ Newtonsoft.Json ์„ค์น˜

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์˜ namespace๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

using System.Diagnostics;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Net.Http;

๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  requestURL์€ ์ธ์ฆํ‚ค๊ฐ€ test์ด๊ณ , ํƒ€์ž…์€ json์ž…๋‹ˆ๋‹ค.

static string requestURL = "http://data.ex.co.kr/openapi/safeDriving/forecast?key=test&type=json";        

ํ•ด๋‹น ์ฃผ์†Œ์— json ํƒ€์ž…์˜ GET method๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.

WebRequest request = WebRequest.Create(requestURL);
request.Method = "GET";
request.ContentType = "application/json";

using (WebResponse response = request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
	string data = reader.ReadToEnd();
	//...
}                

๋งˆ์ง€๋ง‰์œผ๋กœ json ํƒ€์ž…์˜ ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

string data = reader.ReadToEnd();
var obj = JObject.Parse(data);
var list = obj["list"];

Console.WriteLine("============ List ============");
Console.WriteLine(obj);

Console.WriteLine("============ Item ============");
foreach (var item in list)
{
	Console.WriteLine(string.Format("{0}: {1}", "๋‚ ์งœ", item["sdate"]));
	Console.WriteLine(string.Format("{0}: {1}", "์‹œ๊ฐ„", item["stime"]));
	Console.WriteLine(string.Format("{0}: {1}", "์ „๊ตญ ๊ตํ†ต๋Ÿ‰", item["cjunkook"]));
	Console.WriteLine(string.Format("{0}: {1}", "์ง€๋ฐฉ ๋ฐฉํ–ฅ ๊ตํ†ต๋Ÿ‰", item["cjibangDir"]));
	Console.WriteLine(string.Format("{0}: {1}", "์„œ์šธ ๋ฐฉํ–ฅ ๊ตํ†ต๋Ÿ‰", item["cseoulDir"]));
	Console.WriteLine(string.Format("{0}: {1}", "์„œ์šธ->๋Œ€์ „ ์†Œ์š”์‹œ๊ฐ„", item["csudj"]));
	Console.WriteLine(string.Format("{0}: {1}", "์„œ์šธ->๋Œ€๊ตฌ ์†Œ์š”์‹œ๊ฐ„", item["csudg"]));
	Console.WriteLine(string.Format("{0}: {1}", "์„œ์šธ->์šธ์‚ฐ ์†Œ์š”์‹œ๊ฐ„", item["csuus"]));
	Console.WriteLine(string.Format("{0}: {1}", "์„œ์šธ->๋ถ€์‚ฐ ์†Œ์š”์‹œ๊ฐ„", item["csubs"]));
}

item["..."]์—๋Š” ์•„๋ž˜์˜ ์ถœ๋ ฅ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ˜•์‹

์•„์ฃผ ๊ฐ„๋‹จํ•˜์ฃ ? ์˜ˆ์ œ ์ฝ”๋“œ๋„ ์ฒจ๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

ExamOpenAPI.zip
4.92MB

๋ฐ˜์‘ํ˜•