Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update dependencies to Alamofire 5.0.0 & SwiftyJSON #80

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: pod install
- name: Run iOS tests
run: xcodebuild test -workspace Alamofire-SwiftyJSON.xcworkspace -scheme AlamofireSwiftyJSON -destination platform="iOS Simulator,name=iPhone 11 Pro"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ DerivedData
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Pods/
Pods/
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
[submodule "SwiftyJSON"]
path = SwiftyJSON
url = https://github.com/SwiftyJSON/SwiftyJSON.git
[submodule "Alamofire"]
path = Alamofire
url = https://github.com/Alamofire/Alamofire.git
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
5.2
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion Alamofire
Submodule Alamofire deleted from 7fe7b9
8 changes: 4 additions & 4 deletions Alamofire-SwiftyJSON.podspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Pod::Spec.new do |s|
s.name = "Alamofire-SwiftyJSON"
s.version = "3.0.0"
s.version = "5.0.0"
s.summary = "Alamofire extension for serialize NSData to SwiftyJSON "
s.homepage = "https://github.com/SwiftyJSON/Alamofire-SwiftyJSON"
s.license = { :type => "MIT" }
s.authors = { "tangplin" => "[email protected]" }

s.requires_arc = true
s.osx.deployment_target = "10.11"
s.ios.deployment_target = "9.0"
s.ios.deployment_target = "15.0"
s.source = { :git => "https://github.com/SwiftyJSON/Alamofire-SwiftyJSON.git", :tag => s.version }
s.source_files = "Source/*.swift"
s.dependency 'Alamofire', '~> 4.5'
s.dependency 'SwiftyJSON', '~> 4.0.0'
s.dependency 'Alamofire', '~> 5'
s.dependency 'SwiftyJSON', '~> 5'
end
418 changes: 115 additions & 303 deletions Alamofire-SwiftyJSON.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "1100"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -40,8 +40,16 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A8AFB70E19D1401D0070765E"
BuildableName = "AlamofireSwiftyJSON.framework"
BlueprintName = "AlamofireSwiftyJSON"
ReferencedContainer = "container:Alamofire-SwiftyJSON.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -54,23 +62,11 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A8AFB70E19D1401D0070765E"
BuildableName = "AlamofireSwiftyJSON.framework"
BlueprintName = "AlamofireSwiftyJSON"
ReferencedContainer = "container:Alamofire-SwiftyJSON.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand All @@ -86,8 +82,6 @@
ReferencedContainer = "container:Alamofire-SwiftyJSON.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
14 changes: 14 additions & 0 deletions Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
platform :ios, '10'

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

target 'AlamofireSwiftyJSON' do
pod 'Alamofire', '~> 5'
pod 'SwiftyJSON', '~> 5'
end

target 'AlamofireSwiftyJSONTests' do
pod 'Alamofire', '~> 5'
pod 'SwiftyJSON', '~> 5'
end
20 changes: 20 additions & 0 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
PODS:
- Alamofire (5.2.2)
- SwiftyJSON (5.0.0)

DEPENDENCIES:
- Alamofire (~> 5)
- SwiftyJSON (~> 5)

SPEC REPOS:
https://github.com/CocoaPods/Specs.git:
- Alamofire
- SwiftyJSON

SPEC CHECKSUMS:
Alamofire: 814429acc853c6c54ff123fc3d2ef66803823ce0
SwiftyJSON: 36413e04c44ee145039d332b4f4e2d3e8d6c4db7

PODFILE CHECKSUM: 5175165036843bb0979dcfa0e46433202caf367a

