-
Notifications
You must be signed in to change notification settings - Fork 6
[Refactor] #207 - 서버 통신 함수 리팩토링 #222
New issue
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
Merged
The head ref may contain hidden characters: "#207-\uB9AC\uD329\uD1A0\uB9C1"
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
Runnect-iOS/Runnect-iOS/Network/Service/NetworkProvider.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // | ||
| // NetworkProvider.swift | ||
| // Runnect-iOS | ||
| // | ||
| // Created by Sojin Lee on 2023/11/14. | ||
| // | ||
|
|
||
| import Moya | ||
|
|
||
| enum ResponseResult<T> { | ||
| case success(T) | ||
| } | ||
|
|
||
| class NetworkProvider<Provider : TargetType> : MoyaProvider<Provider> { | ||
| func request<Model : Codable>(target : Provider, instance : Model.Type , vc: UIViewController, completion : @escaping(ResponseResult<Model>) -> ()){ | ||
| self.request(target) { result in | ||
| switch result { | ||
| /// 서버 통신 성공 | ||
| case .success(let response): | ||
| if (200..<300).contains(response.statusCode) { | ||
| if let decodeData = try? JSONDecoder().decode(instance, from: response.data) { | ||
| completion(.success(decodeData)) | ||
| } else{ | ||
| /// decoding error | ||
| vc.showNetworkFailureToast() | ||
| } | ||
| } else { | ||
| /// 응답 코드 400인 경우 | ||
| vc.showNetworkFailureToast() | ||
| } | ||
| /// 서버 통신 실패 | ||
| case .failure(let error): | ||
| if let response = error.response { | ||
| if let responseData = String(data: response.data, encoding: .utf8) { | ||
| print(responseData) | ||
| } else { | ||
| print(error.localizedDescription) | ||
| } | ||
| } else { | ||
| print(error.localizedDescription) | ||
| } | ||
| vc.showNetworkFailureToast() | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확실하게 짧아지긴 했네요,
꽤 꼼꼼하게 봤는데도, 고칠 부분은 찾지 못했습니다.
따로
NetworkProvider클래스를 재사용하여 중복되는 코드들을 없애는 거는 초창기에 언급했던 거 같은데, 대부분 작업 끝나면 모두 바꿔봅시다~