개발일기

(2) OttDaDam 헌옷 수거함 공공 API 본문

OttDaDam 서울시 헌옷수거함 project

(2) OttDaDam 헌옷 수거함 공공 API

한둥둥 2023. 8. 2. 22:40

공공 데이터 불러와서 해주는 부분의 코드 리팩토링을 조금 하였다. 

public static List<String> selectApiLocation(String location,String openApiKey) throws URISyntaxException, ParseException {
        switch (location)
        {
            case "guro":
                openApiManagerDto = OpenApiManagerDto.builder()
                                                        .BaseUrl(API_URL)
                                                        .serviceKey(openApiKey)
                                                        .page(START_PAGE)
                                                        .perPage(END_PAGE)
                                                        .uddi("672059d4-1830-44af-97dd-cf0954b2ee86")
                                                        .locationNumber(15068871)
                                                        .build();
                apiData = (ArrayList<String>) openApiManagerDto.getAllFetch();
                break;
            case "yangcheon" :
                openApiManagerDto = OpenApiManagerDto.builder()
                                                        .BaseUrl(API_URL)
                                                        .serviceKey(openApiKey)
                                                        .page(START_PAGE)
                                                        .perPage(END_PAGE)
                                                        .uddi("ce842bd1-877f-41b0-b612-81bb77bdbb1d")
                                                        .locationNumber(15105196)
                                                        .build();
                apiData = (ArrayList<String>) openApiManagerDto.getAllFetch();
                break;
            case "jongno" :
                openApiManagerDto = OpenApiManagerDto.builder()
                                                        .BaseUrl(API_URL)
                                                        .serviceKey(openApiKey)
                                                        .page(START_PAGE)
                                                        .perPage(END_PAGE)
                                                        .uddi("94ddbdca-b180-4156-b4f0-8048116792f9")
                                                        .locationNumber(15104622)
                                                        .build();
                apiData = (ArrayList<String>) openApiManagerDto.getAllFetch();
                break;
            case "gwanak" :
                openApiManagerDto = OpenApiManagerDto.builder()
                        .BaseUrl(API_URL)
                        .serviceKey(openApiKey)
                        .page(START_PAGE)
                        .perPage(END_PAGE)
                        .uddi("6dec2a8d-6404-4318-8767-85419b3c45a0")
                        .locationNumber(15076398)
                        .build();
                apiData = (ArrayList<String>) openApiManagerDto.getAllFetch();
                break;
            case "dongjak" :
                openApiManagerDto = OpenApiManagerDto.builder()
                                                        .BaseUrl(API_URL)
                                                        .serviceKey(openApiKey)
                                                        .page(START_PAGE)
                                                        .perPage(END_PAGE)
                                                        .uddi("05be3e28-de40-426b-9198-e4ca5b3ceee7")
                                                        .locationNumber(15068021)
                                                        .build();
                apiData = (ArrayList<String>) openApiManagerDto.getAllFetch();
                break;
            case "gwangjin" :
                openApiManagerDto = OpenApiManagerDto.builder()
                                                        .BaseUrl(API_URL)
                                                        .serviceKey(openApiKey)
                                                        .page(START_PAGE)
                                                        .perPage(END_PAGE)
                                                        .uddi("d63e68bf-e03d-4d3c-a203-fd9add3d372c")
                                                        .locationNumber(15109594)
                                                        .build();
                apiData = (ArrayList<String>) openApiManagerDto.getAllFetch();
                break;
        }
        return apiData;
    }

Builder패턴과 상수로 사용할 수 있을 만한 부분의 코드들은 빼주었다.  또한 Spring properties값을 설정해주어 API_KEY를 숨겨주었다.

각각의 중복되는 부분을 함수로 따로 빼줄까 고민중인데 해당 작업은 추후에 할 예정이다. 

    @Value("${data.apikey}")
    private String openApiKey;
    // URI형식의 OpenApiData형식을 가져온다.
    public List<String> getOpenApiUriData() throws URISyntaxException, ParseException {
        List<String> openApiUriList = new ArrayList<>();
        String[] locations = {"guro","yangcheon","jongno","gwanak","dongjak","gwangjin"};
        for(String location : locations){
            openApiUriList.addAll(CmnUtil.selectApiLocation(location, openApiKey));
            LOGGER.info("LOGGER::");
        }

        return openApiUriList;
    }

openApiKey를 받을 수 있게 만들어주었다. 

 

# openApiServiceKey
data.apikey = OpenApiServiceKey

application.properties 코드이다. OpenApiServiceKey 쪽에 공공데이터 키값을 넣어주면 된다.