Last active
March 7, 2017 21:12
-
-
Save chriswebb09/40360564e510b1e3e4783cfebc755072 to your computer and use it in GitHub Desktop.
Networking With Protocols Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol APIClient { | |
func downloadData() | |
func downloadImage() | |
} | |
struct Client:APIClient { | |
var request: URLRequest | |
var networkCall: APIRequest | |
init(request: URLRequest, networkCall: APIRequest) { | |
self.request = request | |
self.networkCall = networkCall | |
} | |
internal func downloadImage() { | |
networkCall.sendAPICall(urlRequest: networkCall.urlRequest) { json in | |
print(json) | |
} | |
} | |
internal func downloadData() { | |
networkCall.sendAPICall(urlRequest: networkCall.urlRequest) { json in | |
print(json) | |
} | |
} | |
} | |
class Mock { | |
var client: Client | |
init(client: Client) { | |
self.client = client | |
} | |
func makeCalls() { | |
let urlString = "http://www.google.com" | |
let construct = ConstructAPIRequest(urlString: urlString) | |
client.request = construct.build() | |
client.downloadData() | |
client.downloadImage() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol APIRequest { | |
var urlRequest:URLRequest { get } | |
func sendAPICall(urlRequest:URLRequest, completion: @escaping (JSONData)->()) | |
} | |
struct APICall: APIRequest { | |
var urlRequest: URLRequest | |
var session: URLSession | |
init(request:URLRequest) { | |
self.urlRequest = request | |
self.session = URLSession.shared | |
} | |
func sendAPICall(urlRequest: URLRequest, completion: @escaping (JSONData) -> ()) { | |
session.dataTask(with: urlRequest, completionHandler: { data, response, error in | |
guard let data = data else { return } | |
let json = try! JSONSerialization.jsonObject(with: data, options: .allowFragments) | |
guard let jsonData = json as? JSONData else { return } | |
completion(jsonData) | |
}).resume() | |
} | |
} | |
struct ConstructAPIRequest { | |
var urlString: String | |
var url: URL { | |
return URL(string: urlString)! | |
} | |
init(urlString: String) { | |
self.urlString = urlString | |
} | |
func build() -> URLRequest { | |
var urlRequest = URLRequest(url: url) | |
urlRequest.httpMethod = Method.GET.method | |
return urlRequest | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Response { | |
var error: Error? | |
var result: JSONData? | |
var responseCode: Int? | |
init(data:JSONData?, error: Error?, response: Int?) { | |
self.responseCode = response | |
self.result = data | |
self.error = error | |
} | |
} | |
struct APIResponse { | |
var errorMessage: String? | |
var statusCode: Int = 200 | |
var json: JSONData? | |
init(status: Int = 200, error: String? = nil, data: JSONData? = nil) { | |
self.errorMessage = error | |
self.statusCode = status | |
self.json = data | |
} | |
} | |
extension APIResponse { | |
init(response: Response) { | |
if let responseData = response.result { json = responseData } | |
if let responseError = response.error { errorMessage = responseError.localizedDescription; statusCode = 400 } | |
if let code = response.responseCode { statusCode = code } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
typealias JSONData = [String:Any] | |
typealias RequestParams = [String : AnyObject] | |
enum Method { | |
case GET, POST | |
var method: String { | |
switch self { | |
case .GET: | |
return "GET" | |
case .POST: | |
return "POST" | |
} | |
} | |
} | |
protocol ClientRouter { | |
var method: Method { get } | |
var encoding: URLComponents { get } | |
var path: String { get } | |
var parameters:RequestParams { get } | |
var baseURL: String { get } | |
} | |
extension ClientRouter { | |
var urlRequest: URLRequest { | |
let baseUrl = URL(string: baseURL) | |
var urlRequest = URLRequest(url: baseUrl!) | |
urlRequest.httpMethod = Method.GET.method | |
return urlRequest | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment