-
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 #32 from BE-SOPT-Collaboration-Seminar-Coinone/fea…
…tuer/#27 🎨 Network 통신 준비 완료
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 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
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,49 @@ | ||
// | ||
// APIConstants.swift | ||
// Coinone-iOS | ||
// | ||
// Created by 노한솔 on 2021/06/02. | ||
// | ||
|
||
import Foundation | ||
|
||
struct APIConstants { | ||
|
||
static let baseURL = "http://3.37.86.93:5000" | ||
|
||
static let addCoinURL = baseURL + "/api/add-coin" | ||
static let addUserURL = baseURL + "/api/add-user/" | ||
static let myCoinURL = baseURL + "/api/my-coin/" | ||
static let coinListURL = baseURL + "/api/coin-list/" | ||
} | ||
|
||
struct APIIndex { | ||
|
||
var index: Index | ||
|
||
enum Index { | ||
case id(String) | ||
case userId(String) | ||
case sort(String, String) | ||
|
||
func getIndex() -> String { | ||
switch self { | ||
case .id(let index): | ||
return "/\(index)" | ||
case .userId(let index): | ||
return "/\(index)" | ||
case .sort(let sort, let ascending): | ||
return "/\(sort)/\(ascending)" | ||
} | ||
} | ||
} | ||
} | ||
|
||
// add-user | ||
// let url = APIConstants.addUserURL + APIIndex.init(index: .userID(parameterName)).index.getIndex() | ||
|
||
// my-coin | ||
// let url = APIConstants.myCoinURL + APIIndex.init(index: .id(parameterName)).index.getIndex() | ||
|
||
// coin-list | ||
// let url = APIConstants.coinListURL + APIIndex.init(index: .sort(parameter1, parameter2)).index.getIndex() |
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,16 @@ | ||
// | ||
// NetworkResult.swift | ||
// Coinone-iOS | ||
// | ||
// Created by 노한솔 on 2021/06/02. | ||
// | ||
|
||
import Foundation | ||
|
||
enum NetworkResult<T> { | ||
case success | ||
case requestErr | ||
case pathErr | ||
case serverErr | ||
case networkFail | ||
} |
49 changes: 49 additions & 0 deletions
49
Coinone-iOS/Coinone-iOS/Resource/NetworkServices/CoinFilterService.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,49 @@ | ||
// | ||
// CoinFilterService.swift | ||
// Coinone-iOS | ||
// | ||
// Created by 노한솔 on 2021/06/02. | ||
// | ||
|
||
import Alamofire | ||
import Foundation | ||
|
||
struct CoinFilterService { | ||
static let shared = CoinFilterService() | ||
|
||
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) | ||
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> { | ||
let decoder = JSONDecoder() | ||
guard let decodedData = try? decoder.decode(SortedCoin.self, from: data) else {return .pathErr} | ||
switch statusCode { | ||
case 200: return .success | ||
case 400: return .requestErr | ||
case 500: return .serverErr | ||
default: return .networkFail | ||
} | ||
} | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
Coinone-iOS/Coinone-iOS/Source/Models/SortedCoinModel.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,30 @@ | ||
// | ||
// SortedCoinModel.swift | ||
// Coinone-iOS | ||
// | ||
// Created by 노한솔 on 2021/06/02. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - SortedCoin | ||
struct SortedCoin: Codable { | ||
let success: Bool | ||
let data: [SortedData] | ||
} | ||
|
||
// MARK: - SortedData | ||
struct SortedData: Codable { | ||
let id: String | ||
let coinLogoImage: String | ||
let coinEnglishTitle, coinKoreanTitle, coinCurrentPrice, riseOrDescent: String | ||
let degree, percentage, coinTotalPrice: String | ||
let graphImage: String | ||
let v: Int | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case id = "_id" | ||
case coinLogoImage, coinEnglishTitle, coinKoreanTitle, coinCurrentPrice, riseOrDescent, degree, percentage, coinTotalPrice, graphImage | ||
case v = "__v" | ||
} | ||
} |