Skip to content
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

[Feat] #49 - Moya 초기세팅 #62

Merged
merged 5 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions Spark-iOS/Spark-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
F8096F3027841F9200B71D38 /* HomeVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F2F27841F9200B71D38 /* HomeVC.swift */; };
F8096F3227841FE100B71D38 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F3127841FE100B71D38 /* ViewController.swift */; };
F8096F36278420FA00B71D38 /* TempProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F35278420FA00B71D38 /* TempProtocol.swift */; };
F8096F382784210000B71D38 /* TempService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F372784210000B71D38 /* TempService.swift */; };
F8096F3A2784210A00B71D38 /* TempModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F392784210A00B71D38 /* TempModel.swift */; };
F8096F3C2784211D00B71D38 /* UIColor+.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F3B2784211D00B71D38 /* UIColor+.swift */; };
F8096F3E2784213700B71D38 /* UIFont+.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F3D2784213700B71D38 /* UIFont+.swift */; };
F8096F442784221900B71D38 /* Xib.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8096F432784221900B71D38 /* Xib.swift */; };
Expand All @@ -87,6 +85,14 @@
F82B2E0B278EBC4400219628 /* UserDefaultsKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82B2E0A278EBC4400219628 /* UserDefaultsKey.swift */; };
F82B2E0E278ED01F00219628 /* Splash.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F82B2E0D278ED01F00219628 /* Splash.storyboard */; };
F82B2E12278F54CD00219628 /* SplashVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82B2E11278F54CD00219628 /* SplashVC.swift */; };
F82F57FB27928437003E4174 /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F57FA27928437003E4174 /* NetworkResult.swift */; };
F82F57FD2792847D003E4174 /* GenericResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F57FC2792847D003E4174 /* GenericResponse.swift */; };
F82F57FF27928588003E4174 /* URL.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F57FE27928588003E4174 /* URL.swift */; };
F82F5802279287FD003E4174 /* HabitRoom.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F5801279287FD003E4174 /* HabitRoom.swift */; };
F82F580427928975003E4174 /* MoyaLoggerPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F580327928975003E4174 /* MoyaLoggerPlugin.swift */; };
F82F5808279289B2003E4174 /* HomeAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F5807279289B2003E4174 /* HomeAPI.swift */; };
F82F580A279289C4003E4174 /* HomeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F5809279289C4003E4174 /* HomeService.swift */; };
F82F580C27929F74003E4174 /* Header.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82F580B27929F74003E4174 /* Header.swift */; };
F867B6B6278F86610024B1D4 /* UINavigationController+.swift in Sources */ = {isa = PBXBuildFile; fileRef = F867B6B5278F86610024B1D4 /* UINavigationController+.swift */; };
F8FAA9722790344D00C4190F /* HomeHabitCVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FAA9702790344D00C4190F /* HomeHabitCVC.swift */; };
F8FAA9732790344D00C4190F /* HomeHabitCVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = F8FAA9712790344D00C4190F /* HomeHabitCVC.xib */; };
Expand Down Expand Up @@ -162,8 +168,6 @@
F8096F2F27841F9200B71D38 /* HomeVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeVC.swift; sourceTree = "<group>"; };
F8096F3127841FE100B71D38 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
F8096F35278420FA00B71D38 /* TempProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempProtocol.swift; sourceTree = "<group>"; };
F8096F372784210000B71D38 /* TempService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempService.swift; sourceTree = "<group>"; };
F8096F392784210A00B71D38 /* TempModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempModel.swift; sourceTree = "<group>"; };
F8096F3B2784211D00B71D38 /* UIColor+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+.swift"; sourceTree = "<group>"; };
F8096F3D2784213700B71D38 /* UIFont+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+.swift"; sourceTree = "<group>"; };
F8096F432784221900B71D38 /* Xib.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Xib.swift; sourceTree = "<group>"; };
Expand All @@ -179,6 +183,14 @@
F82B2E0A278EBC4400219628 /* UserDefaultsKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaultsKey.swift; sourceTree = "<group>"; };
F82B2E0D278ED01F00219628 /* Splash.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Splash.storyboard; sourceTree = "<group>"; };
F82B2E11278F54CD00219628 /* SplashVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashVC.swift; sourceTree = "<group>"; };
F82F57FA27928437003E4174 /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = "<group>"; };
F82F57FC2792847D003E4174 /* GenericResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenericResponse.swift; sourceTree = "<group>"; };
F82F57FE27928588003E4174 /* URL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URL.swift; sourceTree = "<group>"; };
F82F5801279287FD003E4174 /* HabitRoom.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HabitRoom.swift; sourceTree = "<group>"; };
F82F580327928975003E4174 /* MoyaLoggerPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoyaLoggerPlugin.swift; sourceTree = "<group>"; };
F82F5807279289B2003E4174 /* HomeAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeAPI.swift; sourceTree = "<group>"; };
F82F5809279289C4003E4174 /* HomeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeService.swift; sourceTree = "<group>"; };
F82F580B27929F74003E4174 /* Header.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Header.swift; sourceTree = "<group>"; };
F8368FAA278F5DFF00110B74 /* Spark-iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Spark-iOS.entitlements"; sourceTree = "<group>"; };
F867B6B5278F86610024B1D4 /* UINavigationController+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UINavigationController+.swift"; sourceTree = "<group>"; };
F8FAA9702790344D00C4190F /* HomeHabitCVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeHabitCVC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -504,6 +516,8 @@
F8096F432784221900B71D38 /* Xib.swift */,
F8096F452784247100B71D38 /* Notification.swift */,
F82B2E0A278EBC4400219628 /* UserDefaultsKey.swift */,
F82F57FE27928588003E4174 /* URL.swift */,
F82F580B27929F74003E4174 /* Header.swift */,
);
path = Constants;
sourceTree = "<group>";
Expand Down Expand Up @@ -584,15 +598,18 @@
F8096F2027841DCC00B71D38 /* NetworkServices */ = {
isa = PBXGroup;
children = (
F8096F372784210000B71D38 /* TempService.swift */,
F82F58052792897B003E4174 /* Plugin */,
F82F5806279289AC003E4174 /* Home */,
);
path = NetworkServices;
sourceTree = "<group>";
};
F8096F2127841DD600B71D38 /* NetworkModels */ = {
isa = PBXGroup;
children = (
F8096F392784210A00B71D38 /* TempModel.swift */,
F82F57FA27928437003E4174 /* NetworkResult.swift */,
F82F57FC2792847D003E4174 /* GenericResponse.swift */,
F82F5800279287EC003E4174 /* Home */,
);
path = NetworkModels;
sourceTree = "<group>";
Expand Down Expand Up @@ -676,6 +693,31 @@
path = Splash;
sourceTree = "<group>";
};
F82F5800279287EC003E4174 /* Home */ = {
isa = PBXGroup;
children = (
F82F5801279287FD003E4174 /* HabitRoom.swift */,
);
path = Home;
sourceTree = "<group>";
};
F82F58052792897B003E4174 /* Plugin */ = {
isa = PBXGroup;
children = (
F82F580327928975003E4174 /* MoyaLoggerPlugin.swift */,
);
path = Plugin;
sourceTree = "<group>";
};
F82F5806279289AC003E4174 /* Home */ = {
isa = PBXGroup;
children = (
F82F5807279289B2003E4174 /* HomeAPI.swift */,
F82F5809279289C4003E4174 /* HomeService.swift */,
);
path = Home;
sourceTree = "<group>";
};
F8FAA96B2790336E00C4190F /* Home */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -842,7 +884,8 @@
2B985205279031D000CE40A7 /* CreateRoomVC.swift in Sources */,
2B69E7E5278E22F4000F927F /* FeedHeaderView.swift in Sources */,
F80A3E55278C1C1F00728E07 /* StorageVC.swift in Sources */,
F8096F3A2784210A00B71D38 /* TempModel.swift in Sources */,
2B9852102790AE1500CE40A7 /* TimerAuthView.swift in Sources */,
F82F580C27929F74003E4174 /* Header.swift in Sources */,
F8096F3C2784211D00B71D38 /* UIColor+.swift in Sources */,
2BE5D818279330A6007A544D /* TimerAuthView.swift in Sources */,
2B98520B2790AD0000CE40A7 /* CreateAuthVC.swift in Sources */,
Expand All @@ -856,9 +899,11 @@
EB9C508A2792A86D00588155 /* CompleteAuthVC.swift in Sources */,
F8096F442784221900B71D38 /* Xib.swift in Sources */,
EB625E8A278F29D300C43DE9 /* CarouselLayout.swift in Sources */,
F82F57FF27928588003E4174 /* URL.swift in Sources */,
EB9C509827931E4600588155 /* AuthUploadVC.swift in Sources */,
2B9C6BCE2792781F00CD914F /* GoalWritingVC.swift in Sources */,
F80A3E4F278C18BA00728E07 /* MainTBC.swift in Sources */,
F82F57FD2792847D003E4174 /* GenericResponse.swift in Sources */,
F80A3E57278C1C2700728E07 /* FeedVC.swift in Sources */,
EB9C50842792A61D00588155 /* HabitAuthVC.swift in Sources */,
2BC17277278F765C00BA3029 /* WaitingFriendCVC.swift in Sources */,
Expand All @@ -869,12 +914,16 @@
EB9C507D27922F2E00588155 /* LineAnimationView.swift in Sources */,
EB625E7B278F271400C43DE9 /* DoingStorageCVC.swift in Sources */,
F816F125278E1C920008ED00 /* LoginVC.swift in Sources */,
F8096F382784210000B71D38 /* TempService.swift in Sources */,
F8096F3E2784213700B71D38 /* UIFont+.swift in Sources */,
F82F580A279289C4003E4174 /* HomeService.swift in Sources */,
EB625E95278F8E7200C43DE9 /* MoreStorageCVC.swift in Sources */,
F82F57FB27928437003E4174 /* NetworkResult.swift in Sources */,
F82F5802279287FD003E4174 /* HabitRoom.swift in Sources */,
F82B2E12278F54CD00219628 /* SplashVC.swift in Sources */,
F82F580427928975003E4174 /* MoyaLoggerPlugin.swift in Sources */,
F8096F2927841F1E00B71D38 /* Const.swift in Sources */,
F8096F3027841F9200B71D38 /* HomeVC.swift in Sources */,
F82F5808279289B2003E4174 /* HomeAPI.swift in Sources */,
EBA93A7F279080E2009AE771 /* JoinCheckVC.swift in Sources */,
EB625E8F278F8E1900C43DE9 /* StorageMoreListDataModel.swift in Sources */,
F8096F462784247100B71D38 /* Notification.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Spark-iOS/Spark-iOS/Resource/Constants/Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ struct Const {
Const.Xib.NibName.mainCVC 과 같이 사용.
Notification.Name 을 extension 했기 때문에 .sendData 와 같이 사용가능.
*/

