Skip to content

Commit

Permalink
feat: POST Image 기능 구현 (#82)
Browse files Browse the repository at this point in the history
주의: 오직 jpg파일만 가능
  • Loading branch information
dahun-lee-daji committed Jun 24, 2021
1 parent a10778b commit 40a9cfd
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 28 deletions.
4 changes: 4 additions & 0 deletions iOS/issue-tracker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
B0783E742681E1830031C2FD /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0783E732681E1830031C2FD /* Color.swift */; };
B0783E772681E1DB0031C2FD /* Milestone.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0783E762681E1DB0031C2FD /* Milestone.swift */; };
B0783E7A2681E21F0031C2FD /* Issue.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0783E792681E21F0031C2FD /* Issue.swift */; };
B0783E7D2684648C0031C2FD /* ImageResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0783E7C2684648C0031C2FD /* ImageResponse.swift */; };
BCCEBE1E1668EC814AFFBE24 /* Pods_issue_tracker.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 906455FA7C42AB9BB530CE77 /* Pods_issue_tracker.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -91,6 +92,7 @@
B0783E732681E1830031C2FD /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
B0783E762681E1DB0031C2FD /* Milestone.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Milestone.swift; sourceTree = "<group>"; };
B0783E792681E21F0031C2FD /* Issue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Issue.swift; sourceTree = "<group>"; };
B0783E7C2684648C0031C2FD /* ImageResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageResponse.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -278,6 +280,7 @@
B0783E732681E1830031C2FD /* Color.swift */,
B0783E762681E1DB0031C2FD /* Milestone.swift */,
B0783E792681E21F0031C2FD /* Issue.swift */,
B0783E7C2684648C0031C2FD /* ImageResponse.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -434,6 +437,7 @@
B0783E772681E1DB0031C2FD /* Milestone.swift in Sources */,
10D55EDE2670D46E007587C4 /* AdditionalTableViewCell.swift in Sources */,
B0783E742681E1830031C2FD /* Color.swift in Sources */,
B0783E7D2684648C0031C2FD /* ImageResponse.swift in Sources */,
10BDE6B52670C324007B38AF /* IssueEditViewController.swift in Sources */,
1029609E2673401E00669988 /* StoryBoardName.swift in Sources */,
10D4258526709D4800AE119C /* UIViewController+Instantiate.swift in Sources */,
Expand Down
59 changes: 45 additions & 14 deletions iOS/issue-tracker/IssueList/IssueListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,15 @@ class IssueListViewController: UIViewController, ReuseIdentity {
coordinator?.pushEditView()
}
@IBAction func testGETAction(_ sender: Any) {
// getUser()
getUser()
getLabel()
getMilestone()
getIssues()
}

@IBAction func testPostAction(_ sender: Any) {
NetworkService.shared.postLabel()
.subscribe(onNext: { responseLabel in
print(responseLabel)
},
onError: { error in
print(error)
},
onCompleted: {
print("comple")
},
onDisposed: {
print("dispose")
})
// postLabel()
postImage()
}

