-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Network [#55] BaseURL 설정 및 네트워크 세팅
- Loading branch information
Showing
17 changed files
with
327 additions
and
64 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
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
15 changes: 15 additions & 0 deletions
15
DontBe-iOS/DontBe-iOS/Network/Foundation/BaseResponse.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,15 @@ | ||
// | ||
// BaseResponse.swift | ||
// DontBe-iOS | ||
// | ||
// Created by 변희주 on 1/14/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct BaseResponse<T: Decodable>: Decodable { | ||
let status: Int | ||
let success: Bool | ||
let message: String | ||
let data: T? | ||
} |
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
12 changes: 12 additions & 0 deletions
12
DontBe-iOS/DontBe-iOS/Network/SocialLogin/DTO/SocialLoginRequestDTO.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,12 @@ | ||
// | ||
// SocialLoginRequestDTO.swift | ||
// DontBe-iOS | ||
// | ||
// Created by 변희주 on 1/14/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SocialLoginRequestDTO: Encodable { | ||
let socialPlatform: String | ||
} |
17 changes: 17 additions & 0 deletions
17
DontBe-iOS/DontBe-iOS/Network/SocialLogin/DTO/SocialLoginResponseDTO.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,17 @@ | ||
// | ||
// SocialLoginResponseDTO.swift | ||
// DontBe-iOS | ||
// | ||
// Created by 변희주 on 1/14/24. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - SocilLoginResponseDTO | ||
struct SocialLoginResponseDTO: Decodable { | ||
let nickName: String | ||
let memberId: Int | ||
let accessToken, refreshToken: String | ||
let memberProfileUrl: String | ||
let isNewUser: Bool | ||
} |
91 changes: 91 additions & 0 deletions
91
DontBe-iOS/DontBe-iOS/Network/SocialLogin/Service/SocialLoginService.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,91 @@ | ||
// | ||
// KakaoLoginService.swift | ||
// DontBe-iOS | ||
// | ||
// Created by 변희주 on 1/14/24. | ||
// | ||
|
||
import Foundation | ||
import Foundation | ||
|
||
protocol SocialLoginServiceType { | ||
func makeRequestURL(accessToken: String) -> URLRequest | ||
func postData(accessToken: String) async throws -> BaseResponse<SocialLoginResponseDTO>? | ||
func parseData(data: Data) -> BaseResponse<SocialLoginResponseDTO>? | ||
} | ||
|
||
final class SocialLoginService: SocialLoginServiceType { | ||
|
||
func makeRequestURL(accessToken: String) -> URLRequest { | ||
|
||
let baseURL = Config.baseURL | ||
|
||
let urlString = "\(baseURL)/auth" | ||
|
||
// URL 생성 | ||
guard let url = URL(string: urlString) else { | ||
fatalError("Failed to create URL") | ||
} | ||
|
||
// URLRequest 생성 | ||
var request = URLRequest(url: url) | ||
request.httpMethod = "POST" | ||
|
||
// 헤더 추가 | ||
let header = ["Content-Type": "application/json", | ||
"Authorization": "Bearer \(accessToken)"] | ||
header.forEach { | ||
request.addValue($0.value, forHTTPHeaderField: $0.key) | ||
} | ||
|
||
// 리퀘스트 바디 설정 | ||
let requestBody = SocialLoginRequestDTO(socialPlatform: "KAKAO") | ||
do { | ||
let jsonData = try JSONEncoder().encode(requestBody) | ||
request.httpBody = jsonData | ||
} catch { | ||
print("Failed to encode request body: \(error)") | ||
} | ||
|
||
return request | ||
} | ||
|
||
func postData(accessToken: String) async throws -> BaseResponse<SocialLoginResponseDTO>? { | ||
do { | ||
let request = self.makeRequestURL(accessToken: accessToken) | ||
let (data, response) = try await URLSession.shared.data(for: request) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse else { | ||
throw NetworkError.responseError | ||
} | ||
|
||
switch httpResponse.statusCode { | ||
case 200..<300: | ||
return parseData(data: data) | ||
case 400: | ||
throw NetworkError.badRequestError | ||
case 401: | ||
throw NetworkError.unautohorizedError | ||
case 404: | ||
throw NetworkError.notFoundError | ||
case 500: | ||
throw NetworkError.internalServerError | ||
default: | ||
throw NetworkError.unknownError | ||
} | ||
} catch { | ||
throw error | ||
} | ||
} | ||
|
||
func parseData(data: Data) -> BaseResponse<SocialLoginResponseDTO>? { | ||
do { | ||
let jsonDecoder = JSONDecoder() | ||
let result = try jsonDecoder.decode(BaseResponse<SocialLoginResponseDTO>.self, from: data) | ||
return result | ||
} catch { | ||
print(error) | ||
return nil | ||
} | ||
} | ||
} |
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
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.