Skip to content

Latest commit

 

History

History
137 lines (111 loc) · 4.77 KB

README.md

File metadata and controls

137 lines (111 loc) · 4.77 KB

SNPNetwork

CI Status Version License Platform

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.

Take a look inside!

Here we describe methods of SNPNetwork that do discussioned tasks.

  1. 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)
  1. 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"
  1. 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"))

Example usage:

  1. set default headers:
SNPNetwork.shared.setDefaultHeaders(headers: ["iOS-Ver":"11.0"])
  1. 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!)
}
}
  1. 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!)
})

Getting Started

1.git clone https://github.com/snapp-cab/SNPNetwork.git
2.run pod install in the iOS project.

pod 'SNPNetwork'

Author

Arash Z. Jahangiri, arash.jahangiri@snapp.cab

Questions

If you have any questions about the project, please contact via email: arash.jahangiri@snapp.cab

Pull requests are welcome!

License

SNPNetwork is available under the MIT license. See the LICENSE file for more info.