From 41bc7ebbd85d4f5c3001a104d7b5470a849ac941 Mon Sep 17 00:00:00 2001 From: phimage Date: Fri, 16 Oct 2015 14:09:41 +0200 Subject: [PATCH] Add also methods with URLRequest(Convertible) for more complex request Factorize code by defining an enum for file serialisation format --- .../MutablePreferencesType+Alamofire.swift | 104 ++++++++++++------ README.md | 16 ++- 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/Alamofire-Prephirences/MutablePreferencesType+Alamofire.swift b/Alamofire-Prephirences/MutablePreferencesType+Alamofire.swift index 01bb3a4..6335cdd 100644 --- a/Alamofire-Prephirences/MutablePreferencesType+Alamofire.swift +++ b/Alamofire-Prephirences/MutablePreferencesType+Alamofire.swift @@ -1,49 +1,90 @@ // // Prephirences+Alamofire.swift // Alamofire-Prephirences -// -// Created by phimage on 15/10/15. -// Copyright © 2015 phimage. All rights reserved. -// +/* +The MIT License (MIT) + +Copyright (c) 2015 Eric Marchand (phimage) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + import Foundation import Prephirences import Alamofire +enum FileSerializationFormat { + case PropertyList(NSPropertyListReadOptions), JSON(NSJSONReadingOptions) + + var errorCode: Error.Code { + switch self { + case PropertyList: return Error.Code.PropertyListSerializationFailed + case JSON: return Error.Code.JSONSerializationFailed + } + } + + var responseSerializer: ResponseSerializer { + switch self { + case PropertyList(let options): return Request.propertyListResponseSerializer(options: options) + case JSON(let options): return Request.JSONResponseSerializer(options: options) + } + } +} + public extension MutablePreferencesType { public func loadPropertyListFromURL(url: URLStringConvertible, completionHandler: (Response -> Void)? = nil) -> Request { - return Alamofire.request(.GET, url) - .validate() - .responsePropertyList { response in - switch response.result { - case .Success(let plist): - guard let dico = plist as? [String : AnyObject] else { - let error = Error.errorWithCode(.PropertyListSerializationFailed, failureReason: "Unable to convert plist to dictionnary") - let failureResponse = Response(request: response.request, - response: response.response, - data: response.data, - result: .Failure(error)) - - completionHandler?(failureResponse) - break - } - self.setObjectsForKeysWithDictionary(dico) - completionHandler?(response) - case .Failure: - completionHandler?(response) - } - } + return loadFromURL(url, format: .PropertyList(NSPropertyListReadOptions()), completionHandler: completionHandler) } public func loadJSONFromURL(url: URLStringConvertible, completionHandler: (Response -> Void)? = nil) -> Request { - return Alamofire.request(.GET, url) - .validate() - .responseJSON{ response in + return loadFromURL(url, format: .JSON(.AllowFragments), completionHandler: completionHandler) + } + + public func loadPropertyListFromURLRequest(URLRequest: URLRequestConvertible, completionHandler: (Response -> Void)? = nil) -> Request { + return loadFromURLRequest(URLRequest, format: .PropertyList(NSPropertyListReadOptions()), completionHandler: completionHandler) + } + + public func loadJSONFromURLRequest(URLRequest: URLRequestConvertible, completionHandler: (Response -> Void)? = nil) -> Request { + return loadFromURLRequest(URLRequest, format: .JSON(.AllowFragments), completionHandler: completionHandler) + } + + // MARK: private load + private func loadFromURL(url: URLStringConvertible, format: FileSerializationFormat, completionHandler: (Response -> Void)? = nil) -> Request { + return responseToRequest(Alamofire.request(.GET, url), format: format, completionHandler: completionHandler) + } + + private func loadFromURLRequest(URLRequest: URLRequestConvertible, format: FileSerializationFormat, completionHandler: (Response -> Void)? = nil) -> Request { + return responseToRequest(Alamofire.request(URLRequest), format: format, completionHandler: completionHandler) + } + + // MARK: response + private func responseToRequest(request: Request, format: FileSerializationFormat, completionHandler: (Response -> Void)? = nil) -> Request { + request.validate() // XXX could add here accepted content type + // XXX if no format defined, could get returned content-type from responseContentType = response.MIMEType, responseMIMEType = MIMEType(responseContentType) + + return request.response(responseSerializer: format.responseSerializer){ response in switch response.result { - case .Success(let plist): - guard let dico = plist as? [String : AnyObject] else { - let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: "Unable to convert JSON to dictionnary") + case .Success(let object): + guard let dico = object as? [String : AnyObject] else { + let error = Error.errorWithCode(format.errorCode, failureReason: "Unable to convert to dictionnary") let failureResponse = Response(request: response.request, response: response.response, data: response.data, @@ -58,6 +99,7 @@ public extension MutablePreferencesType { completionHandler?(response) } } + } } \ No newline at end of file diff --git a/README.md b/README.md index b30bee9..61cce73 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ By using remote preferences you can remotely control the behavior of your app, a It's built on top of [Alamofire](https://github.com/Alamofire/Alamofire) and [Prephirences](https://github.com/phimage/Prephirences), and provides methods to load from remote `plist` or `json` files. # Usage # - +### Load with URL(Convertible) On your `NSUserDefault` or any `MutablePreferencesType` ```swift @@ -37,6 +37,20 @@ And for JSON just change `loadPropertyListFromURL` by `loadJSONFromURL` mutablePref.loadJSONFromURL("http://example.com/pref.json") ``` +### Load with URLRequest +For more complex request, instead of use simple URL you can create your own `NSURLRequest` + +```swift +let url = NSURL(string: "http://example.com/pref.plist")! +let mutableURLRequest = NSMutableURLRequest(URL: url) +mutableURLRequest.HTTPMethod = "GET" +... (add HTTPHeader, etc...) + +mutablePref.loadPropertyListFromURLRequest(mutableURLRequest) +``` + + + # Setup # ## Using [cocoapods](http://cocoapods.org/) ##