Skip to content

Commit

Permalink
Merge pull request #84 from ishkawa/explicit-null-value
Browse files Browse the repository at this point in the history
Improve nullable keys handling, restore [String: AnyObject] for `parameters`
  • Loading branch information
ishkawa committed Oct 15, 2015
2 parents cc32258 + ac3fc03 commit bbac3af
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
24 changes: 9 additions & 15 deletions APIKit/RequestType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public protocol RequestType {
var baseURL: NSURL { get }
var method: HTTPMethod { get }
var path: String { get }
var parameters: [String: AnyObject?] { get }

/// A parameter dictionary for the request. You can pass `NSNull()` as a
/// value for nullable keys, those should be existed in the encoded query or
/// the request body.
var parameters: [String: AnyObject] { get }

/// You can add any configurations here
///
Expand Down Expand Up @@ -45,7 +49,7 @@ public protocol RequestType {

/// Default implementation of RequestType protocol
public extension RequestType {
public var parameters: [String: AnyObject?] {
public var parameters: [String: AnyObject] {
return [:]
}

Expand Down Expand Up @@ -78,16 +82,15 @@ public extension RequestType {
}

let URLRequest = NSMutableURLRequest()
let paramObject = parametersToAnyObject(parameters)


switch method {
case .GET, .HEAD, .DELETE:
if parameters.count > 0 {
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(paramObject)
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)
}
default:
do {
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(paramObject)
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(parameters)
} catch {
return .Failure(.RequestBodySerializationError(error))
}
Expand Down Expand Up @@ -134,12 +137,3 @@ public extension RequestType {
return .Success(response)
}
}

/// Convert `var parameters: [String: AnyObject?]` (non-AnyObject) to AnyObject using NSNull
private func parametersToAnyObject(parameters: [String: AnyObject?]) -> [String: AnyObject] {
var object = [String: AnyObject]()
for (key, value) in parameters {
object[key] = value ?? NSNull()
}
return object
}
2 changes: 1 addition & 1 deletion APIKit/URLEncodedSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public final class URLEncodedSerialization {

public static func stringFromDictionary(dictionary: [String: AnyObject]) -> String {
let pairs = dictionary.map { key, value -> String in
guard (value is NSNull) == false else {
if value is NSNull {
return "\(escape(key))"
}

Expand Down
2 changes: 1 addition & 1 deletion APIKitTests/RequestCreateTaskInURLSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RequestCreateTaskInURLSessionTest: XCTestCase {
var baseURL: NSURL { return NSURL(string: b)! }
var method: HTTPMethod { return m }
var path: String { return p }
var parameters: [String: AnyObject?] { return params }
var parameters: [String: AnyObject] { return params }
func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? { return nil }
}

Expand Down
4 changes: 2 additions & 2 deletions APIKitTests/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class RequestTests: XCTestCase {
return "/"
}

var parameters: [String: AnyObject?] {
var parameters: [String: AnyObject] {
return [
"q": query,
"dummy": nil
"dummy": NSNull()
]
}

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ var path: String
var parameters: [String: AnyObject]
```

`parameters` will be converted into query parameter if `method` is one of `.GET`, `.HEAD` and `.DELETE`. Otherwise, it will be serialized by `requestBodyBuilder` and set to `HTTPBody` of `NSURLRequest`.
`parameters` will be converted into query parameter if `method` is one of `.GET`, `.HEAD` and `.DELETE`.
Otherwise, it will be serialized by `requestBodyBuilder` and set to `HTTPBody` of `NSURLRequest`.
You can pass `NSNull()` as a value for nullable keys if you'd like to preserve the keys when its value is absent.

#### Configuring format of HTTP body

Expand Down

0 comments on commit bbac3af

Please sign in to comment.