COCOAPODS: 1.10.0.beta.2
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ An extension to make serializing [Alamofire](https://github.com/Alamofire/Alamof

⚠️ **To use with Swift 4.x please ensure you are using >= `3.0.0`** ⚠️

⚠️ **To use with Swift 5.x please ensure you are using >= `5.0.0`** ⚠️

## Swift version

Alamofire-SwiftyJSON | Swift version | Alamofire | SwiftyJSON
------------- | --------------| ----------| ----------
2.x | Swift 3.x | 4.x | 3.x
3.x | Swift 4.x | 4.5.x | 4.x
5.x | Swift 5.x | 5.x | 5.x

## Requirements

Expand Down Expand Up @@ -39,7 +42,7 @@ import Alamofire_SwiftyJSON
```

```swift
Alamofire.request(URL, method: .get, parameters: parameters, encoding: URLEncoding.default)
AF.request(URL, method: .get, parameters: parameters, encoding: URLEncoding.default)
.responseSwiftyJSON { dataResponse in
print(dataResponse.request)
print(dataResponse.response)
Expand Down
109 changes: 46 additions & 63 deletions Source/Alamofire-SwiftyJSON.swift
Original file line number Diff line number Diff line change
@@ -1,87 +1,70 @@
//
// AlamofireSwiftyJSON.swift
// AlamofireSwiftyJSON
//
// Created by Pinglin Tang on 14-9-22.
// Copyright (c) 2014 SwiftyJSON. All rights reserved.
//

import Foundation

import Alamofire
import Foundation
import SwiftyJSON

// MARK: - Request for Swift JSON
public final class SwiftyJSONResponseSerializer: ResponseSerializer {
public let dataPreprocessor: DataPreprocessor
public let emptyResponseCodes: Set<Int>
public let emptyRequestMethods: Set<HTTPMethod>
/// `JSONSerialization.ReadingOptions` used when serializing a response.
public let options: JSONSerialization.ReadingOptions

extension Request {
/// Returns a SwiftyJSON object contained in a result type constructed from the response data using `JSONSerialization`
/// with the specified reading options.
/// Creates an instance with the provided values.
///
/// - 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<JSON>
/// - Parameters:
/// - dataPreprocessor: `DataPreprocessor` used to prepare the received `Data` for serialization.
/// - emptyResponseCodes: The HTTP response codes for which empty responses are allowed. `[204, 205]` by default.
/// - emptyRequestMethods: The HTTP request methods for which empty responses are allowed. `[.head]` by default.
/// - options: The options to use. `.allowFragments` by default.
public init(dataPreprocessor: DataPreprocessor = SwiftyJSONResponseSerializer.defaultDataPreprocessor,
emptyResponseCodes: Set<Int> = SwiftyJSONResponseSerializer.defaultEmptyResponseCodes,
emptyRequestMethods: Set<HTTPMethod> = SwiftyJSONResponseSerializer.defaultEmptyRequestMethods,
options: JSONSerialization.ReadingOptions = .allowFragments)
{
guard error == nil else { return .failure(error!) }
self.dataPreprocessor = dataPreprocessor
self.emptyResponseCodes = emptyResponseCodes
self.emptyRequestMethods = emptyRequestMethods
self.options = options
}

if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(JSON.null) }
public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> JSON {
guard error == nil else { throw error! }

guard let validData = data, validData.count > 0 else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength))
guard var data = data, !data.isEmpty else {
guard emptyResponseAllowed(forRequest: request, response: response) else {
throw AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)
}

return JSON(booleanLiteral: false)
}

data = try dataPreprocessor.preprocess(data)

do {
let json = try JSONSerialization.jsonObject(with: validData, options: options)
return .success(JSON(json))
let json = try JSONSerialization.jsonObject(with: data, options: options)
return JSON(json)
} catch {
return .failure(AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error)))
throw 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<JSON>
{
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.
/// - Parameters:
/// - queue: The queue on which the completion handler is dispatched. `.main` by default.
/// - options: The JSON serialization reading options. `.allowFragments` by default.
/// - completionHandler: A closure to be executed once the request has finished.
///
/// - returns: The request.
/// - Returns: The request.
@discardableResult
public func responseSwiftyJSON(
queue: DispatchQueue? = nil,
options: JSONSerialization.ReadingOptions = .allowFragments,
completionHandler: @escaping (DataResponse<JSON>) -> Void)
-> Self
public func responseSwiftyJSON(queue: DispatchQueue = .main,
options: JSONSerialization.ReadingOptions = .allowFragments,
completionHandler: @escaping (AFDataResponse<JSON>) -> Void) -> Self
{
return response(
queue: queue,
responseSerializer: DataRequest.swiftyJSONResponseSerializer(options: options),
completionHandler: completionHandler
)
response(queue: queue,
responseSerializer: SwiftyJSONResponseSerializer(options: options),
completionHandler: completionHandler)
}
}

private let emptyDataStatusCodes: Set<Int> = [204, 205]
1 change: 0 additions & 1 deletion SwiftyJSON
Submodule SwiftyJSON deleted from 6b9f11
9 changes: 4 additions & 5 deletions Tests/Alamofire_SwiftyJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@
// Copyright (c) 2014年 SwiftJSON. All rights reserved.
//

import XCTest
import Alamofire
import SwiftyJSON
import AlamofireSwiftyJSON
import SwiftyJSON
import XCTest

class Alamofire_SwiftyJSONTests: XCTestCase {

func testGETRequestJSONResponse() {
let URL = "http://httpbin.org/get"
let parameters: Parameters = ["foo": "bar"]
let expect = expectation(description: "responseSwiftyJSON method should work")
Alamofire.request(URL, method: .get, parameters: parameters, encoding: URLEncoding.default)
AF.request(URL, method: .get, parameters: parameters, encoding: URLEncoding.default)
.validate()
.responseSwiftyJSON { response in
XCTAssertNotNil(response.request, "request should not be nil")
XCTAssertNotNil(response.response, "response should not be nil")
XCTAssertNil(response.error, "result error should be nil")
XCTAssertEqual(response.value?["args"], SwiftyJSON.JSON(["foo": "bar"] as NSDictionary), "args should be equal")
expect.fulfill()
}
}
waitForExpectations(timeout: 10.0, handler: nil)
}
}