iOS) Moya 사용

1 minute read

Moya

BandAPI

import Foundation
import Moya

class BandAPI {
    static let shared = BandAPI()
    static let provider = MoyaProvider<BandService>()
    
    func getBands(completion: @escaping ([Band]) -> ()) {
        BandAPI.provider.request(.bands) { response in
            switch response {
            case .success(let result):
                do {
                    let results = try JSONDecoder().decode(BandlistDataModel.self, from: result.data)
                    completion(results.band)
                    print(results.message)
                } catch let err {
                    print("JSONDecode: \(err.localizedDescription)")
                    debugPrint(err)
                }
            case .failure(let err):
                print(".failure: \(err.localizedDescription)")
            }
        }
    }
}

BandService

import Foundation
import Moya

public enum BandService {
    case bands
}

extension BandService: TargetType {
    public var baseURL: URL {
        return URL(string: GeneralAPI.baseURL)!
    }
    
    public var path: String {
        switch self {
        case .bands:
            return "/api/bands"
        }
    }
    
    public var method: Moya.Method {
        return .get
    }
    
    public var sampleData: Data {
        return  "sampleData".data(using: .utf8)!
    }
    
    public var task: Task {
        .requestPlain
    }
    
    public var headers: [String : String]? {
        switch self {
        default:
            return ["Content-Type": "application/json"]
        }
    }
}

model

// MARK: - BandlistDataModel
struct BandlistDataModel: Codable {
    let band: [Band]
    let message: String
}

// MARK: - Band
struct Band: Codable {
    let id: String
    let category: Category
    let bandDescription: String
    let img: String
    let member: Int
    let name, owner: String

    enum CodingKeys: String, CodingKey {
        case id = "_id"
        case category
        case bandDescription = "description"
        case img, member, name, owner
    }
    
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        id = (try? values.decode(String.self, forKey: .id)) ?? ""
        category = (try? values.decode(Category.self, forKey: .category)) ?? Category(id: "", name: "")
        bandDescription = (try? values.decode(String.self, forKey: .bandDescription)) ?? ""
        img = (try? values.decode(String.self, forKey: .img)) ?? ""
        member = (try? values.decode(Int.self, forKey: .member)) ?? 0
        name = (try? values.decode(String.self, forKey: .name)) ?? ""
        owner = (try? values.decode(String.self, forKey: .owner)) ?? ""
    }
}

// MARK: - Category
struct Category: Codable {
    let id, name: String

    enum CodingKeys: String, CodingKey {
        case id = "_id"
        case name
    }
}

main

private var bandList = [Band]()
...
BandAPI.shared.getBands { response in
    self.bandList = response
}

Categories:

Updated: