Skip to content

Conversation

@MinwooJe
Copy link
Member

배경

기존의 completion 기반의 메서드들로 인해 가독성 저하, 디버깅 어려움 등의 문제가 있었습니다.
이를 해결하고자 Swift Concurrency를 도입했습니다.

작업 내용

1. NetworkError 케이스 수정

기존의 NetworkError의 case 이름이 발생한 에러를 표현하기 모호한 부분이 있었습니다.
예를 들어 requestFailed case는 네트워크 요청 실패의 원인보다 결과를 나타내므로, 에러 메시지 출력 시 정확한 원인을 파악하기 힘들었습니다.

따라서 각 케이스가 명확한 원인을 나타내도록 수정하고, 문서 주석을 달아 Quick Help로 확인할 수 있습니다.
또한 NetworkError가 CustomStringConvertible을 채택하도록 해 필요 시 description 프로퍼티로 에러 메시지를 출력할 수 있습니다.

2. 네트워크 레이어 리팩토링

Swift Concurrency를 이용한 request, upload 메서드를 구현했습니다.
각 메서드에서 중복되는 응답 검증, 디코딩 로직은 별도 메서드로 분리했습니다.

현재 request, upload 메서드는 NetworkManagerProtocol의 Extension으로 구현되어 있습니다.
NetworkManagerProtocol에 의존하는 레포지토리가 많기에, Swift Concurrency와 completion 방식 모두 사용할 수 있기 위함입니다.

JSONBodyRequestable 프로토콜의 익스텐션에 구현한 아래 메서드도 같은 이유입니다

makeURLRequest() throws -> (URLRequest, Data)

3. Requestable의 url 생성 메서드 수정

URLComponents.queryItems이 빈 배열일때 URLComponents의 url은 "https://host/path?"와 같이 url의 가장 마지막 부분에 ?가 붙습니다.
따라서 빈 배열이라면 nil을 할당하도록 수정해 ?가 해당 문제를 해결했습니다.

// 변경 전
    func makeURL() -> URL? {
        guard var components = URLComponents(string: baseURL) else { return nil }
        components.path = path
        components.queryItems = queryItems

        return components.url
    }
// 변경 후
    func makeURL() -> URL? {
        guard var components = URLComponents(string: baseURL) else { return nil }
        components.path = path
        components.queryItems = queryItems.isEmpty ? nil : queryItems

        return components.url
    }

테스트 방법

해당 메서드를 이용해 요청 보내시면 됩니다!

MinwooJe added 8 commits June 15, 2025 13:51
- body를 URLRequest에 넣지 않기 위해 extension으로 메서드 추가.
검증, 디코딩 로직을 각각 `validate()`, `decode` 메서드로 분리
- 서버 API 기본 형식에 대응되는 DefaultResponseDTO의 확장성을 위해
- 서버에서 내려준 에러 메시지 디코딩: validate 메서드에서 응답 실패 시 에러 메시지를 전파하기 위해
- 케이스 이름 구체화
- 에러 별 주석 작성
- `description` 추가
URLComponents.queryItems에 빈 배열이 들어가면 url의 가장마지막 부분에 ?가 붙음.
따라서 빈 배열이라면 nil을 할당하도록 수정해 ?가 불필요하게 붙는 문제 해결.
@MinwooJe MinwooJe self-assigned this Jun 16, 2025
@MinwooJe MinwooJe added the 👨‍🏭 Refactor 리팩토링 label Jun 16, 2025
Copy link
Contributor

@snughnu snughnu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다~~

@snughnu snughnu merged commit 06931c1 into GDSC-Popcorn:develop Jun 16, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants