Skip to content

Commit

Permalink
[Feat] GO-SOPT-iOS-Part#12 - moya 서버통신
Browse files Browse the repository at this point in the history
  • Loading branch information
kj9470 committed Jul 1, 2023
1 parent 4da1422 commit 19cb6ca
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 60 deletions.
38 changes: 37 additions & 1 deletion GO-SOPT-iOS/GO-SOPT-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
037C35D82A0E47DF00FAC78B /* AdvertisingModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 037C35D72A0E47DF00FAC78B /* AdvertisingModel.swift */; };
037C35DA2A0E487E00FAC78B /* AdvertisingCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 037C35D92A0E487E00FAC78B /* AdvertisingCollectionViewCell.swift */; };
038027672A4ECC9B0089986F /* Moya in Frameworks */ = {isa = PBXBuildFile; productRef = 038027662A4ECC9B0089986F /* Moya */; };
0380276C2A4ECF110089986F /* URLConst.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0380276B2A4ECF110089986F /* URLConst.swift */; };
0380276E2A4ECFAF0089986F /* NetworkLoggerPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0380276D2A4ECFAF0089986F /* NetworkLoggerPlugin.swift */; };
038027702A4ECFC50089986F /* GeneralResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0380276F2A4ECFC50089986F /* GeneralResponse.swift */; };
038027722A4ECFD50089986F /* APIConst.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038027712A4ECFD50089986F /* APIConst.swift */; };
038027742A4ED3850089986F /* Encodable +.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038027732A4ED3850089986F /* Encodable +.swift */; };
03983A8C29FE698700E5539D /* MyPageTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03983A8B29FE698700E5539D /* MyPageTableViewCell.swift */; };
03983A9029FE6D7B00E5539D /* MyPageListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03983A8F29FE6D7B00E5539D /* MyPageListModel.swift */; };
03983A9229FE74D500E5539D /* UITableView +.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03983A9129FE74D500E5539D /* UITableView +.swift */; };
Expand Down Expand Up @@ -108,6 +113,11 @@
037C35D52A0E42E900FAC78B /* LiveModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveModel.swift; sourceTree = "<group>"; };
037C35D72A0E47DF00FAC78B /* AdvertisingModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvertisingModel.swift; sourceTree = "<group>"; };
037C35D92A0E487E00FAC78B /* AdvertisingCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvertisingCollectionViewCell.swift; sourceTree = "<group>"; };
0380276B2A4ECF110089986F /* URLConst.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLConst.swift; sourceTree = "<group>"; };
0380276D2A4ECFAF0089986F /* NetworkLoggerPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkLoggerPlugin.swift; sourceTree = "<group>"; };
0380276F2A4ECFC50089986F /* GeneralResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralResponse.swift; sourceTree = "<group>"; };
038027712A4ECFD50089986F /* APIConst.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConst.swift; sourceTree = "<group>"; };
038027732A4ED3850089986F /* Encodable +.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Encodable +.swift"; sourceTree = "<group>"; };
03983A8B29FE698700E5539D /* MyPageTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageTableViewCell.swift; sourceTree = "<group>"; };
03983A8F29FE6D7B00E5539D /* MyPageListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageListModel.swift; sourceTree = "<group>"; };
03983A9129FE74D500E5539D /* UITableView +.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView +.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -351,7 +361,8 @@
036ED6192A0D42A0006A7F47 /* Foundation */ = {
isa = PBXGroup;
children = (
036ED61D2A0D4B3C006A7F47 /* NetworkResult.swift */,
0380276A2A4ECEFE0089986F /* Moya */,
038027692A4ECEED0089986F /* Alamofire */,
);
path = Foundation;
sourceTree = "<group>";
Expand Down Expand Up @@ -420,6 +431,25 @@
path = CreateNickname;
sourceTree = "<group>";
};
038027692A4ECEED0089986F /* Alamofire */ = {
isa = PBXGroup;
children = (
036ED61D2A0D4B3C006A7F47 /* NetworkResult.swift */,
);
path = Alamofire;
sourceTree = "<group>";
};
0380276A2A4ECEFE0089986F /* Moya */ = {
isa = PBXGroup;
children = (
0380276B2A4ECF110089986F /* URLConst.swift */,
0380276D2A4ECFAF0089986F /* NetworkLoggerPlugin.swift */,
0380276F2A4ECFC50089986F /* GeneralResponse.swift */,
038027712A4ECFD50089986F /* APIConst.swift */,
);
path = Moya;
sourceTree = "<group>";
};
03983A8A29FE695600E5539D /* Cell */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -564,6 +594,7 @@
03983A9329FE74F300E5539D /* UICollectionView +.swift */,
03983A9529FE750B00E5539D /* NSObject +.swift */,
037C35D02A0D5C0700FAC78B /* UIImageView +.swift */,
038027732A4ED3850089986F /* Encodable +.swift */,
);
path = Extension;
sourceTree = "<group>";
Expand Down Expand Up @@ -732,6 +763,9 @@
03CD4E9029E29511002BF3E0 /* Color.swift in Sources */,
036A1C2B29FC16CC00B33403 /* MyPageView.swift in Sources */,
03CD4E6A29DB06AD002BF3E0 /* UIView +.swift in Sources */,
0380276E2A4ECFAF0089986F /* NetworkLoggerPlugin.swift in Sources */,
038027722A4ECFD50089986F /* APIConst.swift in Sources */,
0380276C2A4ECF110089986F /* URLConst.swift in Sources */,
03CD4E9A29E29BAF002BF3E0 /* UIFont +.swift in Sources */,
03753B9E29E3EC9500B4F2A2 /* SignInViewController.swift in Sources */,
036A1C1529FA5B2000B33403 /* BaseViewController.swift in Sources */,
Expand All @@ -741,10 +775,12 @@
03753BB829E5B28300B4F2A2 /* WelcomeViewController.swift in Sources */,
036A1C2D29FC342E00B33403 /* UILabel +.swift in Sources */,
03CD4E4929DAFE76002BF3E0 /* SceneDelegate.swift in Sources */,
038027742A4ED3850089986F /* Encodable +.swift in Sources */,
03983AA12A013FD600E5539D /* PageCollectionViewCell.swift in Sources */,
03CD4E6C29DB0F07002BF3E0 /* SecondViewController.swift in Sources */,
03753BA329E3F2FA00B4F2A2 /* UIButton +.swift in Sources */,
036A1C1829FA64D100B33403 /* MyPageViewController.swift in Sources */,
038027702A4ECFC50089986F /* GeneralResponse.swift in Sources */,
0371AEA529EEABD80036932B /* UIViewController +.swift in Sources */,
036A1C2929FC12C700B33403 /* BuyTicketView.swift in Sources */,
036A1C2729FBF90900B33403 /* MyTicketView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "03CD4E4229DAFE76002BF3E0"
BuildableName = "GO-SOPT-iOS.app"
BlueprintName = "GO-SOPT-iOS"
ReferencedContainer = "container:GO-SOPT-iOS.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "03CD4E4229DAFE76002BF3E0"
BuildableName = "GO-SOPT-iOS.app"
BlueprintName = "GO-SOPT-iOS"
ReferencedContainer = "container:GO-SOPT-iOS.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "03CD4E4229DAFE76002BF3E0"
BuildableName = "GO-SOPT-iOS.app"
BlueprintName = "GO-SOPT-iOS"
ReferencedContainer = "container:GO-SOPT-iOS.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
60 changes: 58 additions & 2 deletions GO-SOPT-iOS/GO-SOPT-iOS/Data/DTO/Content/ContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation
// MARK: - ContentResponse
struct ContentResponse: Codable {
let page: Int
let results: [Result]
let results: [ContentResult]
let totalPages, totalResults: Int

enum CodingKeys: String, CodingKey {
Expand All @@ -23,7 +23,7 @@ struct ContentResponse: Codable {
}

// MARK: - Result
struct Result: Codable {
struct ContentResult: Codable {
let adult: Bool
let backdropPath: String
let genreIDS: [Int]
Expand Down Expand Up @@ -56,3 +56,59 @@ enum OriginalLanguage: String, Codable {
case en = "en"
case es = "es"
}

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
// let contentResponse = try? JSONDecoder().decode(ContentResponse.self, from: jsonData)

//import Foundation
//
//// MARK: - ContentResponse
//struct ContentResponse: Codable {
// let page: Int
// let results: [Result]
// let totalPages, totalResults: Int
//
// enum CodingKeys: String, CodingKey {
// case page, results
// case totalPages = "total_pages"
// case totalResults = "total_results"
// }
//}
//
//// MARK: - Result
//struct Result: Codable {
// let adult: Bool
// let backdropPath: String
// let genreIDS: [Int]
// let id: Int
// let originalLanguage: OriginalLanguage
// let originalTitle, overview: String
// let popularity: Double
// let posterPath, releaseDate, title: String
// let video: Bool
// let voteAverage: Double
// let voteCount: Int
//
// enum CodingKeys: String, CodingKey {
// case adult
// case backdropPath = "backdrop_path"
// case genreIDS = "genre_ids"
// case id
// case originalLanguage = "original_language"
// case originalTitle = "original_title"
// case overview, popularity
// case posterPath = "poster_path"
// case releaseDate = "release_date"
// case title, video
// case voteAverage = "vote_average"
// case voteCount = "vote_count"
// }
//}
//
//enum OriginalLanguage: String, Codable {
// case en = "en"
// case es = "es"
//}
//
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,92 @@
//

import Foundation
import Alamofire

final class ContentService {

static let shared = ContentService()

private init() {}

let url = "https://api.themoviedb.org/3/movie/popular?api_key=5ef8d1b433c1a9970f7ade5c76cc090d&language=ko-KR"

func content(completion: @escaping (NetworkResult<Any>) -> Void) {

let header: HTTPHeaders = ["Content-Type" : "application/json"]
let dataRequest = AF.request(url,
method: .get,
parameters: nil,
headers: header)

dataRequest.responseData { response in
switch response.result {
case .success:
guard let statusCode = response.response?.statusCode else { return }
guard let value = response.value else { return }
let networkResult = self.judgeStatus(by: statusCode, value)
completion(networkResult)
case .failure:
completion(.networkErr)
}
import Moya

enum ContentService {
case fetchContent
}

extension ContentService: TargetType {
var baseURL: URL {
return URL(string: URLConst.baseURL)!
}

var path: String {
switch self {
case .fetchContent:
return "/3/movie/popular"
}
}

private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
switch statusCode {
case 200..<300: return isValidData(data: data)
case 400, 409: return isValidData(data: data)
case 500..<600: return .serverErr
default: return .networkErr

var method: Moya.Method {
return .get
}

var task: Task {
switch self {
case .fetchContent:
let parameters: [String: Any] = [
"api_key": "5ef8d1b433c1a9970f7ade5c76cc090d",
"language": "ko-KR"
]
return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
}
}

private func isValidData(data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(ContentResponse.self, from: data) else { return .pathErr }
return .success(decodedData as Any)

var headers: [String : String]? {
switch self {
case .fetchContent:
return APIConstants.headerWithOutToken
}
}
}

//import Foundation
//import Alamofire
//
//final class ContentService {
//
// static let shared = ContentService()
//
// private init() {}
//
// let url = "https://api.themoviedb.org/3/movie/popular?api_key=5ef8d1b433c1a9970f7ade5c76cc090d&language=ko-KR"
//
// func content(completion: @escaping (NetworkResult<Any>) -> Void) {
//
// let header: HTTPHeaders = ["Content-Type" : "application/json"]
// let dataRequest = AF.request(url,
// method: .get,
// parameters: nil,
// headers: header)
//
// dataRequest.responseData { response in
// switch response.result {
// case .success:
// guard let statusCode = response.response?.statusCode else { return }
// guard let value = response.value else { return }
// let networkResult = self.judgeStatus(by: statusCode, value)
// completion(networkResult)
// case .failure:
// completion(.networkErr)
// }
// }
// }
//
// private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
// switch statusCode {
// case 200..<300: return isValidData(data: data)
// case 400, 409: return isValidData(data: data)
// case 500..<600: return .serverErr
// default: return .networkErr
// }
// }
//
// private func isValidData(data: Data) -> NetworkResult<Any> {
// let decoder = JSONDecoder()
// guard let decodedData = try? decoder.decode(ContentResponse.self, from: data) else { return .pathErr }
// return .success(decodedData as Any)
// }
//}
//
Loading

0 comments on commit 19cb6ca

Please sign in to comment.