Skip to content

Commit

Permalink
Merge pull request #21 from 3lvis/feature/logger
Browse files Browse the repository at this point in the history
Feature: logger
  • Loading branch information
3lvis committed Oct 8, 2015
2 parents 6816009 + f6b5dde commit a40e8bc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
64 changes: 60 additions & 4 deletions Source/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public class Networking {
let semaphore = dispatch_semaphore_create(0)
var connectionError: NSError?
var result: AnyObject?
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { data, _, error in
var returnedResponse: NSURLResponse?
var returnedData: NSData?

NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { data, response, error in
returnedResponse = response
returnedData = data

if let data = data {
(result, connectionError) = data.toJSON()
} else if let error = error {
Expand All @@ -45,13 +51,15 @@ public class Networking {
} else {
dispatch_async(dispatch_get_main_queue(), {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
self.logError(data: returnedData, request: request, response: returnedResponse, error: connectionError)
completion(JSON: result, error: connectionError)
})
}
}).resume()

if NSObject.isUnitTesting() {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
self.logError(data: returnedData, request: request, response: returnedResponse, error: connectionError)
completion(JSON: result, error: connectionError)
}
}
Expand Down Expand Up @@ -102,15 +110,21 @@ public class Networking {
var connectionError: NSError?
var result: AnyObject?
let semaphore = dispatch_semaphore_create(0)
var returnedResponse: NSURLResponse?
var returnedData: NSData?

NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { data, _, error in
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { data, response, error in
returnedResponse = response
connectionError = error
returnedData = data

if let data = data {
do {
result = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves)
} catch let serializingError as NSError {
connectionError = serializingError
if error == nil {
connectionError = serializingError
}
}
}

Expand All @@ -119,13 +133,15 @@ public class Networking {
} else {
dispatch_async(dispatch_get_main_queue(), {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
self.logError(params, data: returnedData, request: request, response: returnedResponse, error: connectionError)
completion(JSON: result, error: connectionError)
})
}
}).resume()

if NSObject.isUnitTesting() {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
self.logError(params, data: returnedData, request: request, response: returnedResponse, error: connectionError)
completion(JSON: result, error: connectionError)
}
}
Expand All @@ -136,9 +152,49 @@ public class Networking {
stubsInstance.stubbedPOSTResponses[path] = response
}

// MARK: - Others
// MARK: - Utilities

public func urlForPath(path: String) -> NSURL {
return NSURL(string: self.baseURL + path)!
}

// MARK: - Logging

public func logError(params: AnyObject? = nil, data: NSData?, request: NSURLRequest?, response: NSURLResponse?, error: NSError?) {
guard let error = error else { return }

print(" ")
print("========== Networking Error ==========")
print(" ")

print("Error \(error.code): \(error.description)")
print(" ")

if let request = request {
print("Request: \(request)")
print(" ")
}

if let params = params {
print("Params: \(params)")
print(" ")
}

if let data = data, stringData = NSString(data: data, encoding: NSUTF8StringEncoding) {
print("Data: \(stringData)")
print(" ")
}

if let response = response as? NSHTTPURLResponse {
print("Response status code: \(response.statusCode)")
print(" ")
print("Path: \(response.URL!.absoluteString)")
print(" ")
print("Response: \(response)")
print(" ")
}

print("================= ~ ==================")
print(" ")
}
}
2 changes: 1 addition & 1 deletion Tests/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Tests: XCTestCase {
var success = false

let networking = Networking(baseURL: baseURL)
networking.GET("invalidpath", completion: { JSON, error in
networking.GET("/invalidpath", completion: { JSON, error in
if let JSON: AnyObject = JSON {
fatalError("JSON not nil: \(JSON)")
} else {
Expand Down

0 comments on commit a40e8bc

Please sign in to comment.