Commit 343e47f7 authored by Drew's avatar Drew

initial commit

parent 09a4367b
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
Carthage/Build
Carthage/Checkouts
DCAFoundationAdditions © 2015 DrewCrawfordApps LLC.
DrewCrawfordApps clarifies that we understand binary applications to be a
"copy or substantial portion of the Software".
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\ No newline at end of file
......@@ -10,6 +10,8 @@
3A0D8F841BB1299100A8ED0C /* SynchronousRequestKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A0D8F831BB1299100A8ED0C /* SynchronousRequestKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
3A0D8F8B1BB1299100A8ED0C /* SynchronousRequestKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A0D8F801BB1299100A8ED0C /* SynchronousRequestKit.framework */; settings = {ASSET_TAGS = (); }; };
3A0D8F901BB1299100A8ED0C /* SynchronousRequestKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A0D8F8F1BB1299100A8ED0C /* SynchronousRequestKitTests.swift */; };
3A0D8F9B1BB129B900A8ED0C /* NSURLSession+Synchronous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A0D8F9A1BB129B900A8ED0C /* NSURLSession+Synchronous.swift */; settings = {ASSET_TAGS = (); }; };
3A0D8FC31BB12B1400A8ED0C /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = 3A0D8FC21BB12B1400A8ED0C /* LICENSE */; settings = {ASSET_TAGS = (); }; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
......@@ -29,6 +31,8 @@
3A0D8F8A1BB1299100A8ED0C /* SynchronousRequestKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SynchronousRequestKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3A0D8F8F1BB1299100A8ED0C /* SynchronousRequestKitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronousRequestKitTests.swift; sourceTree = "<group>"; };
3A0D8F911BB1299100A8ED0C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3A0D8F9A1BB129B900A8ED0C /* NSURLSession+Synchronous.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSURLSession+Synchronous.swift"; sourceTree = "<group>"; };
3A0D8FC21BB12B1400A8ED0C /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -53,6 +57,7 @@
3A0D8F761BB1299100A8ED0C = {
isa = PBXGroup;
children = (
3A0D8FC21BB12B1400A8ED0C /* LICENSE */,
3A0D8F821BB1299100A8ED0C /* SynchronousRequestKit */,
3A0D8F8E1BB1299100A8ED0C /* SynchronousRequestKitTests */,
3A0D8F811BB1299100A8ED0C /* Products */,
......@@ -73,6 +78,7 @@
children = (
3A0D8F831BB1299100A8ED0C /* SynchronousRequestKit.h */,
3A0D8F851BB1299100A8ED0C /* Info.plist */,
3A0D8F9A1BB129B900A8ED0C /* NSURLSession+Synchronous.swift */,
);
path = SynchronousRequestKit;
sourceTree = "<group>";
......@@ -142,6 +148,7 @@
3A0D8F771BB1299100A8ED0C /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = DrewCrawfordApps;
TargetAttributes = {
......@@ -176,6 +183,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3A0D8FC31BB12B1400A8ED0C /* LICENSE in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -193,6 +201,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3A0D8F9B1BB129B900A8ED0C /* NSURLSession+Synchronous.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -302,6 +311,7 @@
3A0D8F951BB1299100A8ED0C /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
......@@ -314,12 +324,14 @@
PRODUCT_BUNDLE_IDENTIFIER = com.dca.SynchronousRequestKit;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
3A0D8F961BB1299100A8ED0C /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
......
......@@ -5,6 +5,22 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A0D8F7F1BB1299100A8ED0C"
BuildableName = "SynchronousRequestKit.framework"
BlueprintName = "SynchronousRequestKit"
ReferencedContainer = "container:SynchronousRequestKit.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
......@@ -12,7 +28,26 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A0D8F891BB1299100A8ED0C"
BuildableName = "SynchronousRequestKitTests.xctest"
BlueprintName = "SynchronousRequestKitTests"
ReferencedContainer = "container:SynchronousRequestKit.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A0D8F7F1BB1299100A8ED0C"
BuildableName = "SynchronousRequestKit.framework"
BlueprintName = "SynchronousRequestKit"
ReferencedContainer = "container:SynchronousRequestKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
......@@ -26,6 +61,15 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A0D8F7F1BB1299100A8ED0C"
BuildableName = "SynchronousRequestKit.framework"
BlueprintName = "SynchronousRequestKit"
ReferencedContainer = "container:SynchronousRequestKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
......@@ -35,6 +79,15 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A0D8F7F1BB1299100A8ED0C"
BuildableName = "SynchronousRequestKit.framework"
BlueprintName = "SynchronousRequestKit"
ReferencedContainer = "container:SynchronousRequestKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
......
//
// NSURLSession+Synchronous.swift
// SynchronousRequestKit
//
// This file is part of SynchronousRequestKit. It is subject to the license terms in the LICENSE
// file found in the top level of this distribution
// No part of SynchronousRequestKit, including this file, may be copied, modified,
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
import Foundation
private var cassettes : [SynchronousDataTask] = []
/**Add a cassette to be played back on the next request. This is primarily used for testing purposes.*/
public func addCassette(string string: String) {
let response = NSHTTPURLResponse(URL: NSURL(), statusCode: 200, HTTPVersion: nil, headerFields: nil)
let cassette = SynchronousDataTask(task: NSURLSessionDataTask(), error: nil, data: string.dataUsingEncoding(NSUTF8StringEncoding), response:response)
cassettes.append(cassette)
}
public func addCassette(path path: String, testClass: AnyClass) {
let path = NSBundle(forClass: testClass).pathForResource(path, ofType: "cassette")
let data = NSData(contentsOfFile: path!)!
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
addCassette(string: str as! String)
}
enum SynchronousDataTaskError : ErrorType {
case NoData
case BadHTTPStatus(code: Int, body: String?)
case NotHTTPResponse
case NoResponse
case NotUTF8
case NotJSONDict
}
import Foundation
private let errorDomain = "SynchronousDataTaskErrorDomain"
public final class SynchronousDataTask {
public var task: NSURLSessionDataTask
/**The error, if any, returned by Cocoa. If you want a better error, try errorOrData/errorOrString. */
public var underlyingCocoaError: NSError?
public var underlyingCocoaData: NSData?
public var underlyingCocoaResponse: NSURLResponse?
private init(task: NSURLSessionDataTask, error: NSError?, data: NSData?, response:NSURLResponse?) {
self.underlyingCocoaData = data
self.underlyingCocoaError = error
self.underlyingCocoaResponse = response
self.task = task
}
/**Performs some basic error checking on the result.
This function tries to get some data for the request.
*/
public func getData() throws -> NSData {
if let cocoaError = underlyingCocoaError { //may have an error already
throw cocoaError
}
if underlyingCocoaResponse == nil {
throw SynchronousDataTaskError.NoResponse
}
guard let myResponse = underlyingCocoaResponse as? NSHTTPURLResponse else {
throw SynchronousDataTaskError.NotHTTPResponse
}
if myResponse.statusCode < 200 || myResponse.statusCode >= 300 {
throw SynchronousDataTaskError.BadHTTPStatus(code: myResponse.statusCode, body: _string)
}
guard let d = underlyingCocoaData else {
throw SynchronousDataTaskError.NoData
}
return d
}
/**Gets the string, if available. Performs as little error checking as possible.
- note: If *any* string can be returned for the request, this function will do it. This is useful if you want to return a string as part of an error message.*/
var _string : String? {
get {
guard let data = underlyingCocoaData else { return nil }
guard let str = NSString(data: data, encoding: NSUTF8StringEncoding) else { return nil }
return str as String
}
}
/**Performs some error checking on the result.
This function tries to get a UTF-8 string for the request.
*/
public func getString() throws -> String {
let data = try self.getData()
guard let str = NSString(data: data, encoding: NSUTF8StringEncoding) else { throw SynchronousDataTaskError.NotUTF8 }
return str as String
}
/**This function tries to get a JSON dictionary for the request. */
public func getJsonDict() throws -> [String: AnyObject] {
let data = try getData()
let jsonObj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
if let j = jsonObj as? [String: AnyObject] {
return j
}
throw SynchronousDataTaskError.NotJSONDict
}
/**This checks the response to see if it's a well-known string. */
public func isParticularError(code code: Int, body: String) -> Bool {
guard let myResponse = underlyingCocoaResponse as? NSHTTPURLResponse else { return false }
if myResponse.statusCode != code {
return false
}
guard let u = underlyingCocoaData else {
return false
}
let str = NSString(data: u, encoding: NSUTF8StringEncoding)
guard let s = str else { return false }
if s != body {
return false
}
return true
}
}
extension NSURLSession {
public func synchronousDataRequestWithRequest(request: NSURLRequest) -> SynchronousDataTask {
if cassettes.count > 0 {
return cassettes.removeFirst()
}
let sema = dispatch_semaphore_create(0)
var data : NSData?
var response : NSURLResponse?
var error : NSError?
let task = self.dataTaskWithRequest(request) { (ldata, lresponse, lerror) -> Void in
data = ldata
response = lresponse
error = lerror
dispatch_semaphore_signal(sema);
return;
}
task.resume()
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
return SynchronousDataTask(task: task, error: error, data: data, response: response)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment