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: 이미지 업로드 API 구현 (#390) #391

Merged
merged 6 commits into from
Apr 11, 2023
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
19 changes: 18 additions & 1 deletion NADA-iOS-forRelease/Sources/NetworkService/Card/CardAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ public class CardAPI {
}
}

func cardImageUpload(image: UIImage, completion: @escaping (NetworkResult<Any>) -> Void) {
cardProvider.request(.imageUpload(image: image)) { result in
switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data)

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

// MARK: - JudgeStatus methods

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

let decoder = JSONDecoder()
Expand Down Expand Up @@ -196,7 +213,7 @@ public class CardAPI {

switch statusCode {
case 200:
return .success("")
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.error?.message ?? "error message")
case 500:
Expand Down
13 changes: 11 additions & 2 deletions NADA-iOS-forRelease/Sources/NetworkService/Card/CardService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum CardService {
case cardListFetch(userID: String, isList: Bool, offset: Int?)
case cardListEdit(request: CardListEditRequest)
case cardDelete(cardID: String)
case imageUpload(image: UIImage)
}

extension CardService: TargetType {
Expand All @@ -34,14 +35,16 @@ extension CardService: TargetType {
return "/cards"
case .cardDelete(let cardID):
return "/card/\(cardID)"
case .imageUpload:
return "/image"
}
}

var method: Moya.Method {
switch self {
case .cardDetailFetch, .cardListFetch:
return .get
case .cardCreation:
case .cardCreation, .imageUpload:
return .post
case .cardListEdit:
return .put
Expand Down Expand Up @@ -106,6 +109,12 @@ extension CardService: TargetType {
encoding: URLEncoding.queryString)
case .cardListEdit(let requestModel):
return .requestJSONEncodable(requestModel)
case .imageUpload(let image):
var multiPartData: [Moya.MultipartFormData] = []
let imageData = MultipartFormData(provider: .data(image.pngData() ?? Data()), name: "image", mimeType: "image/png")
multiPartData.append(imageData)
Comment on lines +114 to +115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

설명 읽어봤습니당!

뭔가 filename을 포함하지 않는다면 제대로 업로드 되지 않을수도 있겠다는 생각이 들었어요
우리가 스웨거에서 파일을 입력할때는 그야말로 이미 컴퓨터에 존재하는 "png 파일"을 골라서 넣는거라 이미 파일명이 있겠지만,
실제로 사용자가 앱에서 업로드할때는 "파일" 이 아니라 사용자가 갤러리에 가지고 있는 "사진"을 png 파일로 우리가 변환해?주는거라 파일명을 따로 붙여줘야될수도 있겠다는 생각이 들었습니당

근데 이러고 파일 이름 안넣어도 되면 스껄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요것도 숫자랑 문자로 이루어진 HEIG 파일명이 존재하는거 같더라구용? 그리고 파라미터가 옵셔널이라서 일단 비워서 한번 테스트해보공!
문제가 있으면 이슈 만들어서 반영해보겠습니당! 고생하셨어요~ 스껄~


return .uploadMultipart(multiPartData)
}
}

Expand All @@ -115,7 +124,7 @@ extension CardService: TargetType {
return Const.Header.bearerHeader()
case .cardListEdit:
return Const.Header.basicHeader()
case .cardCreation:
case .cardCreation, .imageUpload:
return Const.Header.multipartFormHeader()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public class GroupAPI {
}
}

// MARK: - JudgeStatus methods

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

let decoder = JSONDecoder()
Expand Down Expand Up @@ -207,7 +209,7 @@ public class GroupAPI {

switch statusCode {
case 200:
return .success("success")
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.error?.message ?? "error message")
case 500:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class UserAPI {
}
}

// MARK: - JudgeStatus methods

private func judgeUserSocialSignUpStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<AccessToken>.self, from: data)
Expand Down Expand Up @@ -144,7 +146,7 @@ public class UserAPI {

switch statusCode {
case 200:
return .success("success")
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.error?.message ?? "error message")
case 500:
Expand Down
2 changes: 2 additions & 0 deletions NADA-iOS-forRelease/Sources/NetworkService/Util/UtilAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class UtilAPI {
}
}

// MARK: - JudgeStatus methods

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

let decoder = JSONDecoder()
Expand Down