Created
August 16, 2017 15:57
-
-
Save rldaulton/5d9956d75b18f07d041699f88acff6a1 to your computer and use it in GitHub Desktop.
A simple Swift network management file
This file contains 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
// | |
// NetworkManager.swift | |
// | |
// Created by Ryan Daulton on 8/15/17. | |
// Copyright © 2017 Ryan Daulton. All rights reserved. | |
// | |
import Foundation | |
import Alamofire | |
import SwiftyJSON | |
enum Endpoint { | |
case getAllObjects | |
case getObjectById(Id: String) | |
// MARK: - Public Properties in case we expand this | |
var method: Alamofire.HTTPMethod { | |
switch self { | |
case .getAllObjects: | |
return .get | |
case .getObjectById: | |
return .get | |
} | |
} | |
var url: URLConvertible { | |
let baseUrl = URL.getBaseUrl() | |
switch self { | |
case .getAllObjects: | |
return baseUrl.appendingPathComponent("/all") | |
case .getObjectById(let Id): | |
return baseUrl.appendingPathComponent("/\(Id)") | |
} | |
} | |
} | |
private extension URL { | |
static func getBaseUrl() -> URL { | |
guard let info = Bundle.main.infoDictionary, | |
let urlString = info["Base URL"] as? String, | |
let url = URL(string: urlString) else { | |
fatalError("Cannot get base url from info.plist") | |
} | |
return url | |
} | |
} | |
final class ObjectAPI { | |
// MARK: - Public Methods | |
func getAllObjectItems(completionHandler: @escaping (_ responseObject: Data?, _ error: Error?) -> ()) { | |
Alamofire.request(Endpoint.getAllObjects.url).responseData { response in | |
debugPrint("All Response Info: \(response)") | |
guard let data = response.result.value, response.error == nil else { | |
completionHandler(nil, response.error) | |
return | |
} | |
completionHandler(data, nil) | |
} | |
} | |
func getObjectItemByID(ID: String, completionHandler: @escaping (_ responseObject: Data?, _ error: Error?) -> ()) { | |
Alamofire.request(Endpoint.getObjectById(Id: ID).url).responseData { response in | |
debugPrint("All Response Info: \(response)") | |
guard let data = response.result.value, response.error == nil else { | |
completionHandler(nil, response.error) | |
return | |
} | |
completionHandler(data, nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Use of Above Network Manager
Begin by creating some sort of
DataManager
class and initialize it from theAppDelegate
using anActivate()
method:Then, using a private initializer, call a
parsed()
method to begin pulling & parsing data from your endpoint. A post notification is thrown on completion, which can be received elsewhere in your app to handle the ingested data.An example of the parsing function that uses your Network Manager:
And finally, here would be an example of the remaining file structure to wrap up the
DataManager
class and include some helpful extensions: