POP網絡重構(二)

override func viewDidLoad() {
        super.viewDidLoad()
        let request = LGPersonRequest(name: "kody")
        LGClient.manager.send(request) { (person) in
            if let person = person {
                self.updataUI(person: person)
            }
        }
//        LGLocalClient().send(request) { [weak self](person) in
//            if let person = person {
//                self?.updataUI(person: person)
//            }
//        }
    }
    
    func updataUI(person:LGPerson){
        imageView.image = UIImage(named: person.iconName)
        nickName.text   = person.name
        ageLabel.text   = person.age
        hobbyLabel.text = person.hobby
        petPhraseLabel.text = person.petPhrase
    }
複製代碼
  • 模型 結構體LGPerson,擴展了LGDecodable協議,具有了parse能力
//LGPerson.swift
import UIKit

struct LGPerson {
    let name: String
    let iconName: String
    let age: String
    let hobby: String
    let petPhrase: String

    init?(data: Data) {
        guard let obj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else {
            return nil
        }
        guard let name = obj["name"] as? String else {
            return nil
        }
        guard let iconName = obj["iconName"] as? String else {
            return nil
        }
        guard let age = obj["age"] as? String else {
            return nil
        }
        guard let hobby = obj["hobby"] as? String else {
            return nil
        }
        guard let petPhrase = obj["petPhrase"] as? String else {
            return nil
        }
        self.name = name
        self.iconName = iconName
        self.age = age
        self.hobby = hobby
        self.petPhrase = petPhrase
    }
}

extension LGPerson: LGDecodable {
    static func parse(data: Data) -> LGPerson? {
        return LGPerson(data: data)
    }
}
複製代碼
  • associatedtype Response: LGDecodable: Response實現LGDecodable協議,具有parse功能
//LGRequest.swift
import UIKit

protocol LGRequest {
    var path: String { get }
    var method: LGHTTPMethod { get }
    var parameter: [String: Any] { get }
    
    associatedtype Response: LGDecodable
}

struct LGPersonRequest: LGRequest {
    
    typealias Response = LGPerson
    
    let name: String
    var path: String {
        return "/pythonJson/getTeacherInfo/?username=\(name)"
    }
    let method: LGHTTPMethod = .GET
    let parameter: [String: Any] = [:]
}
複製代碼
  • hostsend集中到ClientProtocol協議中
  • LGClient發起請求
//LGClient.swift
import UIKit

enum LGHTTPMethod: String {
    case GET
    case POST
}

protocol ClientProtocol {
    var host: String { get }
    func send<T: LGRequest>(_ r: T, handler: @escaping (T.Response?) -> Void)
}

class LGClient: ClientProtocol{
    static let manager = LGClient()
    let host: String = "http://127.0.0.1:5000"
    
    func send<T>(_ r: T, handler: @escaping (T.Response?) -> Void) where T : LGRequest {
        
        let url = URL(string: host.appending(r.path))!
        var request = URLRequest(url: url)
        request.httpMethod = r.method.rawValue
        let task = URLSession.shared.dataTask(with: request) {
            data, response, error in
            if let data = data, let res = T.Response.parse(data: data) {
                DispatchQueue.main.async { handler(res) }
            } else {
                DispatchQueue.main.async { handler(nil) }
            }
        }
        task.resume()
    }
}

class LGLocalClient: LGClient {
    
    override func send<T>(_ r: T, handler: @escaping (T.Response?) -> Void) where T : LGRequest {
        
        switch r.path {
        case let string where string.contains("/pythonJson/getTeacherInfo"):
            print("123456")
            handler(nil)
        default:
            let url = URL(string: host.appending(r.path))!
            var request = URLRequest(url: url)
            request.httpMethod = r.method.rawValue
            let task = URLSession.shared.dataTask(with: request) {
                data, response, error in
                if let data = data, let res = T.Response.parse(data: data) {
                    DispatchQueue.main.async { handler(res) }
                } else {
                    DispatchQueue.main.async { handler(nil) }
                }
            }
            task.resume()
        }

    }
}
複製代碼
  • 協議LGDecodable
//LGResponse.swift
import UIKit

class LGResponse: NSObject {

    // 序列化 , response存儲其餘,不寫了!!!
}

protocol LGDecodable {
    static func parse(data: Data) -> Self?
}
複製代碼