GitHub: SolarNetwork
剛開始寫Swift項(xiàng)目時(shí),用到網(wǎng)絡(luò)請(qǐng)求時(shí),毫無(wú)疑問(wèn),選擇了Moya。但隨著項(xiàng)目的增大,接口越來(lái)越多,TargetType文件也越來(lái)越大,尋找某一個(gè)接口的參數(shù)和其他配置時(shí),就要在各種Switch中跳來(lái)跳去,感到十分煩惱,就算分了多個(gè)TargetType,也還是存在這個(gè)問(wèn)題,于是就干脆借鑒Moya的優(yōu)點(diǎn),基于Alamofire重新封裝了一個(gè)簡(jiǎn)單易用的網(wǎng)絡(luò)庫(kù),保留了TargetType的服務(wù)配置功能,并將接口配置分離成各個(gè)獨(dú)立的Request,形成一個(gè)新的網(wǎng)絡(luò)庫(kù)SolarNetwork。
SolarNetwork包括以前幾個(gè)類:
willSend
和 接收到網(wǎng)絡(luò)回調(diào)后修改回調(diào)內(nèi)容的 didReceive
。 所以一個(gè)網(wǎng)絡(luò)請(qǐng)求的具體流程為:
SLNetwork(SLTarget).request(SLRequest).willSend(SLRequest) .progressClosure(SLProgress) .reponseData(OriginalResponse) .didReceive(SLResponse).decodeTo(Dictionary) .completionClosure(SLResponse) .decodeTo(Model: Decodable).dealWithError大多數(shù)情況下,你只需要關(guān)心的是:
SLNetwork(SLTarget).request(SLRequest) .progressClosure(SLProgress) .completionClosure(SLResponse)
更多內(nèi)容請(qǐng)直接到GitHub查看。
import SolarNetwork struct HTTPBinTarget: SLTarget { var baseURLString: String { return "https://httpbin.org" } } let HTTPBinNetwork = SLNetwork(HTTPBinTarget())
import SolarNetwork //Mark: - GET class HTTPBinGETRequest: SLRequest { override func loadRequest() { super.loadRequest() self.path = "/get" } } HTTPBinNetwork.request(HTTPBinGETRequest()) { (response) in if let dictionary = response.dataDictionary { } else if let error = response.error { //show error } } //Mark: - POST class HTTPBinPOSTRequest: SLRequest { override func loadRequest() { super.loadRequest() self.method = .post self.path = "/post" } /** 利用反射,將屬性自動(dòng)轉(zhuǎn)換為Parameters,不需要自己組裝了 ["userName": "myUserName", "password": "myPassword"] */ let userName = "myUserName" let password = "myPassword" } HTTPBinNetwork.request(HTTPBinPOSTRequest()) { (response) in if let dictionary = response.dataDictionary { } else if let error = response.error { //show error } }
import SolarNetwork class GitHubDownloadRequest: SLDownloadRequest { override func loadRequest() { super.loadRequest() self.URLString = "http://cdnvue.com/video/rzGHzRA19L/64tBZo" } } let downloadRequest = GitHubDownloadRequest() GitHubNetwork.download(downloadRequest, progressClosure: { (progress) in }) { (response) in } //如果需要斷點(diǎn)下載此文件,請(qǐng)?jiān)O(shè)置該屬性為true downloadRequest.isResume = true //自定義文件下載完成后的存放路徑,默認(rèn)為"/Library/Caches/SLNetwork/Destination/(requestID)" downloadRequest.destinationURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
import SolarNetwork class HTTPBinUploadRequest: SLUploadRequest { override func loadRequest() { super.loadRequest() self.path = "/post" } } let uploadRequest = HTTPBinUploadRequest() uploadRequest.data = data //data to upload HTTPBinNetwork.upload(uploadRequest, progressClosure: { (progress) in }) { (response) in }
Swift 4下,利用Decodable將Json轉(zhuǎn)為Model
import SolarNetwork struct User: Decodable { //Swift 4 Codable var id: Int var name: String var token: String } HTTPBinNetwork.request(UserRequest()) { (response) in if let user = response.decode(to: User.self) { } else if let error = response.error { //show error } }