static let accessToken = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken) ?? ""
}
18 changes: 18 additions & 0 deletions Spark-iOS/Spark-iOS/Resource/Constants/Header.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Header.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/15.
//

import Foundation

extension Const {
struct Header {
/// Content-Type: application/json
static var basicHeader = ["Content-Type": "application/json"]
/// access token 을 헤더에 담아서 보내야하는 경우에 사용.
static var authrizationHeader = ["Content-Type": "application/json",
"Authorization": accessToken]
}
}
14 changes: 14 additions & 0 deletions Spark-iOS/Spark-iOS/Resource/Constants/URL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// URL.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/15.
//

import Foundation

extension Const {
struct URL {
static let baseURL = "https://asia-northeast3-we-sopt-spark.cloudfunctions.net/api"
}
}
18 changes: 18 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkModels/GenericResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// GenericResponse.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/15.
//

import Foundation

struct GenericResponse<T: Codable>: Codable {
let statusCode: Int
let success, message: String
let data: T?

enum CodingKeys: String, CodingKey {
case statusCode, success, message, data
}
}
30 changes: 30 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkModels/Home/HabitRoom.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// HabitRoom.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/15.
//

import Foundation

// MARK: - HabitRoom
struct HabitRoom: Codable {
let rooms: [Room]
}

