diff --git a/Alamofire-SwiftyJSON watchOS/Alamofire-SwiftyJSON-watchOS.swift b/Alamofire-SwiftyJSON watchOS/Alamofire-SwiftyJSON-watchOS.swift new file mode 100644 index 0000000..8a04050 --- /dev/null +++ b/Alamofire-SwiftyJSON watchOS/Alamofire-SwiftyJSON-watchOS.swift @@ -0,0 +1,87 @@ +// +// Alamofire-SwiftyJSON-watchOS.swift +// Alamofire-SwiftyJSON watchOS +// +// Created by Sam Eckert on 26.10.18. +// Copyright © 2018 SwiftJSON. All rights reserved. +// + +import Foundation + +import Alamofire +import SwiftyJSON + +// MARK: - Request for Swift JSON + +extension Request { + /// Returns a SwiftyJSON object contained in a result type constructed from the response data using `JSONSerialization` + /// with the specified reading options. + /// + /// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`. + /// - parameter response: The response from the server. + /// - parameter data: The data returned from the server. + /// - parameter error: The error already encountered if it exists. + /// + /// - returns: The result data type. + public static func serializeResponseSwiftyJSON( + options: JSONSerialization.ReadingOptions, + response: HTTPURLResponse?, + data: Data?, + error: Error?) + -> Result + { + guard error == nil else { return .failure(error!) } + + if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(JSON.null) } + + guard let validData = data, validData.count > 0 else { + return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)) + } + + do { + let json = try JSONSerialization.jsonObject(with: validData, options: options) + return .success(JSON(json)) + } catch { + return .failure(AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error))) + } + } +} + +extension DataRequest { + /// Creates a response serializer that returns a SwiftyJSON object result type constructed from the response data using + /// `JSONSerialization` with the specified reading options. + /// + /// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`. + /// + /// - returns: A JSON object response serializer. + public static func swiftyJSONResponseSerializer( + options: JSONSerialization.ReadingOptions = .allowFragments) + -> DataResponseSerializer + { + return DataResponseSerializer { _, response, data, error in + return Request.serializeResponseSwiftyJSON(options: options, response: response, data: data, error: error) + } + } + + /// Adds a handler to be called once the request has finished. + /// + /// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`. + /// - parameter completionHandler: A closure to be executed once the request has finished. + /// + /// - returns: The request. + @discardableResult + public func responseSwiftyJSON( + queue: DispatchQueue? = nil, + options: JSONSerialization.ReadingOptions = .allowFragments, + completionHandler: @escaping (DataResponse) -> Void) + -> Self + { + return response( + queue: queue, + responseSerializer: DataRequest.swiftyJSONResponseSerializer(options: options), + completionHandler: completionHandler + ) + } +} + +private let emptyDataStatusCodes: Set = [204, 205] diff --git a/Alamofire-SwiftyJSON watchOS/Alamofire_SwiftyJSON_watchOS.h b/Alamofire-SwiftyJSON watchOS/Alamofire_SwiftyJSON_watchOS.h new file mode 100644 index 0000000..1367a70 --- /dev/null +++ b/Alamofire-SwiftyJSON watchOS/Alamofire_SwiftyJSON_watchOS.h @@ -0,0 +1,19 @@ +// +// Alamofire_SwiftyJSON_watchOS.h +// Alamofire-SwiftyJSON watchOS +// +// Created by Sam Eckert on 26.10.18. +// Copyright © 2018 SwiftJSON. All rights reserved. +// + +#import + +//! Project version number for Alamofire_SwiftyJSON_watchOS. +FOUNDATION_EXPORT double Alamofire_SwiftyJSON_watchOSVersionNumber; + +//! Project version string for Alamofire_SwiftyJSON_watchOS. +FOUNDATION_EXPORT const unsigned char Alamofire_SwiftyJSON_watchOSVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + + diff --git a/Alamofire-SwiftyJSON watchOS/Info.plist b/Alamofire-SwiftyJSON watchOS/Info.plist new file mode 100644 index 0000000..e1fe4cf --- /dev/null +++ b/Alamofire-SwiftyJSON watchOS/Info.plist @@ -0,0 +1,22 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + + diff --git a/Alamofire-SwiftyJSON.xcodeproj/project.pbxproj b/Alamofire-SwiftyJSON.xcodeproj/project.pbxproj index e703136..c7d070e 100644 --- a/Alamofire-SwiftyJSON.xcodeproj/project.pbxproj +++ b/Alamofire-SwiftyJSON.xcodeproj/project.pbxproj @@ -7,8 +7,8 @@ objects = { /* Begin PBXBuildFile section */ - A8A2DBBA1A23292000AF9EFC /* Alamofire.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A8AFB75A19D14A5C0070765E /* Alamofire.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - A8A2DBBB1A23292000AF9EFC /* SwiftyJSON.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A8AFB76519D14A670070765E /* SwiftyJSON.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 221EFD6B21828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 221EFD6921828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 221EFD8021828B3B00E48923 /* Alamofire-SwiftyJSON-watchOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 221EFD7F21828B3B00E48923 /* Alamofire-SwiftyJSON-watchOS.swift */; }; A8A2DBC31A2329E700AF9EFC /* Alamofire.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A8AFB75A19D14A5C0070765E /* Alamofire.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; A8A2DBC41A2329E700AF9EFC /* SwiftyJSON.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A8AFB76519D14A670070765E /* SwiftyJSON.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; A8A2DBC51A2329E700AF9EFC /* AlamofireSwiftyJSON.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A8AFB70F19D1401D0070765E /* AlamofireSwiftyJSON.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -18,6 +18,20 @@ /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ + 221EFD912182908800E48923 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = A8AFB75419D14A5C0070765E /* Alamofire.xcodeproj */; + proxyType = 1; + remoteGlobalIDString = E4202FCD1B667AA100C997FB; + remoteInfo = "Alamofire watchOS"; + }; + 221EFD932182908C00E48923 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = A8AFB75D19D14A670070765E /* SwiftyJSON.xcodeproj */; + proxyType = 1; + remoteGlobalIDString = E4D7CCDE1B9465A700EE7221; + remoteInfo = "SwiftyJSON watchOS"; + }; 4BC5E9F91BB7E15600A393DD /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = A8AFB75419D14A5C0070765E /* Alamofire.xcodeproj */; @@ -154,18 +168,6 @@ /* End PBXContainerItemProxy section */ /* Begin PBXCopyFilesBuildPhase section */ - A82ED83919ED37D300A502B1 /* Copy Frameworks */ = { - isa = PBXCopyFilesBuildPhase; - buildActionMask = 8; - dstPath = ""; - dstSubfolderSpec = 10; - files = ( - A8A2DBBA1A23292000AF9EFC /* Alamofire.framework in Copy Frameworks */, - A8A2DBBB1A23292000AF9EFC /* SwiftyJSON.framework in Copy Frameworks */, - ); - name = "Copy Frameworks"; - runOnlyForDeploymentPostprocessing = 1; - }; A8A2DBC21A2329CE00AF9EFC /* Copy Frameworks */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 12; @@ -182,6 +184,10 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 221EFD6721828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Alamofire_SwiftyJSON_watchOS.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 221EFD6921828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Alamofire_SwiftyJSON_watchOS.h; sourceTree = ""; }; + 221EFD6A21828A8B00E48923 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 221EFD7F21828B3B00E48923 /* Alamofire-SwiftyJSON-watchOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Alamofire-SwiftyJSON-watchOS.swift"; sourceTree = ""; }; A8AFB70F19D1401D0070765E /* AlamofireSwiftyJSON.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AlamofireSwiftyJSON.framework; sourceTree = BUILT_PRODUCTS_DIR; }; A8AFB71319D1401D0070765E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; A8AFB71419D1401D0070765E /* Alamofire-SwiftyJSON.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Alamofire-SwiftyJSON.h"; sourceTree = ""; }; @@ -194,6 +200,13 @@ /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + 221EFD6421828A8B00E48923 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; A8AFB70B19D1401D0070765E /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -211,6 +224,16 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 221EFD6821828A8B00E48923 /* Alamofire-SwiftyJSON watchOS */ = { + isa = PBXGroup; + children = ( + 221EFD6921828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.h */, + 221EFD7F21828B3B00E48923 /* Alamofire-SwiftyJSON-watchOS.swift */, + 221EFD6A21828A8B00E48923 /* Info.plist */, + ); + path = "Alamofire-SwiftyJSON watchOS"; + sourceTree = ""; + }; A8AFB70519D1401D0070765E = { isa = PBXGroup; children = ( @@ -218,6 +241,7 @@ A8AFB75D19D14A670070765E /* SwiftyJSON.xcodeproj */, A8AFB71119D1401D0070765E /* Source */, A8AFB71B19D1401D0070765E /* Tests */, + 221EFD6821828A8B00E48923 /* Alamofire-SwiftyJSON watchOS */, A8AFB71019D1401D0070765E /* Products */, ); sourceTree = ""; @@ -227,6 +251,7 @@ children = ( A8AFB70F19D1401D0070765E /* AlamofireSwiftyJSON.framework */, A8AFB71A19D1401D0070765E /* AlamofireSwiftyJSONTests.xctest */, + 221EFD6721828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.framework */, ); name = Products; sourceTree = ""; @@ -297,6 +322,14 @@ /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ + 221EFD6221828A8B00E48923 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 221EFD6B21828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; A8AFB70C19D1401D0070765E /* Headers */ = { isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; @@ -308,6 +341,26 @@ /* End PBXHeadersBuildPhase section */ /* Begin PBXNativeTarget section */ + 221EFD6621828A8B00E48923 /* Alamofire-SwiftyJSON watchOS */ = { + isa = PBXNativeTarget; + buildConfigurationList = 221EFD7C21828A8B00E48923 /* Build configuration list for PBXNativeTarget "Alamofire-SwiftyJSON watchOS" */; + buildPhases = ( + 221EFD6221828A8B00E48923 /* Headers */, + 221EFD6321828A8B00E48923 /* Sources */, + 221EFD6421828A8B00E48923 /* Frameworks */, + 221EFD6521828A8B00E48923 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + 221EFD942182908C00E48923 /* PBXTargetDependency */, + 221EFD922182908800E48923 /* PBXTargetDependency */, + ); + name = "Alamofire-SwiftyJSON watchOS"; + productName = "Alamofire-SwiftyJSON watchOS"; + productReference = 221EFD6721828A8B00E48923 /* Alamofire_SwiftyJSON_watchOS.framework */; + productType = "com.apple.product-type.framework"; + }; A8AFB70E19D1401D0070765E /* AlamofireSwiftyJSON */ = { isa = PBXNativeTarget; buildConfigurationList = A8AFB72219D1401D0070765E /* Build configuration list for PBXNativeTarget "AlamofireSwiftyJSON" */; @@ -316,7 +369,6 @@ A8AFB70B19D1401D0070765E /* Frameworks */, A8AFB70C19D1401D0070765E /* Headers */, A8AFB70D19D1401D0070765E /* Resources */, - A82ED83919ED37D300A502B1 /* Copy Frameworks */, ); buildRules = ( ); @@ -360,6 +412,11 @@ LastUpgradeCheck = 0900; ORGANIZATIONNAME = SwiftJSON; TargetAttributes = { + 221EFD6621828A8B00E48923 = { + CreatedOnToolsVersion = 10.0; + LastSwiftMigration = 1000; + ProvisioningStyle = Automatic; + }; A8AFB70E19D1401D0070765E = { CreatedOnToolsVersion = 6.0.1; LastSwiftMigration = 0820; @@ -394,6 +451,7 @@ targets = ( A8AFB70E19D1401D0070765E /* AlamofireSwiftyJSON */, A8AFB71919D1401D0070765E /* AlamofireSwiftyJSONTests */, + 221EFD6621828A8B00E48923 /* Alamofire-SwiftyJSON watchOS */, ); }; /* End PBXProject section */ @@ -500,6 +558,13 @@ /* End PBXReferenceProxy section */ /* Begin PBXResourcesBuildPhase section */ + 221EFD6521828A8B00E48923 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; A8AFB70D19D1401D0070765E /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; @@ -517,6 +582,14 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ + 221EFD6321828A8B00E48923 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 221EFD8021828B3B00E48923 /* Alamofire-SwiftyJSON-watchOS.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; A8AFB70A19D1401D0070765E /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -536,6 +609,16 @@ /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ + 221EFD922182908800E48923 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + name = "Alamofire watchOS"; + targetProxy = 221EFD912182908800E48923 /* PBXContainerItemProxy */; + }; + 221EFD942182908C00E48923 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + name = "SwiftyJSON watchOS"; + targetProxy = 221EFD932182908C00E48923 /* PBXContainerItemProxy */; + }; A8A2DBB71A23290100AF9EFC /* PBXTargetDependency */ = { isa = PBXTargetDependency; name = Alamofire; @@ -564,6 +647,83 @@ /* End PBXTargetDependency section */ /* Begin XCBuildConfiguration section */ + 221EFD6C21828A8B00E48923 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + APPLICATION_EXTENSION_API_ONLY = NO; + CLANG_ANALYZER_NONNULL = YES_NONAGGRESSIVE; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_WEAK = NO; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = NO; + CLANG_WARN_DOCUMENTATION_COMMENTS = NO; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = NO; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + GCC_C_LANGUAGE_STANDARD = gnu99; + INFOPLIST_FILE = "Alamofire-SwiftyJSON watchOS/Info.plist"; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + MTL_ENABLE_DEBUG_INFO = YES; + MTL_FAST_MATH = YES; + PRODUCT_BUNDLE_IDENTIFIER = "com.sam0711er.Alamofire-SwiftyJSON-watchOS"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + SDKROOT = watchos; + SKIP_INSTALL = YES; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ""; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.2; + TARGETED_DEVICE_FAMILY = 4; + WATCHOS_DEPLOYMENT_TARGET = 3.0; + }; + name = Debug; + }; + 221EFD6D21828A8B00E48923 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + APPLICATION_EXTENSION_API_ONLY = NO; + CLANG_ANALYZER_NONNULL = YES_NONAGGRESSIVE; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_WEAK = NO; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = NO; + CLANG_WARN_DOCUMENTATION_COMMENTS = NO; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = NO; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + GCC_C_LANGUAGE_STANDARD = gnu99; + INFOPLIST_FILE = "Alamofire-SwiftyJSON watchOS/Info.plist"; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + MTL_FAST_MATH = YES; + PRODUCT_BUNDLE_IDENTIFIER = "com.sam0711er.Alamofire-SwiftyJSON-watchOS"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + SDKROOT = watchos; + SKIP_INSTALL = YES; + SWIFT_VERSION = 4.2; + TARGETED_DEVICE_FAMILY = 4; + WATCHOS_DEPLOYMENT_TARGET = 3.0; + }; + name = Release; + }; A8AFB72019D1401D0070765E /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -734,6 +894,15 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ + 221EFD7C21828A8B00E48923 /* Build configuration list for PBXNativeTarget "Alamofire-SwiftyJSON watchOS" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 221EFD6C21828A8B00E48923 /* Debug */, + 221EFD6D21828A8B00E48923 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; A8AFB70919D1401D0070765E /* Build configuration list for PBXProject "Alamofire-SwiftyJSON" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/Alamofire-SwiftyJSON.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Alamofire-SwiftyJSON.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Alamofire-SwiftyJSON.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Alamofire-SwiftyJSON.xcodeproj/xcshareddata/xcschemes/Alamofire-SwiftyJSON watchOS.xcscheme b/Alamofire-SwiftyJSON.xcodeproj/xcshareddata/xcschemes/Alamofire-SwiftyJSON watchOS.xcscheme new file mode 100644 index 0000000..321ed7b --- /dev/null +++ b/Alamofire-SwiftyJSON.xcodeproj/xcshareddata/xcschemes/Alamofire-SwiftyJSON watchOS.xcscheme @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AlamofireSwiftyJSON watchOS/Alamofire-SwiftyJSON-Watch.swift b/AlamofireSwiftyJSON watchOS/Alamofire-SwiftyJSON-Watch.swift new file mode 100644 index 0000000..ab0cc16 --- /dev/null +++ b/AlamofireSwiftyJSON watchOS/Alamofire-SwiftyJSON-Watch.swift @@ -0,0 +1,86 @@ +// +// Alamofire-SwiftyJSON-Watch.swift +// Alamofire-SwiftyJSON-Watch +// +// Created by Sam Eckert on 24.04.18. +// Copyright © 2018 SwiftJSON. All rights reserved. +// + +import Foundation +import Alamofire +import SwiftyJSON + +// MARK: - Request for Swift JSON + +extension Request { + /// Returns a SwiftyJSON object contained in a result type constructed from the response data using `JSONSerialization` + /// with the specified reading options. + /// + /// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`. + /// - parameter response: The response from the server. + /// - parameter data: The data returned from the server. + /// - parameter error: The error already encountered if it exists. + /// + /// - returns: The result data type. + public static func serializeResponseSwiftyJSON( + options: JSONSerialization.ReadingOptions, + response: HTTPURLResponse?, + data: Data?, + error: Error?) + -> Result + { + guard error == nil else { return .failure(error!) } + + if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(JSON.null) } + + guard let validData = data, validData.count > 0 else { + return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)) + } + + do { + let json = try JSONSerialization.jsonObject(with: validData, options: options) + return .success(JSON(json)) + } catch { + return .failure(AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error))) + } + } +} + +extension DataRequest { + /// Creates a response serializer that returns a SwiftyJSON object result type constructed from the response data using + /// `JSONSerialization` with the specified reading options. + /// + /// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`. + /// + /// - returns: A JSON object response serializer. + public static func swiftyJSONResponseSerializer( + options: JSONSerialization.ReadingOptions = .allowFragments) + -> DataResponseSerializer + { + return DataResponseSerializer { _, response, data, error in + return Request.serializeResponseSwiftyJSON(options: options, response: response, data: data, error: error) + } + } + + /// Adds a handler to be called once the request has finished. + /// + /// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`. + /// - parameter completionHandler: A closure to be executed once the request has finished. + /// + /// - returns: The request. + @discardableResult + public func responseSwiftyJSON( + queue: DispatchQueue? = nil, + options: JSONSerialization.ReadingOptions = .allowFragments, + completionHandler: @escaping (DataResponse) -> Void) + -> Self + { + return response( + queue: queue, + responseSerializer: DataRequest.swiftyJSONResponseSerializer(options: options), + completionHandler: completionHandler + ) + } +} + +private let emptyDataStatusCodes: Set = [204, 205] diff --git a/AlamofireSwiftyJSON watchOS/AlamofireSwiftyJSON_watchOS.h b/AlamofireSwiftyJSON watchOS/AlamofireSwiftyJSON_watchOS.h new file mode 100644 index 0000000..7e02e0f --- /dev/null +++ b/AlamofireSwiftyJSON watchOS/AlamofireSwiftyJSON_watchOS.h @@ -0,0 +1,19 @@ +// +// AlamofireSwiftyJSON_watchOS.h +// AlamofireSwiftyJSON watchOS +// +// Created by Sam Eckert on 24.04.18. +// Copyright © 2018 SwiftJSON. All rights reserved. +// + +#import + +//! Project version number for AlamofireSwiftyJSON_watchOS. +FOUNDATION_EXPORT double AlamofireSwiftyJSON_watchOSVersionNumber; + +//! Project version string for AlamofireSwiftyJSON_watchOS. +FOUNDATION_EXPORT const unsigned char AlamofireSwiftyJSON_watchOSVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + + diff --git a/AlamofireSwiftyJSON watchOS/Info.plist b/AlamofireSwiftyJSON watchOS/Info.plist new file mode 100644 index 0000000..1007fd9 --- /dev/null +++ b/AlamofireSwiftyJSON watchOS/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + NSPrincipalClass + + +