본문 바로가기
C#

[C# ] 요소수 중점 유통 주유소 재고현황 API 조회

by Jcoder 2021. 11. 20.

API 조회 결과

 

데이터 상세 | 공공데이터포털 (data.go.kr)

 

공공데이터활용지원센터_요소수 중점 유통 주유소 재고현황_20211120

전국의 중점 유통 주유소의 요소수 재고 현황은 매일 2회 업데이트 됩니다. (12시 기준 재고현황 : 14시, 18시 기준 재고현황 : 20시)<br/> * 현재 버전 11월20일 12시 기준 업데이트 완료

www.data.go.kr

해당 페이지에서 활용신청 후 마이페이지에서 들어가면 아래처럼 일반 인증키가 나온다.

인증키

Header와 Query에서 인증을 두번 해야한다.

System.Net.Http.HttpClient httpClient = new();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("개인키"); // 헤더에 인증키 추가
"https://api.odcloud.kr/api/15094782/v1/uddi:6b2017af-659d-437e-a549-c59788817675?page={0}&perPage={1}&serviceKey={2}"

 

코드

Program.cs

// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
using Diesel_exhaust_fluid_API_console;

const string GET_URL = "https://api.odcloud.kr/api/15094782/v1/uddi:6b2017af-659d-437e-a549-c59788817675?page={0}&perPage={1}&serviceKey={2}";
string privateApiKey = "개인키";

System.Net.Http.HttpClient httpClient = new();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(privateApiKey);

string Get_Api_Url = string.Format(GET_URL, 1, 1000, privateApiKey);
string responseResult = await httpClient.GetStringAsync(Get_Api_Url);
if (!string.IsNullOrEmpty(responseResult))
{
    var deserializeAsyncResult = System.Text.Json.JsonSerializer.Deserialize<DieselExhaustFluid>(responseResult);

    Console.WriteLine(deserializeAsyncResult);
}
else
    Console.WriteLine("데이터 없음.");

DieselExhaustFluid.cs

using System.Text;
using System.Text.Json.Serialization;

namespace Diesel_exhaust_fluid_API_console
{
    public class DieselExhaustFluid
    {
        [JsonPropertyName("page")]
        public int Page { get; set; }
        [JsonPropertyName("perPage")]
        public int PerPage { get; set; }
        [JsonPropertyName("totalCount")]
        public int TotalCount { get; set; }
        [JsonPropertyName("currentCount")]
        public int CurrentCount { get; set; }
        [JsonPropertyName("data")]
        public List<DieselExhaustFluidData> DieselExhaustFluidDataList { get; set; } = new();

        public override string ToString()
        {
            StringBuilder stringBuilder = new();
            foreach (var dieselExhaustFluidData in DieselExhaustFluidDataList)
                stringBuilder.Append(dieselExhaustFluidData);
            return $"{stringBuilder}{Environment.NewLine}";
        }
    }
}

DieselExhaustFluidData.cs

using System.Text.Json.Serialization;

namespace Diesel_exhaust_fluid_API_console
{
    public class DieselExhaustFluidData
    {
        [JsonPropertyName("코드")]
        public string? 코드 { get; set; }
        [JsonPropertyName("명칭")]
        public string? 명칭 { get; set; }
        [JsonPropertyName("주소")]
        public string? 주소 { get; set; }
        [JsonPropertyName("전화번호")]
        public string? 전화번호 { get; set; }
        [JsonPropertyName("영업시간")]
        public string? 영업시간 { get; set; }
        [JsonPropertyName("재고량")]
        public int 재고량 { get; set; }
        [JsonPropertyName("가격")]
        public string? 가격 { get; set; }
        [JsonPropertyName("위도")]
        public string? 위도 { get; set; }
        [JsonPropertyName("경도")]
        public string? 경도 { get; set; }
        [JsonPropertyName("데이터기준일")]
        public string? 데이터기준일 { get; set; }

        public override string ToString() => $"데이터기준일 : {데이터기준일}, 코드 : {코드}, 명칭 : {명칭}, 주소 : {주소}, 재고량 : {재고량}, 가격 : {가격}, 전화번호 : {전화번호}, 영업시간 : {영업시간}{Environment.NewLine}";
    }
}