Skip to content

Commit

Permalink
Add parameters support
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul committed Nov 15, 2017
1 parent 57c0b76 commit 9fe6233
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
25 changes: 18 additions & 7 deletions Sources/MoyaSugar/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import Moya
///
/// ```
/// JSONEncoding() => [
/// "key1": "value1"
/// "key2": "value2"
/// "key1": "value1",
/// "key2": "value2",
/// "key3": nil, // will be ignored
/// ]
/// ```
public struct Parameters {
public var encoding: Alamofire.ParameterEncoding
public var values: [String: Any]

public init(encoding: Alamofire.ParameterEncoding, values: [String: Any]) {
public init(encoding: Alamofire.ParameterEncoding, values: [String: Any?]) {
self.encoding = encoding
self.values = values
self.values = filterNil(values)
}
}

extension Parameters: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String, Any)...) {
var values: [String: Any] = [:]
public init(dictionaryLiteral elements: (String, Any?)...) {
var values: [String: Any?] = [:]
for (key, value) in elements {
values[key] = value
}
Expand All @@ -33,6 +34,16 @@ extension Parameters: ExpressibleByDictionaryLiteral {

infix operator =>

public func => (encoding: Alamofire.ParameterEncoding, values: [String: Any]) -> Parameters {
public func => (encoding: Alamofire.ParameterEncoding, values: [String: Any?]) -> Parameters {
return Parameters(encoding: encoding, values: values)
}

/// Returns a new dictinoary by filtering out nil values.
private func filterNil(_ dictionary: [String: Any?]) -> [String: Any] {
var newDictionary: [String: Any] = [:]
for (key, value) in dictionary {
guard let value = value else { continue }
newDictionary[key] = value
}
return newDictionary
}
6 changes: 6 additions & 0 deletions Sources/MoyaSugar/SugarTargetType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public protocol SugarTargetType: TargetType {
/// }
/// ```
var route: Route { get }
var parameters: Parameters? { get }
}

public extension SugarTargetType {
Expand All @@ -33,4 +34,9 @@ public extension SugarTargetType {
public var method: Moya.Method {
return self.route.method
}

public var task: Task {
guard let parameters = self.parameters else { return .requestPlain }
return .requestParameters(parameters: parameters.values, encoding: parameters.encoding)
}
}
35 changes: 14 additions & 21 deletions Tests/MoyaSugarTests/GitHubAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,23 @@ enum GitHubAPI: SugarTargetType, Then {
}
}

var task: Task {
var parameters: Parameters? {
switch self {
case .url: return .requestPlain
case .index: return .requestPlain
case .userRepos: return .requestPlain
case .url: return nil
case .index: return nil
case .userRepos: return nil

case .createIssue(_, _, let title, let body):
return .requestParameters(
parameters: filterNil([
"title": title,
"body": body,
]),
encoding: JSONEncoding()
)
case let .createIssue(_, _, title, body):
return JSONEncoding() => [
"title": title,
"body": body,
]

case .editIssue(_, _, _, let title, let body):
return .requestParameters(
parameters: filterNil([
"title": title,
"body": body,
]),
encoding: URLEncoding()
)
case let .editIssue(_, _, _, title, body):
return URLEncoding() => [
"title": title,
"body": body,
]
}
}

Expand All @@ -81,4 +75,3 @@ enum GitHubAPI: SugarTargetType, Then {
return Data()
}
}

0 comments on commit 9fe6233

Please sign in to comment.