func getUser() {
Expand Down Expand Up @@ -99,6 +88,48 @@ class IssueListViewController: UIViewController, ReuseIdentity {
.disposed(by: disposeBag)
}

func postLabel() {
let input = Label.init(id: "",name: "testDowneyName3", description: "testDowneyDescription0", colors: Color.init(backgroundColor: "#FFFFFF", textColor: "#000000"))
let observer: Observable<LabelWithKey> = NetworkService.shared.post(input: input, target: .labels)

observer
.subscribe(onNext: { responseLabel in
print(responseLabel)
},
onError: { error in
print(error)
},
onCompleted: {
print("comple")
},
onDisposed: {
print("dispose")
})
.disposed(by: disposeBag)
}

func postImage() {

let testInput = UIImage.init(named: "githubIcon")!
let testInput2 = UIImage.init(named: "testJPG")!

let observer: Observable<ImageResponse> = NetworkService.shared.uploadImage(input: testInput2)

observer.subscribe(onNext: { response in
print(response)
},
onError: { error in
print(error)
},
onCompleted: {
print("comple")
},
onDisposed: {
print("disposed")
})
.disposed(by: disposeBag)
}

override func viewDidLoad() {
super.viewDidLoad()

Expand Down
16 changes: 16 additions & 0 deletions iOS/issue-tracker/Model/ImageResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// ImageResponse.swift
// issue-tracker
//
// Created by 이다훈 on 2021/06/24.
//

import Foundation

struct ImageResponse: Codable {
let image: InnerImageResponse
}

struct InnerImageResponse: Codable {
let url: String
}
14 changes: 3 additions & 11 deletions iOS/issue-tracker/Model/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,20 @@ struct Issues: Codable {

struct Issue: Codable {
let id: Int
let author: UserForIssue?
let author: User?
let title: String?
let createdAt: String?
let labels: [Label]?
let assignees: [UserForIssue]?
let assignees: [User]?
let milestone: Milestone?
let comments: [Comment]
let open: Bool
}

struct UserForIssue: Codable {
let id: String
let nickName: String?
let imageUrl: String?
let githubId: String?
let appleId: String?
}

struct Comment: Codable {
let id: String
let issueId: Int
let author: UserForIssue
let author: User
let createdAt: String
let content: String
}
2 changes: 2 additions & 0 deletions iOS/issue-tracker/Model/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ struct User: Codable {
let id: String
let nickName: String?
let imageUrl: String?
let githubId: String?
let appleId: String?
}
1 change: 1 addition & 0 deletions iOS/issue-tracker/Network/EndPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum APIPath: String, CustomStringConvertible {
case milestones = "api/milestones"
case comments = "api/comments"
case issues = "api/issues"
case images = "api/images"
}

struct LoginEndPoint: URLConvertible {
Expand Down
39 changes: 39 additions & 0 deletions iOS/issue-tracker/Network/NetworkRequester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ protocol NetworkRequesting {
func get<T: Codable>(endPoint: EndPoint, token: String?, parameters: [String: Codable]?) -> Observable<T>

func post<T: Codable, J:Codable>(endPoint: EndPoint, token: String?, body: T) -> Observable<J>

func uploadImage<T: Codable>(endPoint: EndPoint, token: String?, body: UIImage) -> Observable<T>
}

class NetworkRequester: NetworkRequesting {
Expand Down Expand Up @@ -77,4 +79,41 @@ class NetworkRequester: NetworkRequesting {

}

func uploadImage<T: Codable>(endPoint: EndPoint, token: String?, body: UIImage) -> Observable<T> {

let url = endPoint.urlFromEndPoint()
let accessToken = token != nil ? token! : ""
let header: HTTPHeaders = [
.authorization(bearerToken: accessToken),
.contentType("multipart/form-data")
]
let data = body.jpegData(compressionQuality: 0.2)

return Observable<T>.create({ observer in

let uploader = AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(data!,withName: "image",fileName: "image-downey" ,mimeType: "image/jpg")
},
to: url,
usingThreshold: UInt64.init(),
method: .post,
headers: header)
.responseData(completionHandler: { response in
switch response.result {
case .success(let data):
let decoded = try? JSONDecoder().decode(T.self, from: data)
observer.onNext(decoded!)
case .failure(let error):
observer.onError(error)
}
})

return Disposables.create {
uploader.cancel()
}

})
}

}
11 changes: 8 additions & 3 deletions iOS/issue-tracker/Network/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ class NetworkService {
return requester.get(endPoint: endPoint, token: loginToken, parameters: nil)
}

func postLabel() -> Observable<LabelWithKey> {
let endPoint = EndPoint.init(path: .labels, method: .post)
let input = Label.init(id: "",name: "testDowneyName3", description: "testDowneyDescription0", colors: Color.init(backgroundColor: "#FFFFFF", textColor: "#000000"))
func post<T: Codable, J: Codable>(input: T, target: APIPath) -> Observable<J> {
let endPoint = EndPoint.init(path: target, method: .post)

return requester.post(endPoint: endPoint, token: loginToken, body: input)
}

func uploadImage<T: Codable>(input: UIImage) -> Observable<T> {
let endPoint = EndPoint.init(path: .images, method: .post)

return requester.uploadImage(endPoint: endPoint, token: loginToken, body: input)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "testJPG.jpg",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 40a9cfd

Please sign in to comment.