0001 // The MIT License (MIT) 0002 0003 // Copyright (c) 2015 JohnLui <wenhanlv@gmail.com> https://github.com/johnlui 0004 0005 // Permission is hereby granted, free of charge, to any person obtaining a copy 0006 // of this software and associated documentation files (the "Software"), to deal 0007 // in the Software without restriction, including without limitation the rights 0008 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 // copies of the Software, and to permit persons to whom the Software is 0010 // furnished to do so, subject to the following conditions: 0011 0012 // The above copyright notice and this permission notice shall be included in all 0013 // copies or substantial portions of the Software. 0014 0015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0018 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0020 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0021 // SOFTWARE. 0022 // 0023 // Pitaya.swift 0024 // Pitaya 0025 // 0026 // Created by JohnLui on 15/5/14. 0027 // 0028 0029 import Foundation 0030 0031 /// make your code looks tidier 0032 public typealias Pita = Pitaya 0033 0034 public class Pitaya{ 0035 0036 /// if set to true, Pitaya will log all information in a NSURLSession lifecycle 0037 public static var DEBUG
Pitaya.swift:32 public typealias Pita = PitayaPitaya.swift:49 public static func build(HTTPMethod method: HTTPMethod, url: String) -> Pitaya {Pitaya.swift:50 let p = Pitaya()Pitaya.swift:62 public func addParams(params: [String: AnyObject]) -> Pitaya {Pitaya.swift:74 public func addFiles(files: [File]) -> Pitaya {Pitaya.swift:87 public func addSSLPinning(LocalCertData data: NSData, SSLValidateErrorCallBack: (()->Void)? = nil) -> Pitaya {Pitaya.swift:100 public func setHTTPHeader(Name key: String, Value value: String) -> Pitaya {Pitaya.swift:113 public func setHTTPBodyRaw(string: String, isJSON: Bool = false) -> Pitaya {Pitaya.swift:126 public func setBasicAuth(username: String, password: String) -> Pitaya {Pitaya.swift:139 public func onNetworkError(errorCallback: ((error: NSError) -> Void)) -> Pitaya {PitayaManager.swift:176 if Pitaya.DEBUG { if let a = request.allHTTPHeaderFields { NSLog("Pitaya Request HEADERS: ", a.description); }; }PitayaManager.swift:178 if Pitaya.DEBUG { if let a = response { NSLog("Pitaya Response: ", a.description); }}= false 0038 0039 var pitayaManager
PitayaManager.swift:176 if Pitaya.DEBUG { if let a = request.allHTTPHeaderFields { NSLog("Pitaya Request HEADERS: ", a.description); }; }PitayaManager.swift:178 if Pitaya.DEBUG { if let a = response { NSLog("Pitaya Response: ", a.description); }}: PitayaManager! 0040 0041 /** 0042 the only init method to fire a HTTP / HTTPS request 0043 0044 - parameter method: the HTTP method you want 0045 - parameter url: the url you want 0046 0047 - returns: a Pitaya object 0048 */ 0049 public static func build(HTTPMethod method: HTTPMethod, url: String) -> Pitaya { 0050 let p = Pitaya() 0051 p.pitayaManager = PitayaManager.build(method, url: url) 0052 return p 0053 } 0054 0055 /** 0056 add params to self (Pitaya object) 0057 0058 - parameter params: what params you want to add in the request. Pitaya will do things right whether methed is GET or POST. 0059 0060 - returns: self (Pitaya object) 0061 */ 0062 public func addParams(params: [String: AnyObject]) -> Pitaya { 0063 self.pitayaManager.addParams(params) 0064 return self 0065 } 0066 0067 /** 0068 add files to self (Pitaya object), POST only 0069 0070 - parameter params: add some files to request 0071 0072 - returns: self (Pitaya object) 0073 */ 0074 public func addFiles(files: [File]) -> Pitaya { 0075 self.pitayaManager.addFiles(files) 0076 return self 0077 } 0078 0079 /** 0080 add a SSL pinning to check whether undering the Man-in-the-middle attack 0081 0082 - parameter data: data of certification file, .cer format 0083 - parameter SSLValidateErrorCallBack: error callback closure 0084 0085 - returns: self (Pitaya object) 0086 */ 0087 public func addSSLPinning(LocalCertData data: NSData, SSLValidateErrorCallBack: (()->Void)? = nil) -> Pitaya { 0088 self.pitayaManager.addSSLPinning(LocalCertData: data, SSLValidateErrorCallBack: SSLValidateErrorCallBack) 0089 return self 0090 } 0091 0092 /** 0093 set a custom HTTP header 0094 0095 - parameter key: HTTP header key 0096 - parameter value: HTTP header value 0097 0098 - returns: self (Pitaya object) 0099 */ 0100 public func setHTTPHeader(Name key: String, Value value: String) -> Pitaya { 0101 self.pitayaManager.setHTTPHeader(Name: key, Value: value) 0102 return self 0103 } 0104 0105 /** 0106 set HTTP body to what you want. This method will discard any other HTTP body you have built. 0107 0108 - parameter string: HTTP body string you want 0109 - parameter isJSON: is JSON or not: will set "Content-Type" of HTTP request to "application/json" or "text/plain;charset=UTF-8" 0110 0111 - returns: self (Pitaya object) 0112 */ 0113 public func setHTTPBodyRaw(string: String, isJSON: Bool = false) -> Pitaya { 0114 self.pitayaManager.sethttpBodyRaw(string, isJSON: isJSON) 0115 return self 0116 } 0117 0118 /** 0119 set username and password of HTTP Basic Auth to the HTTP request header 0120 0121 - parameter username: username 0122 - parameter password: password 0123 0124 - returns: self (Pitaya object) 0125 */ 0126 public func setBasicAuth(username: String, password: String) -> Pitaya { 0127 self.pitayaManager.setBasicAuth((username, password)) 0128 return self 0129 } 0130 0131 /** 0132 add error callback to self (Pitaya object). 0133 this will called only when network error, if we can receive any data from server, responseData() will be fired. 0134 0135 - parameter errorCallback: errorCallback Closure 0136 0137 - returns: self (Pitaya object) 0138 */ 0139 public func onNetworkError(errorCallback: ((error: NSError) -> Void)) -> Pitaya { 0140 self.pitayaManager.addErrorCallback(errorCallback) 0141 return self 0142 } 0143 0144 /** 0145 async response the http body in NSData type 0146 0147 - parameter callback: callback Closure 0148 - parameter response: void 0149 */ 0150 public func responseData
Pitaya.swift:51 p.pitayaManager = PitayaManager.build(method, url: url)Pitaya.swift:63 self.pitayaManager.addParams(params)Pitaya.swift:75 self.pitayaManager.addFiles(files)Pitaya.swift:88 self.pitayaManager.addSSLPinning(LocalCertData: data, SSLValidateErrorCallBack: SSLValidateErrorCallBack)Pitaya.swift:101 self.pitayaManager.setHTTPHeader(Name: key, Value: value)Pitaya.swift:114 self.pitayaManager.sethttpBodyRaw(string, isJSON: isJSON)Pitaya.swift:127 self.pitayaManager.setBasicAuth((username, password))Pitaya.swift:140 self.pitayaManager.addErrorCallback(errorCallback)Pitaya.swift:151 self.pitayaManager?.fire(callback)Pitaya.swift:193 self.pitayaManager.cancelCallback = callbackPitaya.swift:194 self.pitayaManager.task.cancel()(callback: ((data: NSData?, response: NSHTTPURLResponse?) -> Void)?) { 0151 self.pitayaManager?.fire(callback) 0152 } 0153 0154 /** 0155 async response the http body in String type 0156 0157 - parameter callback: callback Closure 0158 - parameter response: void 0159 */ 0160 public func responseString(callback: ((string: String?, response: NSHTTPURLResponse?) -> Void)?) { 0161 self.responseData { (data, response) -> Void in 0162 var string = "" 0163 if let d = data, 0164 s = NSString(data: d, encoding: NSUTF8StringEncoding) as? String { 0165 string = s 0166 } 0167 callback?(string: string, response: response) 0168 } 0169 } 0170 0171 /** 0172 async response the http body in JSON type use JSONNeverDie(https://github.com/johnlui/JSONNeverDie). 0173 0174 - parameter callback: callback Closure 0175 - parameter response: void 0176 */ 0177 public func responseJSON(callback: ((json: JSONND, response: NSHTTPURLResponse?) -> Void)?) { 0178 self.responseData { (data, response) -> Void in 0179 var json = JSONND() 0180 if let d = data { 0181 json = JSONND.initWithData(d) 0182 } 0183 callback?(json: json, response: response) 0184 } 0185 } 0186 0187 /** 0188 cancel the request. 0189 0190 - parameter callback: callback Closure 0191 */ 0192 public func cancel(callback: (() -> Void)?) { 0193 self.pitayaManager.cancelCallback = callback 0194 self.pitayaManager.task.cancel() 0195 } 0196 }
Pitaya.swift:161 self.responseData { (data, response) -> Void inPitaya.swift:178 self.responseData { (data, response) -> Void in