Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #14 from Otbivnoe/update-alamofire-v5
Browse files Browse the repository at this point in the history
Update the project according to Alamofire 5
  • Loading branch information
Otbivnoe authored Dec 3, 2019
2 parents 1521aa1 + fb0d245 commit 25fb5ea
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 119 deletions.
28 changes: 15 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "Alamofire/Alamofire"
github "Alamofire/Alamofire" "5.0.0-rc.3"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "Alamofire/Alamofire" "4.8.1"
github "Alamofire/Alamofire" "5.0.0-rc.3"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/Alamofire
Submodule Alamofire updated 373 files
12 changes: 6 additions & 6 deletions CodableAlamofire.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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" => "[email protected]" }
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
26 changes: 18 additions & 8 deletions CodableAlamofire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 = "<group>"; };
84A8C84E1EED2F0C00AB31BE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
84B504862396FFC20003140A /* DataKeyPathSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DataKeyPathSerializer.swift; path = CodableAlamofire/DataKeyPathSerializer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -281,6 +286,7 @@
children = (
8447D5C91F77D4AE003B32F3 /* AlamofireDecodableError.swift */,
8447D5C81F77D4AE003B32F3 /* DataRequest+Decodable.swift */,
84B504862396FFC20003140A /* DataKeyPathSerializer.swift */,
84A8C84C1EED2F0C00AB31BE /* Supporting Files */,
);
path = Sources;
Expand Down Expand Up @@ -670,6 +676,7 @@
files = (
846670281F77D7FE00C119BC /* AlamofireDecodableError.swift in Sources */,
846670341F77D80100C119BC /* DataRequest+Decodable.swift in Sources */,
84B504872396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -679,6 +686,7 @@
files = (
846670311F77D7FE00C119BC /* AlamofireDecodableError.swift in Sources */,
846670351F77D80100C119BC /* DataRequest+Decodable.swift in Sources */,
84B504882396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -688,6 +696,7 @@
files = (
846670331F77D7FF00C119BC /* AlamofireDecodableError.swift in Sources */,
846670361F77D80100C119BC /* DataRequest+Decodable.swift in Sources */,
84B504892396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -697,6 +706,7 @@
files = (
846670321F77D7FE00C119BC /* AlamofireDecodableError.swift in Sources */,
846670371F77D80200C119BC /* DataRequest+Decodable.swift in Sources */,
84B5048A2396FFC20003140A /* DataKeyPathSerializer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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 = "";
};
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Expand Down
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</p>

[![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)
Expand Down Expand Up @@ -63,7 +63,7 @@ private struct Repo: Decodable {
There is a similar method to `responseData`, `responseJSON` - **`responseDecodableObject`**:

```swift
func responseDecodableObject<T: Decodable>(queue: DispatchQueue? = nil, keyPath: String? = nil, decoder: JSONDecoder = JSONDecoder(), completionHandler: @escaping (DataResponse<T>) -> Void)
func responseDecodableObject<T: Decodable>(queue: DispatchQueue? = nil, keyPath: String? = nil, decoder: JSONDecoder = JSONDecoder(), completionHandler: @escaping (AFDataResponse<T>) -> Void)
```

- `queue` - The queue on which the completion handler is dispatched.
Expand All @@ -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)
}
```
Expand Down
49 changes: 49 additions & 0 deletions Sources/CodableAlamofire/DataKeyPathSerializer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// DataKeyPathSerializer.swift
// CodableAlamofire
//
// Created by Nikita Ermolenko on 03/12/2019.
//

import Foundation
import Alamofire

internal final class DataKeyPathSerializer<SerializedObject: Decodable>: 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
}
}
}
Loading

0 comments on commit 25fb5ea

Please sign in to comment.