https://github.com/ahmetkgunay/networklayer
Protocol Oriented Generic Network Layer example with Alamofire and Swift4
https://github.com/ahmetkgunay/networklayer
alamofire generic-functions generic-programming generics network networking protocol-oriented swift4
Last synced: 15 days ago
JSON representation
Protocol Oriented Generic Network Layer example with Alamofire and Swift4
- Host: GitHub
- URL: https://github.com/ahmetkgunay/networklayer
- Owner: ahmetkgunay
- License: mit
- Created: 2017-10-29T14:04:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-01T16:43:30.000Z (over 7 years ago)
- Last Synced: 2025-03-28T02:11:59.519Z (about 1 month ago)
- Topics: alamofire, generic-functions, generic-programming, generics, network, networking, protocol-oriented, swift4
- Language: Swift
- Size: 118 KB
- Stars: 17
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NetworkLayer
Protocol Oriented Generic Network Layer example with Alamofire and Swift4
## Usage
Usage is so simple:
Just create Request Class and implement the Requestable protocol, in example project i created UserRequest to fetch user info from Github API
Requestable protocol will force you to implement some usefull functions as shown below.
```swift
final class UserRequest: Requestable {
typealias ResponseType = UserResponse
private var userName : String
init(userName:String) {
self.userName = userName
}
var baseUrl: URL {
return URL(string: "https://api.github.com/")!
}var endpoint: String {
return "users/\(self.userName)"
}var method: Network.Method {
return .get
}var query: Network.QueryType {
return .path
}var parameters: [String : Any]? {
return nil
}var headers: [String : String]? {
return defaultJSONHeader
}var timeout: TimeInterval {
return 30.0
}var cachePolicy: NSURLRequest.CachePolicy {
return .reloadIgnoringLocalAndRemoteCacheData
}
}```
Meanwhile you should create also responseModel and implement the Codable protocol your model. Example project shows UserResponse as shown below:
And Thats all! You do not have to decode the response. Network class does it for you.. :)
```swift
public struct UserResponse: Codable {
public let userName: String?
public let userId: Int?
public let avatarUrl: String?enum CodingKeys: String, CodingKey {
case userName = "login", userId = "id", avatarUrl = "avatar_url"
}
}```
#### Request from ViewController
```swift
override func viewDidLoad() {
super.viewDidLoad()// Assign request instance to cancel in future
// Just call cancel() to assigned instance
_ = Network.request(req: UserRequest(userName: "ahmetkgunay")) { (result) inswitch result {
case .success(let userResponse):
print(userResponse)
case .cancel(let cancelError):
print(cancelError!)
case .failure(let error):
print(error!)
}
}
}```
## Installation
- By cloning the project into your repository
## Author
Ahmet Kazım Günay, [email protected]
## License
NetworkLayer is available under the MIT license. See the LICENSE file for more info.