Skip to content

Commit

Permalink
Feat: 회원가입 네트워크 요청 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
GeonH0 committed Sep 12, 2024
1 parent af3201f commit a01a130
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// EmptyResponse.swift
// HomeCafeRecipes
//
// Created by 김건호 on 9/11/24.
//

import Foundation

struct EmptyResponse: Decodable {}
49 changes: 49 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipes/Data/Network/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import RxSwift
protocol NetworkService {
func getRequest<T: Decodable>(url: URL, responseType: T.Type) -> Single<T>
func postRequest<T: Decodable>(url: URL, parameters: [String: Any], imageDatas: [Data], responseType: T.Type) -> Single<T>
func postJsonRequest<T: Decodable>(url: URL, parameters:[String: Any], responseType: T.Type) -> Single<T>
}

class BaseNetworkService: NetworkService {
Expand Down Expand Up @@ -98,4 +99,52 @@ class BaseNetworkService: NetworkService {
}
}
}

func postJsonRequest<T: Decodable>(
url: URL,
parameters: [String: Any],
responseType: T.Type
) -> Single<T> {
return Single.create { single in
// JSON으로 변환
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
single(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "JSON 인코딩 실패"])))
return Disposables.create()
}

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = httpBody

let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
single(.failure(error))
} else if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
let statusCode = httpResponse.statusCode
let responseString = data.flatMap { String(data: $0, encoding: .utf8) } ?? "No response data"
let error = NSError(
domain: "",
code: statusCode,
userInfo: [NSLocalizedDescriptionKey: "HTTP \(statusCode): \(responseString)"]
)
single(.failure(error))
} else if let data = data {
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let responseObject = try decoder.decode(T.self, from: data)
single(.success(responseObject))
} catch let decodingError {
single(.failure(decodingError))
}
}
}
task.resume()

return Disposables.create {
task.cancel()
}
}
}
}
63 changes: 63 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipes/Data/Network/SignUpService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// SignUpService.swift
// HomeCafeRecipes
//
// Created by 김건호 on 9/11/24.
//

import Foundation

import RxSwift

protocol SignUpService {
func signUp(
userNickName: String,
userID: String,
password: String
) -> Single<Void>
}

final class SignUpServiceImpl: SignUpService {
private let networkService: NetworkService

init(networkService: NetworkService) {
self.networkService = networkService
}

private func makeURL(endpoint: String) -> URL {
return APIConfig().baseURL.appendingPathComponent(endpoint)
}

func signUp(
userNickName: String,
userID: String,
password: String
) -> Single<Void> {
let url = makeURL(endpoint: "auth/register")

let parameters: [String: Any] = [
"email": userID,
"password": password,
"nickname": userNickName
]

return networkService.postJsonRequest(
url: url,
parameters: parameters,
responseType: NetworkResponseDTO<EmptyResponse>.self
)
.flatMap { response in
if response.statusCode == 200 {
// 성공 시 data가 null이더라도 성공 처리
return .just(())
} else {
// statusCode가 200이 아니면 에러 처리
return .error(NSError(domain: "SignUpError", code: response.statusCode, userInfo: [NSLocalizedDescriptionKey: response.message]))
}
}
.catch { error in
// 네트워크 또는 서버에서 에러가 발생했을 경우 처리
return .error(error)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// SignUpRepository.swift
// HomeCafeRecipes
//
// Created by 김건호 on 9/10/24.
//

import UIKit

import RxSwift

protocol SignUpRepository {
func signUp(userNickName: String, userID: String, password: String) -> Single<Void>
}

final class SignUpRepositoryImpl: SignUpRepository {

private let SignUpService: SignUpService

init(SignUpService: SignUpService) {
self.SignUpService = SignUpService
}

func signUp(userNickName: String, userID: String, password: String) -> Single<Void> {
return SignUpService.signUp(
userNickName: userNickName,
userID: userID,
password: password
)
}
}

0 comments on commit a01a130

Please sign in to comment.