-
Notifications
You must be signed in to change notification settings - Fork 0
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] iOS 4차 과제 #13
base: main
Are you sure you want to change the base?
[FEAT] iOS 4차 과제 #13
Changes from all commits
7c11ed3
bf12859
7838c27
b46a8d2
7ee2325
815cb32
4127bb5
afa9612
7a0f23f
a11e12b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// APIConstants.swift | ||
// InstagramClone | ||
// | ||
// Created by Inwoo Park on 2022/05/14. | ||
// | ||
|
||
import Foundation | ||
|
||
struct APIConstants { | ||
static let baseURL = "http://13.124.62.236" | ||
static let loginURL = baseURL + "/auth/signin" | ||
static let signupURL = baseURL + "/auth/signup" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// LoginResponse.swift | ||
// InstagramClone | ||
// | ||
// Created by Inwoo Park on 2022/05/14. | ||
// | ||
|
||
import Foundation | ||
|
||
struct LoginResponse: Codable { | ||
let status: Int | ||
let success: Bool? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 성공, 실패 둘 다 success값이 전달되던데, 혹시 success 파라미터를 옵셔널로 선언한 이유가 따로 있나요??🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음.. 그러게요? 이유는 딱히 없고 실수인거 같습니다ㅠ |
||
let message: String | ||
let data: LoginData? | ||
} | ||
|
||
struct LoginData: Codable { | ||
let name: String | ||
let email: String | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// SignUpResponse.swift | ||
// InstagramClone | ||
// | ||
// Created by Inwoo Park on 2022/05/14. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SignUpResponse: Codable { | ||
let status: Int | ||
let success: Bool? | ||
let message: String | ||
let data: SignUpData? | ||
} | ||
|
||
struct SignUpData: Codable { | ||
let id: Int | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// NetworkService.swift | ||
// InstagramClone | ||
// | ||
// Created by Inwoo Park on 2022/05/14. | ||
// | ||
|
||
import Foundation | ||
|
||
import RxSwift | ||
import RxCocoa | ||
|
||
class NetworkService { | ||
static let shared = NetworkService() | ||
private init() {} | ||
|
||
func loginRequest(id: String, password: String) throws -> Observable<LoginResponse?> { | ||
let body = ["email": id, "password": password] | ||
let encoder = JSONEncoder() | ||
let encodedBody = try encoder.encode(body) | ||
|
||
return Observable.just(APIConstants.loginURL) | ||
.map { URL(string: $0)! } | ||
.map { | ||
var request = URLRequest(url: $0) | ||
request.httpMethod = "POST" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rx .. 멋있습니다 .. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 아직 어렵지만... 열심히해보고 있슴당.. |
||
request.httpBody = encodedBody | ||
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | ||
return request | ||
} | ||
.flatMap { URLSession.shared.rx.response(request: $0) } | ||
.map { _, data in | ||
do { | ||
return try JSONDecoder().decode(LoginResponse.self, from: data) | ||
} catch { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func signUpRequest(id: String, password: String) throws -> Observable<SignUpResponse?> { | ||
let body = ["name": "", "email": id, "password": password] | ||
let encoder = JSONEncoder() | ||
let encodedBody = try encoder.encode(body) | ||
|
||
return Observable.just(APIConstants.signupURL) | ||
.map { URL(string: $0)! } | ||
.map { | ||
var request = URLRequest(url: $0) | ||
request.httpMethod = "POST" | ||
request.httpBody = encodedBody | ||
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | ||
return request | ||
} | ||
.flatMap { URLSession.shared.rx.response(request: $0) } | ||
.map { _, data in | ||
do { | ||
return try JSONDecoder().decode(SignUpResponse.self, from: data) | ||
} catch { | ||
return nil | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 케이스마다 에러를 보기쉽게 나눌수도 있군요!!!!😮