We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extension APIService { func execute<T: APIRequest>(request: T) async throws -> T.APIResponse { guard let urlRequest = request.urlRequest else { throw APIError.invalidURL } let (data, response) = try await session.data(for: urlRequest) guard let response = response as? HTTPURLResponse, // 원하는 json데이터가 아닌 xml데이터가 200으로 도착 (200...299).contains(response.statusCode) else { print("\(T.self) failed to receive success response(status code: 200-299)") throw APIError.HTTPResponseFailure } guard let parsedData: T.APIResponse = parse(response: data) else { print("parsing failed. type: \(T.APIResponse.Type.self) ") throw APIError.invalidParsedData } return parsedData } }
application/json;charset=UTF-8
struct CountryCodeAPIService: APIService { var session: URLSession = URLSession(configuration: .default) func requestAllCountryCode() async throws -> [CountryCode] { let request = CountryCodeListRequest(pageNo: 1, numOfRows: 240) let countryCodeList: CountryCodeList = try await getResponse(request: request) return countryCodeList.data } func getResponse<T: APIRequest>(request: T) async throws -> T.APIResponse { guard let urlRequest = request.urlRequest else { throw APIError.invalidURL } let (data, response) = try await session.data(for: urlRequest) guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else { print("\(T.self) failed to receive success response(status code: 200-299)") throw APIError.HTTPResponseFailure } guard let contentType = response.value(forHTTPHeaderField: "Content-Type"), contentType == "application/json;charset=UTF-8" else { print("\(T.self) : response Content-Type is \(String(describing: response.value(forHTTPHeaderField: "Content-Type")))") throw APIError.HTTPResponseFailure } guard let parsedData: T.APIResponse = parse(response: data) else { print("parsing failed. type: \(T.APIResponse.Type.self) ") throw APIError.invalidParsedData } return parsedData } }
getResponse
protocol APIService { var session: URLSession { get set } func getResponse<T: APIRequest>(request: T) async throws -> T.APIResponse } protocol CombineAPIService: APIService { func getResponse<T: APIRequest>( request: T) -> AnyPublisher<T.APIResponse, Error> }
The text was updated successfully, but these errors were encountered:
👍REFACTOR: CountryCodeAPIService getResponse 응답 실패 처리 #30
d67900b
- 기존 APIService의 execute, getResponse 메서드는 OCP, SRP를 위반하므로 프로토콜로 인터페이스를 제공하기로 수정, 각 구현객체의 변경과 확장에 용이하도록 함 - execute를 getResponse로 리네임하여 동일한 역할을 하는 메서드임을 명시 - response의 header field의 Content-Type확인하는 로직 추가, json이 아닐시 에러 반환하도록 오류 처리
👍REFACTOR: percentEncodePlusSign 메서드 분리 #30
24909ab
👍REFACTOR: CombineAPIService 프로토콜 정의 및 채택 #30
1433241
- APIService에서 Combine 프레임워크와의 의존성을 분리하기 위해 APIService를 상속한 CombineAPIService 프로토콜 정의 - Combine을 사용하는 API에서만 해당 프로토콜을 채택하도록 구현
yeahg-dev
No branches or pull requests
🚨 문제 설명
✅ 해결 방법
문제 분석
해결 방법
application/json;charset=UTF-8
이 아니면 에러를 반환getResponse
메서드는 프로토콜 기본 구현으로 다른 Service에서도 사용되므로, 단일책임원칙과 개방폐쇄원칙을 위반한 코드임getResponse
를 프로토콜에 정의, 추상화하여 확장에 용이하도록 리팩터링👩🏻💻결과와 피드백
The text was updated successfully, but these errors were encountered: