Created
September 2, 2016 19:34
-
-
Save DreamingInBinary/42845a15cf510ed39c837bacae488551 to your computer and use it in GitHub Desktop.
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
// | |
// IGNNetworking.swift | |
// Unofficial Metacritic | |
// | |
// Created by Jordan Morgan on 8/21/16. | |
// Copyright © 2016 Dreaming In Binary, LLC. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
typealias JSONDictionary = [String: AnyObject] | |
class Networking : NSObject | |
{ | |
private let defaultSession:NSURLSession | |
private var dataTask: NSURLSessionDataTask? | |
//MARK: Initializers | |
override init() | |
{ | |
defaultSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) | |
super.init() | |
} | |
init(withAdditionalHeaders headers:[String:AnyObject]) | |
{ | |
let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() | |
sessionConfiguration.HTTPAdditionalHeaders = headers | |
defaultSession = NSURLSession(configuration: sessionConfiguration) | |
super.init() | |
} | |
//MARK: GET | |
internal func GET(with givenURL:NSURL, completion: (JSONDictionary?, NetworkError?) -> ()) | |
{ | |
UIApplication.sharedApplication().networkActivityIndicatorVisible = true | |
if dataTask != nil | |
{ | |
dataTask?.cancel() | |
} | |
dataTask = defaultSession.dataTaskWithURL(givenURL) { data, response, error in | |
dispatch_async(dispatch_get_main_queue()) | |
{ | |
UIApplication.sharedApplication().networkActivityIndicatorVisible = false | |
} | |
if let error = error | |
{ | |
completion(nil, NetworkError(message: error.localizedDescription, errorType: .GeneralError)) | |
} | |
else if let httpResponse = response as? NSHTTPURLResponse | |
{ | |
guard httpResponse.statusCode == 200 else | |
{ | |
completion(nil, NetworkError(message: "Received an unexpected response from the server.", errorType: .BadResponse)) | |
return | |
} | |
do | |
{ | |
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSDictionary | |
if let response = json as? JSONDictionary | |
{ | |
completion(response, nil) | |
} | |
else | |
{ | |
completion(nil, NetworkError(message: "Unable to parse JSON.", errorType: .JSONSerialization)) | |
} | |
} | |
catch | |
{ | |
completion(nil, NetworkError(message: "Unable to parse JSON.", errorType: .JSONSerialization)) | |
} | |
} | |
} | |
dataTask?.resume() | |
} | |
//MARK: POST String Sanitization | |
private func sanitizeText(from query:String) -> String | |
{ | |
let expectedCharSet = NSCharacterSet.URLQueryAllowedCharacterSet() | |
return query.stringByAddingPercentEncodingWithAllowedCharacters(expectedCharSet)! | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment