-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from BE-SOPT-Collaboration-Seminar-Coinone/fea…
- Loading branch information
Showing
5 changed files
with
567 additions
and
485 deletions.
There are no files selected for viewing
This file contains 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
61 changes: 61 additions & 0 deletions
61
Coinone-iOS/Coinone-iOS/Resource/NetworkServices/CoinListService.swift
This file contains 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,61 @@ | ||
// | ||
// CoinListService.swift | ||
// Coinone-iOS | ||
// | ||
// Created by soyeon on 2021/06/10. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
struct CoinListService { | ||
static let shared = CoinListService() | ||
|
||
func sortCoin(sort: String, | ||
ascending: String, | ||
completion: @escaping (NetworkResult<Any>) -> Void) { | ||
let header: HTTPHeaders = ["Content-Type": "application/json"] | ||
let url = APIConstants.coinListURL + APIIndex.init(index: .sort(sort, ascending)).index.getIndex() | ||
let dataRequest = AF.request(url, | ||
method: .get, | ||
encoding: JSONEncoding.default, | ||
headers: header) | ||
dataRequest.responseData { dataResponse in | ||
dump(dataResponse) | ||
print(dataResponse.result) | ||
switch dataResponse.result { | ||
case .success: | ||
guard let statusCode = dataResponse.response?.statusCode else {return} | ||
guard let value = dataResponse.value else {return} | ||
let networkResult = self.judgeStatus(by: statusCode, value) | ||
completion(networkResult) | ||
case .failure: | ||
completion(.pathErr) | ||
} | ||
} | ||
} | ||
|
||
private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> { | ||
switch statusCode { | ||
case 200: | ||
return isValidData(data: data) | ||
case 400: return .requestErr | ||
case 500: return .serverErr | ||
default: return .networkFail | ||
} | ||
} | ||
|
||
private func isValidData(data : Data) -> NetworkResult<Any> { | ||
let decoder = JSONDecoder() | ||
|
||
guard let decodedData = try? decoder.decode([SortedCoin].self, from: data) | ||
else { return .pathErr} | ||
// 우선 PersonDataModel 형태로 decode(해독)을 한번 거칩니다. 실패하면 pathErr | ||
|
||
// 해독에 성공하면 Person data를 success에 넣어줍니다. | ||
return .success(decodedData) | ||
|
||
} | ||
} | ||
|
||
|
This file contains 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
This file contains 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
Oops, something went wrong.