Skip to content
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
87 changes: 87 additions & 0 deletions Alamofire-SwiftyJSON watchOS/Alamofire-SwiftyJSON-watchOS.swift
Original file line number Diff line number Diff line change
@@ -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<JSON>
{
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<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.
///
/// - returns: The request.
@discardableResult
public func responseSwiftyJSON(
queue: DispatchQueue? = nil,
options: JSONSerialization.ReadingOptions = .allowFragments,
completionHandler: @escaping (DataResponse<JSON>) -> Void)
-> Self
{
return response(
queue: queue,
responseSerializer: DataRequest.swiftyJSONResponseSerializer(options: options),
completionHandler: completionHandler
)
}
}

private let emptyDataStatusCodes: Set<Int> = [204, 205]
19 changes: 19 additions & 0 deletions Alamofire-SwiftyJSON watchOS/Alamofire_SwiftyJSON_watchOS.h
Original file line number Diff line number Diff line change
@@ -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 <WatchKit/WatchKit.h>

//! 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 <Alamofire_SwiftyJSON_watchOS/PublicHeader.h>


22 changes: 22 additions & 0 deletions Alamofire-SwiftyJSON watchOS/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
</plist>
Loading