// MARK: - Room
struct Room: Codable {
let roomID: Int
let roomName: String
let leftDay: Int?
let profileImg: [String]?
let life: Int?
// isStarted: 습관방 시작했는지 여부. isDone: 인증 완료 여부.
let isStarted, isDone: Bool
let memberNum, doneMemberNum: Int?

enum CodingKeys: String, CodingKey {
case roomID = "roomId"
case roomName, leftDay, profileImg, life, isStarted, isDone, memberNum, doneMemberNum
}
}
16 changes: 16 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkModels/NetworkResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NetworkResult.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/15.
//

import Foundation

enum NetworkResult<T> {
case success(T) // 서버 통신 성공
case requestErr(T) // 요청 에러 발생
case pathErr // 경로 에러
case serverErr // 서버의 내부적 에러
case networkFail // 네트워크 연결 실패
}
8 changes: 0 additions & 8 deletions Spark-iOS/Spark-iOS/Source/NetworkModels/TempModel.swift

This file was deleted.

71 changes: 71 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkServices/Home/HomeAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// HomeAPI.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/15.
//

import Foundation
import Moya

public class UserAPI {

static let shared = UserAPI()
var userProvider = MoyaProvider<HomeService>(plugins: [MoyaLoggerPlugin()])

public init() { }

func habitRoomFetch(lastID: Int, size: Int, completion: @escaping (NetworkResult<Any>) -> Void) {
userProvider.request(.habitRoomFetch(lastID: lastID, size: size)) { (result) in
switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data

let networkResult = self.judgeHabitRoomFetchStatus(by: statusCode, data)
completion(networkResult)

case .failure(let err):
print(err)
}
}
}

private func judgeHabitRoomFetchStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {

let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<HabitRoom>.self, from: data)
else {
return .pathErr
}

switch statusCode {
case 200:
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.message)
case 500:
return .serverErr
default:
return .networkFail
}
}

/// 응답 시 data 가 없는 경우.
private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<String>.self, from: data)
else { return .pathErr }

switch statusCode {
case 200:
return .success(decodedData.message)
case 400..<500:
return .requestErr(decodedData.message)
case 500:
return .serverErr
default:
return .networkFail
}
}
}
51 changes: 51 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkServices/Home/HomeService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// HomeService.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/15.
//

import Foundation
import Moya

enum HomeService {
case habitRoomFetch(lastID: Int, size:Int)
}

extension HomeService: TargetType {
var baseURL: URL {
return URL(string: Const.URL.baseURL)!
}

var path: String {
switch self {
case .habitRoomFetch:
return "/room"
}
}

var method: Moya.Method {
switch self {
case .habitRoomFetch:
return .get
}
}

var task: Task {
switch self {
case .habitRoomFetch(let lastID, let size):
return .requestParameters(parameters: ["lastid": lastID,
"size": size],
encoding: URLEncoding.queryString)
}
}

var headers: [String : String]? {
switch self {
case .habitRoomFetch:
return Const.Header.authrizationHeader
}
}


}
Loading