SNPNetwork is a Swift-based HTTP networking library for iOS. It provides an interface on top of Alamofire that simplifies a number of common networking tasks. We've created it to add some features we needed in Snapp application which is not supported directly in Alamofire. For example it has a super-simplified JSON parsing facility that gives you clearer syntax by set it's 'responseKey' parameter. To modularize our architecture we release it as Pod file so that you can add it to your project. Let's see what's inside SNPNetwork and how you can use it. It also has another functionality for download tasks.
Here we describe methods of SNPNetwork that do discussioned tasks.
- Before you start to call APIs using 'requsest' function you can call below function to set all of your default headers, necessary headers, for all of your requests. One of best places to set default headers is in AppDelegate.
Parameters:
- headers: is a dictionary like this [String: String]
class func setDefaultHeaders(headers: HTTPHeaders)
- common networking tasks:
class func request<T: Decodable, E: SNPError>(url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil,
responseKey: String? = nil,
appendDefaultHeaders: Bool = true,
completion: @escaping (T?, E?) -> Void)
As you can see request is a generic function that returns expected model you want and also an error of type 'SNPError'. It is a class function which means that without making instance of SNPNetwork class you can use it anywhere.(SNPNetwork.request("www.test.com"))
Parameters:
- url: url of interest to retrieve data. It should be String
- method: is the type of request you look for and is an enumeration like this:
public enum HTTPMethod: String {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
case trace = "TRACE"
case connect = "CONNECT"
}
- parameters: is a dictionary like this [String: Any]
- headers: is a dictionary like this [String: String]
- appendDefaultHeaders is of type Bool. If you set default headers for request, this flag is responsible to append defaultHeader to 'headers' parameter by default, else(appendDefaultHeaders = false) default headers will be replaced with 'headers' parameter.
- responseKey: is expected path of response and will be like "Data.Information.Employee.Person"
- download tasks:
class func download(_ url: String,
progress: ((_ progress: Double) -> Void)?,
completion: @escaping (_ status: String?) -> Void)
- url: url of interest to download data. It should be String
- progress: show progress of downloading file
As you can see download is a class function which means that without making instance of SNPNetwork class you can use it anywhere.(SNPNetwork.download("www.test.com"))
- set default headers:
SNPNetwork.shared.setDefaultHeaders(headers: ["iOS-Ver":"11.0"])
- common networking tasks:
SNPNetwork.shared.request(url: "www.test.com",
method: .post,
parameters: parameters(),
encoding: JSONEncoding.default,
responseKey: "data") { (config: Config?, error: SNPError?) in
if error == nil {
self.config = config
} else {
print(error!)
}
}
--OR--
SNPNetwork.shared.request(url: "www.test.com") { (config: Config?, error: SNPError?) in
if error == nil {
self.config = config
} else {
print(error!)
}
}
- download tasks:
SNPNetwork.shared.download("www.test.com", progress: {(progress) in
print(progress)
}, completion: {(status) in
print(status!)
})
--OR--
SNPNetwork.shared.download("www.test.com", progress: nil, completion: {(status) in
print(status!)
})
1.git clone https://github.com/snapp-cab/SNPNetwork.git
2.run pod install in the iOS project.
pod 'SNPNetwork'
Arash Z. Jahangiri, [email protected]
If you have any questions about the project, please contact via email: [email protected]
Pull requests are welcome!
SNPNetwork is available under the MIT license. See the LICENSE file for more info.