From fb0d245878edc18c2cabcf527095c13cf96eee47 Mon Sep 17 00:00:00 2001
From: Nikita Ermolenko
Date: Tue, 3 Dec 2019 23:51:17 +0300
Subject: [PATCH] Update the project according to Alamofire 5
---
.travis.yml | 28 ++++----
Cartfile | 2 +-
Cartfile.resolved | 2 +-
Carthage/Checkouts/Alamofire | 2 +-
CodableAlamofire.podspec | 12 ++--
CodableAlamofire.xcodeproj/project.pbxproj | 26 ++++---
Package.resolved | 4 +-
Package.swift | 10 +--
README.md | 8 +--
.../DataKeyPathSerializer.swift | 49 +++++++++++++
.../DataRequest+Decodable.swift | 70 +++----------------
Tests/CodableAlamofireTests/MainTests.swift | 34 ++++-----
12 files changed, 128 insertions(+), 119 deletions(-)
create mode 100644 Sources/CodableAlamofire/DataKeyPathSerializer.swift
diff --git a/.travis.yml b/.travis.yml
index 325d548..b1fd359 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,42 +1,44 @@
-osx_image: xcode11
+osx_image: xcode10.2
xcode_project: CodableAlamofire.xcodeproj
matrix:
include:
- - os: linux
- dist: trusty
- sudo: required
- language: generic
+ - osx_image:
+ os: linux
+ env: TEST=Unix swift=5.0
- xcode_scheme: CodableAlamofire-macOS
language: objective-c
env:
- - XCODE_SDK=macosx10.13
+ - XCODE_SDK=macosx10.14
- XCODE_ACTION="test"
- XCODE_DESTINATION="arch=x86_64"
- xcode_scheme: CodableAlamofire-iOS
language: objective-c
env:
- - XCODE_SDK=iphonesimulator11.0
+ - XCODE_SDK=iphonesimulator12.2
- XCODE_ACTION="test"
- - XCODE_DESTINATION="platform=iOS Simulator,OS=11.0,name=iPhone 6"
+ - XCODE_DESTINATION="platform=iOS Simulator,OS=12.2,name=iPhone 6"
- xcode_scheme: CodableAlamofire-tvOS
language: objective-c
env:
- - XCODE_SDK=appletvsimulator11.0
+ - XCODE_SDK=appletvsimulator12.2
- XCODE_ACTION="test"
- - XCODE_DESTINATION="platform=tvOS Simulator,OS=10.2,name=Apple TV 1080p"
+ - XCODE_DESTINATION="platform=tvOS Simulator,OS=12.2,name=Apple TV"
- xcode_scheme: CodableAlamofire-watchOS
language: objective-c
env:
- - XCODE_SDK=watchsimulator4.0
+ - XCODE_SDK=watchsimulator5.2
- XCODE_ACTION="build"
- XCODE_DESTINATION="platform=watchOS Simulator,name=Apple Watch - 38mm"
install:
- - if [[ `uname` == "Linux" ]] ; then
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)";
+ - if [[ `uname` == "Linux" ]] ; then
+ curl "https://swift.org/builds/swift-${swift}-release/ubuntu1604/swift-${swift}-RELEASE/swift-${swift}-RELEASE-ubuntu16.04.tar.gz" > /tmp/swift.tar.gz;
+ tar -xzf /tmp/swift.tar.gz -C /tmp;
+ export PATH="${PATH}:/tmp/swift-${swift}-RELEASE-ubuntu16.04/usr/bin";
+ swift -version;
fi
script:
- if [[ `uname` == "Linux" ]] ; then
diff --git a/Cartfile b/Cartfile
index 8c5b013..9bdc202 100644
--- a/Cartfile
+++ b/Cartfile
@@ -1 +1 @@
-github "Alamofire/Alamofire"
\ No newline at end of file
+github "Alamofire/Alamofire" "5.0.0-rc.3"
\ No newline at end of file
diff --git a/Cartfile.resolved b/Cartfile.resolved
index f77e86a..1503d6d 100644
--- a/Cartfile.resolved
+++ b/Cartfile.resolved
@@ -1 +1 @@
-github "Alamofire/Alamofire" "4.8.1"
+github "Alamofire/Alamofire" "5.0.0-rc.3"
diff --git a/Carthage/Checkouts/Alamofire b/Carthage/Checkouts/Alamofire
index d82c794..2cbf599 160000
--- a/Carthage/Checkouts/Alamofire
+++ b/Carthage/Checkouts/Alamofire
@@ -1 +1 @@
-Subproject commit d82c7943d80785834da4693b92133012626c060b
+Subproject commit 2cbf59935fbb1f26e352ce4db53f1cf9408d5313
diff --git a/CodableAlamofire.podspec b/CodableAlamofire.podspec
index 008bcb7..bc60477 100644
--- a/CodableAlamofire.podspec
+++ b/CodableAlamofire.podspec
@@ -9,23 +9,23 @@
Pod::Spec.new do |s|
s.name = "CodableAlamofire"
- s.version = "1.1.2"
+ s.version = "1.2.0"
s.summary = "An extension for Alamofire that converts JSON data into Decodable Objects."
s.homepage = "https://github.com/Otbivnoe/CodableAlamofire"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Nikita Ermolenko" => "gnod94@gmail.com" }
s.social_media_url = "https://twitter.com/iOtbivnoe"
- s.ios.deployment_target = '8.0'
- s.osx.deployment_target = '10.10'
- s.watchos.deployment_target = '2.0'
- s.tvos.deployment_target = '9.0'
+ s.ios.deployment_target = '10.0'
+ s.osx.deployment_target = '10.12'
+ s.watchos.deployment_target = '3.0'
+ s.tvos.deployment_target = '10.0'
s.source = { :git => "https://github.com/Otbivnoe/CodableAlamofire.git", :tag => "#{s.version}" }
s.source_files = "Sources/**/*.{h,swift}"
s.swift_versions = ['4.0', '5.0', '5.1']
s.requires_arc = true
- s.dependency 'Alamofire', '~> 4.0'
+ s.dependency 'Alamofire', '~> 5.0.0-rc.3'
end
diff --git a/CodableAlamofire.xcodeproj/project.pbxproj b/CodableAlamofire.xcodeproj/project.pbxproj
index 6851b28..8d8ee00 100644
--- a/CodableAlamofire.xcodeproj/project.pbxproj
+++ b/CodableAlamofire.xcodeproj/project.pbxproj
@@ -33,6 +33,10 @@
84A8C8511EED2F2700AB31BE /* CodableAlamofire.h in Headers */ = {isa = PBXBuildFile; fileRef = 84A8C84D1EED2F0C00AB31BE /* CodableAlamofire.h */; settings = {ATTRIBUTES = (Public, ); }; };
84A8C8521EED2F2800AB31BE /* CodableAlamofire.h in Headers */ = {isa = PBXBuildFile; fileRef = 84A8C84D1EED2F0C00AB31BE /* CodableAlamofire.h */; settings = {ATTRIBUTES = (Public, ); }; };
84A8C8531EED2F2800AB31BE /* CodableAlamofire.h in Headers */ = {isa = PBXBuildFile; fileRef = 84A8C84D1EED2F0C00AB31BE /* CodableAlamofire.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 84B504872396FFC20003140A /* DataKeyPathSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B504862396FFC20003140A /* DataKeyPathSerializer.swift */; };
+ 84B504882396FFC20003140A /* DataKeyPathSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B504862396FFC20003140A /* DataKeyPathSerializer.swift */; };
+ 84B504892396FFC20003140A /* DataKeyPathSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B504862396FFC20003140A /* DataKeyPathSerializer.swift */; };
+ 84B5048A2396FFC20003140A /* DataKeyPathSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B504862396FFC20003140A /* DataKeyPathSerializer.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -133,6 +137,7 @@
84A8C8431EED2ED500AB31BE /* CodableAlamofire.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CodableAlamofire.framework; sourceTree = BUILT_PRODUCTS_DIR; };
84A8C84D1EED2F0C00AB31BE /* CodableAlamofire.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CodableAlamofire.h; sourceTree = ""; };
84A8C84E1EED2F0C00AB31BE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
+ 84B504862396FFC20003140A /* DataKeyPathSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DataKeyPathSerializer.swift; path = CodableAlamofire/DataKeyPathSerializer.swift; sourceTree = ""; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -281,6 +286,7 @@
children = (
8447D5C91F77D4AE003B32F3 /* AlamofireDecodableError.swift */,
8447D5C81F77D4AE003B32F3 /* DataRequest+Decodable.swift */,
+ 84B504862396FFC20003140A /* DataKeyPathSerializer.swift */,
84A8C84C1EED2F0C00AB31BE /* Supporting Files */,
);
path = Sources;
@@ -670,6 +676,7 @@
files = (
846670281F77D7FE00C119BC /* AlamofireDecodableError.swift in Sources */,
846670341F77D80100C119BC /* DataRequest+Decodable.swift in Sources */,
+ 84B504872396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -679,6 +686,7 @@
files = (
846670311F77D7FE00C119BC /* AlamofireDecodableError.swift in Sources */,
846670351F77D80100C119BC /* DataRequest+Decodable.swift in Sources */,
+ 84B504882396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -688,6 +696,7 @@
files = (
846670331F77D7FF00C119BC /* AlamofireDecodableError.swift in Sources */,
846670361F77D80100C119BC /* DataRequest+Decodable.swift in Sources */,
+ 84B504892396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -697,6 +706,7 @@
files = (
846670321F77D7FE00C119BC /* AlamofireDecodableError.swift in Sources */,
846670371F77D80200C119BC /* DataRequest+Decodable.swift in Sources */,
+ 84B5048A2396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1208,7 +1218,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_FILE = "$(SRCROOT)/Sources/Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
- IPHONEOS_DEPLOYMENT_TARGET = 8.0;
+ IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
@@ -1278,7 +1288,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_FILE = "$(SRCROOT)/Sources/Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
- IPHONEOS_DEPLOYMENT_TARGET = 8.0;
+ IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.otbivnoe.CodableAlamofire;
@@ -1363,7 +1373,7 @@
TARGETED_DEVICE_FAMILY = 4;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
- WATCHOS_DEPLOYMENT_TARGET = 2.0;
+ WATCHOS_DEPLOYMENT_TARGET = 3.0;
};
name = Debug;
};
@@ -1427,7 +1437,7 @@
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
- WATCHOS_DEPLOYMENT_TARGET = 2.0;
+ WATCHOS_DEPLOYMENT_TARGET = 3.0;
};
name = Release;
};
@@ -1496,7 +1506,7 @@
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 3;
- TVOS_DEPLOYMENT_TARGET = 9.0;
+ TVOS_DEPLOYMENT_TARGET = 10.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
@@ -1559,7 +1569,7 @@
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 3;
- TVOS_DEPLOYMENT_TARGET = 9.0;
+ TVOS_DEPLOYMENT_TARGET = 10.0;
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
@@ -1623,7 +1633,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Sources/Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
- MACOSX_DEPLOYMENT_TARGET = 10.10;
+ MACOSX_DEPLOYMENT_TARGET = 10.12;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.otbivnoe.CodableAlamofire;
@@ -1689,7 +1699,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Sources/Supporting Files/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
- MACOSX_DEPLOYMENT_TARGET = 10.10;
+ MACOSX_DEPLOYMENT_TARGET = 10.12;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.otbivnoe.CodableAlamofire;
PRODUCT_NAME = CodableAlamofire;
diff --git a/Package.resolved b/Package.resolved
index b4aaad4..2126465 100644
--- a/Package.resolved
+++ b/Package.resolved
@@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/Alamofire/Alamofire",
"state": {
"branch": null,
- "revision": "ce5be6fbc6f51414c49f56fc8e2b7c99253d9f8e",
- "version": "4.9.0"
+ "revision": "2cbf59935fbb1f26e352ce4db53f1cf9408d5313",
+ "version": "5.0.0-rc.3"
}
}
]
diff --git a/Package.swift b/Package.swift
index b537913..fa8f79c 100644
--- a/Package.swift
+++ b/Package.swift
@@ -5,16 +5,16 @@ import PackageDescription
let package = Package(
name: "CodableAlamofire",
platforms: [
- .iOS(.v8),
- .tvOS(.v9),
- .watchOS(.v2),
- .macOS(.v10_10)
+ .iOS(.v10),
+ .tvOS(.v10),
+ .watchOS(.v3),
+ .macOS(.v10_12)
],
products: [
.library(name: "CodableAlamofire", targets: ["CodableAlamofire"]),
],
dependencies: [
- .package(url: "https://github.com/Alamofire/Alamofire", from: "4.5.0")
+ .package(url: "https://github.com/Alamofire/Alamofire", from: "5.0.0-rc.3")
],
targets: [
.target(
diff --git a/README.md b/README.md
index 5d567d0..b94b9ed 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/Otbivnoe/CodableAlamofire.svg?branch=master)](https://travis-ci.org/Otbivnoe/CodableAlamofire)
-![Swift 4.0.x](https://img.shields.io/badge/Swift-4.0-orange.svg)
+![Swift 5.x](https://img.shields.io/badge/Swift-5.x-orange)
[![SPM compatible](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Version](https://img.shields.io/cocoapods/v/CodableAlamofire.svg?style=flat)](http://cocoadocs.org/docsets/CodableAlamofire)
@@ -63,7 +63,7 @@ private struct Repo: Decodable {
There is a similar method to `responseData`, `responseJSON` - **`responseDecodableObject`**:
```swift
-func responseDecodableObject(queue: DispatchQueue? = nil, keyPath: String? = nil, decoder: JSONDecoder = JSONDecoder(), completionHandler: @escaping (DataResponse) -> Void)
+func responseDecodableObject(queue: DispatchQueue? = nil, keyPath: String? = nil, decoder: JSONDecoder = JSONDecoder(), completionHandler: @escaping (AFDataResponse) -> Void)
```
- `queue` - The queue on which the completion handler is dispatched.
@@ -75,8 +75,8 @@ let url = URL(string: "https://raw.githubusercontent.com/otbivnoe/CodableAlamofi
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970 // It is necessary for correct decoding. Timestamp -> Date.
-Alamofire.request(url).responseDecodableObject(keyPath: "result.libraries", decoder: decoder) { (response: DataResponse<[Repo]>) in
- let repo = response.result.value
+AF.request(url).responseDecodableObject(keyPath: "result.libraries", decoder: decoder) { (response: AFDataResponse<[Repo]>) in
+ let repo = response.value
print(repo)
}
```
diff --git a/Sources/CodableAlamofire/DataKeyPathSerializer.swift b/Sources/CodableAlamofire/DataKeyPathSerializer.swift
new file mode 100644
index 0000000..8e3e18f
--- /dev/null
+++ b/Sources/CodableAlamofire/DataKeyPathSerializer.swift
@@ -0,0 +1,49 @@
+//
+// DataKeyPathSerializer.swift
+// CodableAlamofire
+//
+// Created by Nikita Ermolenko on 03/12/2019.
+//
+
+import Foundation
+import Alamofire
+
+internal final class DataKeyPathSerializer: DataResponseSerializerProtocol {
+
+ private let keyPath: String?
+ private let decoder: JSONDecoder
+
+ init(keyPath: String?, decoder: JSONDecoder = JSONDecoder()) {
+ self.keyPath = keyPath
+ self.decoder = decoder
+ }
+
+ func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> SerializedObject {
+ if let error = error {
+ throw error
+ }
+
+ if let keyPath = self.keyPath {
+ if keyPath.isEmpty {
+ throw AFError.responseSerializationFailed(reason: .decodingFailed(error: AlamofireDecodableError.emptyKeyPath))
+ }
+
+ let json = try JSONResponseSerializer().serialize(request: nil, response: response, data: data, error: nil)
+ if let nestedJson = (json as AnyObject).value(forKeyPath: keyPath) {
+ guard JSONSerialization.isValidJSONObject(nestedJson) else {
+ throw AFError.responseSerializationFailed(reason: .decodingFailed(error: AlamofireDecodableError.invalidJSON))
+ }
+ let data = try JSONSerialization.data(withJSONObject: nestedJson)
+ let object = try decoder.decode(SerializedObject.self, from: data)
+ return object
+ }
+ else {
+ throw AFError.responseSerializationFailed(reason: .decodingFailed(error: AlamofireDecodableError.invalidKeyPath))
+ }
+ } else {
+ let data = try DataResponseSerializer().serialize(request: nil, response: response, data: data, error: nil)
+ let object = try self.decoder.decode(SerializedObject.self, from: data)
+ return object
+ }
+ }
+}
diff --git a/Sources/CodableAlamofire/DataRequest+Decodable.swift b/Sources/CodableAlamofire/DataRequest+Decodable.swift
index ead6862..0b5256a 100644
--- a/Sources/CodableAlamofire/DataRequest+Decodable.swift
+++ b/Sources/CodableAlamofire/DataRequest+Decodable.swift
@@ -10,67 +10,10 @@ import Foundation
import Alamofire
extension DataRequest {
-
- private static func DecodableObjectSerializer(_ keyPath: String?, _ decoder: JSONDecoder) -> DataResponseSerializer {
- return DataResponseSerializer { _, response, data, error in
- if let error = error {
- return .failure(error)
- }
- if let keyPath = keyPath {
- if keyPath.isEmpty {
- return .failure(AlamofireDecodableError.emptyKeyPath)
- }
- return DataRequest.decodeToObject(byKeyPath: keyPath, decoder: decoder, response: response, data: data)
- }
- return DataRequest.decodeToObject(decoder: decoder, response: response, data: data)
- }
- }
-
- private static func decodeToObject(decoder: JSONDecoder, response: HTTPURLResponse?, data: Data?) -> Result {
- let result = Request.serializeResponseData(response: response, data: data, error: nil)
-
- switch result {
- case .success(let data):
- do {
- let object = try decoder.decode(T.self, from: data)
- return .success(object)
- }
- catch {
- return .failure(error)
- }
- case .failure(let error): return .failure(error)
- }
- }
-
- private static func decodeToObject(byKeyPath keyPath: String, decoder: JSONDecoder, response: HTTPURLResponse?, data: Data?) -> Result {
- let result = Request.serializeResponseJSON(options: [], response: response, data: data, error: nil)
-
- switch result {
- case .success(let json):
- if let nestedJson = (json as AnyObject).value(forKeyPath: keyPath) {
- do {
- guard JSONSerialization.isValidJSONObject(nestedJson) else {
- return .failure(AlamofireDecodableError.invalidJSON)
- }
- let data = try JSONSerialization.data(withJSONObject: nestedJson)
- let object = try decoder.decode(T.self, from: data)
- return .success(object)
- }
- catch {
- return .failure(error)
- }
- }
- else {
- return .failure(AlamofireDecodableError.invalidKeyPath)
- }
- case .failure(let error): return .failure(error)
- }
- }
-
-
+
/// Adds a handler to be called once the request has finished.
- /// - parameter queue: The queue on which the completion handler is dispatched.
+ /// - parameter queue: The queue on which the completion handler is dispatched. Default: `.main`.
/// - parameter keyPath: The keyPath where object decoding should be performed. Default: `nil`.
/// - parameter decoder: The decoder that performs the decoding of JSON into semantic `Decodable` type. Default: `JSONDecoder()`.
/// - parameter completionHandler: The code to be executed once the request has finished and the data has been mapped by `JSONDecoder`.
@@ -78,7 +21,12 @@ extension DataRequest {
/// - returns: The request.
@discardableResult
- public func responseDecodableObject(queue: DispatchQueue? = nil, keyPath: String? = nil, decoder: JSONDecoder = JSONDecoder(), completionHandler: @escaping (DataResponse) -> Void) -> Self {
- return response(queue: queue, responseSerializer: DataRequest.DecodableObjectSerializer(keyPath, decoder), completionHandler: completionHandler)
+ public func responseDecodableObject(queue: DispatchQueue = .main,
+ keyPath: String? = nil,
+ decoder: JSONDecoder = JSONDecoder(),
+ completionHandler: @escaping (AFDataResponse) -> Void) -> Self {
+ return response(queue: queue,
+ responseSerializer: DataKeyPathSerializer(keyPath: keyPath, decoder: decoder),
+ completionHandler: completionHandler)
}
}
diff --git a/Tests/CodableAlamofireTests/MainTests.swift b/Tests/CodableAlamofireTests/MainTests.swift
index 4566bce..4f48d19 100644
--- a/Tests/CodableAlamofireTests/MainTests.swift
+++ b/Tests/CodableAlamofireTests/MainTests.swift
@@ -45,9 +45,9 @@ final class MainTests: XCTestCase {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
-
- Alamofire.request(url).responseDecodableObject(decoder: decoder) { (response: DataResponse) in
- let repo = response.result.value
+
+ AF.request(url).responseDecodableObject(decoder: decoder) { (response: AFDataResponse) in
+ let repo = response.value
XCTAssertNotNil(repo, "Result should not be nil")
XCTAssertEqual(repo?.name, "Alamofire")
@@ -70,8 +70,8 @@ final class MainTests: XCTestCase {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
- Alamofire.request(url).responseDecodableObject(decoder: decoder) { (response: DataResponse<[Repo]>) in
- let repos = response.result.value
+ AF.request(url).responseDecodableObject(decoder: decoder) { (response: AFDataResponse<[Repo]>) in
+ let repos = response.value
XCTAssertNotNil(repos, "Result should not be nil")
@@ -100,8 +100,8 @@ final class MainTests: XCTestCase {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
- Alamofire.request(url).responseDecodableObject(keyPath: "result.libraries", decoder: decoder) { (response: DataResponse<[Repo]>) in
- let repos = response.result.value
+ AF.request(url).responseDecodableObject(keyPath: "result.libraries", decoder: decoder) { (response: AFDataResponse<[Repo]>) in
+ let repos = response.value
XCTAssertNotNil(repos, "Result should not be nil")
@@ -127,9 +127,9 @@ final class MainTests: XCTestCase {
let url = URL(string: "https://raw.githubusercontent.com/otbivnoe/CodableAlamofire/master/Tests/Mock/keypathArray.json")!
let expectation = self.expectation(description: "Reponse from \(url.absoluteString)")
- Alamofire.request(url).responseDecodableObject(keyPath: "") { (response: DataResponse<[Repo]>) in
+ AF.request(url).responseDecodableObject(keyPath: "") { (response: AFDataResponse<[Repo]>) in
XCTAssertNotNil(response.error)
- if case AlamofireDecodableError.emptyKeyPath = response.error! {
+ if case AFError.responseSerializationFailed(reason: .decodingFailed(error: AlamofireDecodableError.emptyKeyPath)) = response.error! {
XCTAssertTrue(true)
}
else {
@@ -147,9 +147,9 @@ final class MainTests: XCTestCase {
let url = URL(string: "https://raw.githubusercontent.com/otbivnoe/CodableAlamofire/master/Tests/Mock/keypathArray.json")!
let expectation = self.expectation(description: "Reponse from \(url.absoluteString)")
- Alamofire.request(url).responseDecodableObject(keyPath: "keypath") { (response: DataResponse<[Repo]>) in
+ AF.request(url).responseDecodableObject(keyPath: "keypath") { (response: AFDataResponse<[Repo]>) in
XCTAssertNotNil(response.error)
- if case AlamofireDecodableError.invalidKeyPath = response.error! {
+ if case AFError.responseSerializationFailed(reason: .decodingFailed(error: AlamofireDecodableError.invalidKeyPath)) = response.error! {
XCTAssertTrue(true)
}
else {
@@ -167,9 +167,9 @@ final class MainTests: XCTestCase {
let url = URL(string: "https://raw.githubusercontent.com/otbivnoe/CodableAlamofire/master/Tests/Mock/object.json")!
let expectation = self.expectation(description: "Reponse from \(url.absoluteString)")
- Alamofire.request(url).responseDecodableObject { (response: DataResponse) in
+ AF.request(url).responseDecodableObject { (response: AFDataResponse) in
XCTAssertNotNil(response.error)
- if case DecodingError.keyNotFound = response.error! {
+ if case AFError.responseSerializationFailed(reason: .customSerializationFailed(error: DecodingError.keyNotFound)) = response.error! {
XCTAssertTrue(true)
}
else {
@@ -187,9 +187,9 @@ final class MainTests: XCTestCase {
let url = URL(string: "https://raw.githubusercontent.com/otbivnoe/CodableAlamofire/master/Tests/Mock/keypathObject.json")!
let expectation = self.expectation(description: "Reponse from \(url.absoluteString)")
- Alamofire.request(url).responseDecodableObject(keyPath: "result.library") { (response: DataResponse) in
+ AF.request(url).responseDecodableObject(keyPath: "result.library") { (response: AFDataResponse) in
XCTAssertNotNil(response.error)
- if case DecodingError.keyNotFound = response.error! {
+ if case AFError.responseSerializationFailed(reason: .customSerializationFailed(error: DecodingError.keyNotFound)) = response.error! {
XCTAssertTrue(true)
}
else {
@@ -207,9 +207,9 @@ final class MainTests: XCTestCase {
let url = URL(string: "https://raw.githubusercontent.com/Otbivnoe/CodableAlamofire/master/Tests/Mock/emptyKeypath.json")!
let expectation = self.expectation(description: "Reponse from \(url.absoluteString)")
- Alamofire.request(url).responseDecodableObject(keyPath: "data") { (response: DataResponse) in
+ AF.request(url).responseDecodableObject(keyPath: "data") { (response: AFDataResponse) in
XCTAssertNotNil(response.error)
- if case AlamofireDecodableError.invalidJSON = response.error! {
+ if case AFError.responseSerializationFailed(reason: .decodingFailed(error: AlamofireDecodableError.invalidJSON)) = response.error! {
XCTAssertTrue(true)
}
else {