Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
APIKitのRequestをProtocol用いてまとめてみた ref: https://qiita.com/RichGreenTea/items/4957f1388f11b5b454d5
struct EditingUser: Decodable {
var id: Int?
var name: String?
var email: String?
var tel: String?
}
let repository = EventRepository()
repository.get(id: self.id) { [unowned self] result in
switch result {
case .success(let user):
self.user = user
case .failure(let error):
print(error)
}
}
enum LocalDataKey: String {
case authToken = "AuthToken"
}
class MyAppLocalData {
class func authToken() -> String? {
return UserDefaults.standard.string(forKey: LocalDataKey.authToken.rawValue)
}
class func setAuthToken(_ token: String?) {
if token == nil {return}
UserDefaults.standard.set(token!, forKey: LocalDataKey.authToken.rawValue)
UserDefaults.standard.synchronize()
}
}
enum Result<T> {
case success(T)
case failure(Error)
}
struct User: Decodable {
let id: Int
let name: String
let email: String
let tel: String
}
let repository = EventRepository()
repository.get(id: self.id) { result in
switch result {
case .success(let user):
self.user = user
case .failure(let error):
print(error)
}
}
import Foundation
import APIKit
struct UserRepository {
/// Userの登録
func post(user: EditingUser, handler: @escaping (Result<User>) -> Void) {
Session.send(UserPostRequest(user: user)) { result in
switch result {
case .success(let response):
handler(.success(response.data))
case .failure(let error):
handler(.failure(error))
}
}
}
/// Userの更新
func patch(user: EditingUser, handler: @escaping (Result<User>) -> Void) {
Session.send(UserPatchRequest(user: user)) { result in
switch result {
case .success(let response):
handler(.success(response.data))
case .failure(let error):
handler(.failure(error))
}
}
}
/// Userの削除
func delete(id: Int, _ handler: @escaping (Result<Any>) -> Void) {
Session.send(UserDeleteRequest(id: id)) { result in
switch result {
case .success(let response):
handler(.success(response))
case .failure(let error):
handler(.failure(error))
}
}
}
/// Userの取得
func get(id: Int, handler: @escaping (Result<User>) -> Void) {
Session.send(UserGetRequest(id: id)) { result in
switch result {
case .success(let response):
handler(.success(response.data))
case .failure(let error):
handler(.failure(error))
}
}
}
// MARK: - POST
// MyRequestプロトコルを使用!!
struct UserPostRequest: MyRequest {
typealias Response = UserPostResponse
let user: EditingUser
let method: HTTPMethod = .post
let path: String = "/users"
var bodyParameters: BodyParameters? {
var params: [MultipartFormDataBodyParameters.Part] = []
// required params
guard let name = user.name, let email = user.email else { return nil }
params.append(try! MultipartFormDataBodyParameters.Part(
value: name,
name: "name"))
params.append(try! MultipartFormDataBodyParameters.Part(
value: email,
name: "email"))
// optional params
if let tel = user.tel {
params.append(try! MultipartFormDataBodyParameters.Part(
value: tel,
name: "tel"))
}
return MultipartFormDataBodyParameters(parts: params)
}
}
struct UserPostResponse: Decodable {
let status: String
let data: User
}
// MARK: - PATCH
struct UserPatchRequest: MyRequest {
typealias Response = UserPatchResponse
let user: EditingUser
let method: HTTPMethod = .patch
var path: String {
guard let id = user.id else { return "" }
return "/users/\(id)"
}
var bodyParameters: BodyParameters? {
var params: [MultipartFormDataBodyParameters.Part] = []
// required params
guard let name = user.name, let email = user.email else { return nil }
params.append(try! MultipartFormDataBodyParameters.Part(
value: name,
name: "name"))
params.append(try! MultipartFormDataBodyParameters.Part(
value: email,
name: "email"))
// optional params
if let tel = user.tel {
params.append(try! MultipartFormDataBodyParameters.Part(
value: tel,
name: "tel"))
}
return MultipartFormDataBodyParameters(parts: params)
}
}
struct UserPatchResponse: Decodable {
let status: String
let data: User
}
// MARK: - DELETE
struct UserDeleteRequest: MyRequest {
typealias Response = UserDeleteResponse
let id: Int
let method: HTTPMethod = .delete
var path: String {
return "/users/\(id)"
}
}
struct UserDeleteResponse: Decodable {
let status: String?
}
// MARK: - GET
struct UserGetRequest: MyRequest {
typealias Response = UserGetResponse
let id: Int
let method: HTTPMethod = .get
var path: String {
return "/users/\(id)"
}
}
struct UserGetResponse: Decodable {
let status: String
let data: User
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.