Skip to content

Commit

Permalink
Add also methods with URLRequest(Convertible) for more complex request
Browse files Browse the repository at this point in the history
Factorize code by defining an enum for file serialisation format
  • Loading branch information
phimage committed Oct 16, 2015
1 parent a631b22 commit 41bc7eb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 32 deletions.
104 changes: 73 additions & 31 deletions Alamofire-Prephirences/MutablePreferencesType+Alamofire.swift
Original file line number Diff line number Diff line change
@@ -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<AnyObject, NSError> {
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<AnyObject, NSError> -> 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<AnyObject, NSError>(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<AnyObject, NSError> -> 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<AnyObject, NSError> -> Void)? = nil) -> Request {
return loadFromURLRequest(URLRequest, format: .PropertyList(NSPropertyListReadOptions()), completionHandler: completionHandler)
}

public func loadJSONFromURLRequest(URLRequest: URLRequestConvertible, completionHandler: (Response<AnyObject, NSError> -> Void)? = nil) -> Request {
return loadFromURLRequest(URLRequest, format: .JSON(.AllowFragments), completionHandler: completionHandler)
}

// MARK: private load
private func loadFromURL(url: URLStringConvertible, format: FileSerializationFormat, completionHandler: (Response<AnyObject, NSError> -> Void)? = nil) -> Request {
return responseToRequest(Alamofire.request(.GET, url), format: format, completionHandler: completionHandler)
}

private func loadFromURLRequest(URLRequest: URLRequestConvertible, format: FileSerializationFormat, completionHandler: (Response<AnyObject, NSError> -> Void)? = nil) -> Request {
return responseToRequest(Alamofire.request(URLRequest), format: format, completionHandler: completionHandler)
}

// MARK: response
private func responseToRequest(request: Request, format: FileSerializationFormat, completionHandler: (Response<AnyObject, NSError> -> 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<AnyObject, NSError>(request: response.request,
response: response.response,
data: response.data,
Expand All @@ -58,6 +99,7 @@ public extension MutablePreferencesType {
completionHandler?(response)
}
}

}

}
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/) ##
Expand Down

0 comments on commit 41bc7eb

Please sign in to comment.