Skip to content

Commit

Permalink
Use Params typealias for [String: Any]
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Apr 13, 2017
1 parent 69a7d0b commit 7e120e5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
15 changes: 8 additions & 7 deletions ws/WS+Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@

import Foundation

public typealias Params = [String :Any]

extension WS {

public func getRequest(_ url: String, params: [String:Any] = [String: Any]()) -> WSRequest {
public func getRequest(_ url: String, params: Params = Params()) -> WSRequest {
return call(url, verb: .get, params: params)
}

public func putRequest(_ url: String, params: [String:Any] = [String: Any]()) -> WSRequest {
public func putRequest(_ url: String, params: Params = Params()) -> WSRequest {
return call(url, verb: .put, params: params)
}

public func postRequest(_ url: String, params: [String: Any] = [String: Any]()) -> WSRequest {
public func postRequest(_ url: String, params: Params = Params()) -> WSRequest {
return call(url, verb: .post, params: params)
}

public func deleteRequest(_ url: String, params: [String: Any] = [String: Any]()) -> WSRequest {
public func deleteRequest(_ url: String, params: Params = Params()) -> WSRequest {
return call(url, verb: .delete, params: params)
}

public func postMultipartRequest(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
name: String,
data: Data,
fileName: String,
Expand All @@ -42,7 +44,7 @@ extension WS {
}

public func putMultipartRequest(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
name: String,
data: Data,
fileName: String,
Expand All @@ -55,5 +57,4 @@ extension WS {
c.multipartMimeType = mimeType
return c
}

}
24 changes: 12 additions & 12 deletions ws/WS+TypedCalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import then
extension WS {

public func get<T: ArrowParsable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<[T]> {
let keypath = keypath ?? defaultCollectionParsingKeyPath
return getRequest(url, params: params).fetch()
Expand All @@ -23,32 +23,32 @@ extension WS {
}

public func get<T: ArrowParsable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return resourceCall(.get, url: url, params: params, keypath: keypath)
}

public func post<T: ArrowParsable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return resourceCall(.post, url: url, params: params, keypath: keypath)
}

public func put<T: ArrowParsable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return resourceCall(.put, url: url, params: params, keypath: keypath)
}

public func delete<T: ArrowParsable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return resourceCall(.delete, url: url, params: params, keypath: keypath)
}

private func resourceCall<T: ArrowParsable>(_ verb: WSHTTPVerb,
url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
let c = defaultCall()
c.httpVerb = verb
Expand All @@ -66,7 +66,7 @@ extension WS {
extension WS {

public func get<T: ArrowInitializable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<[T]> {
let keypath = keypath ?? defaultCollectionParsingKeyPath
return getRequest(url, params: params)
Expand All @@ -84,31 +84,31 @@ extension WS {
}

public func get<T: ArrowInitializable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return typeCall(.get, url: url, params: params, keypath: keypath)
}

public func post<T: ArrowInitializable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return typeCall(.post, url: url, params: params, keypath: keypath)
}

public func put<T: ArrowInitializable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return typeCall(.put, url: url, params: params, keypath: keypath)
}

public func delete<T: ArrowInitializable>(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
return typeCall(.delete, url: url, params: params, keypath: keypath)
}

private func typeCall<T: ArrowInitializable>(_ verb: WSHTTPVerb,
url: String, params: [String: Any] = [String: Any](),
url: String, params: Params = Params(),
keypath: String? = nil) -> Promise<T> {
let c = defaultCall()
c.httpVerb = verb
Expand Down
22 changes: 11 additions & 11 deletions ws/WS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ open class WS {

// MARK: - Calls

internal func call(_ url: String, verb: WSHTTPVerb = .get, params: [String: Any] = [String: Any]()) -> WSRequest {
internal func call(_ url: String, verb: WSHTTPVerb = .get, params: Params = Params()) -> WSRequest {
let c = defaultCall()
c.httpVerb = verb
c.URL = url
Expand All @@ -79,43 +79,43 @@ open class WS {

// MARK: JSON calls

open func get(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<JSON> {
open func get(_ url: String, params: Params = Params()) -> Promise<JSON> {
return getRequest(url, params: params).fetch().resolveOnMainThread()
}

open func post(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<JSON> {
open func post(_ url: String, params: Params = Params()) -> Promise<JSON> {
return postRequest(url, params: params).fetch().resolveOnMainThread()
}

open func put(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<JSON> {
open func put(_ url: String, params: Params = Params()) -> Promise<JSON> {
return putRequest(url, params: params).fetch().resolveOnMainThread()
}

open func delete(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<JSON> {
open func delete(_ url: String, params: Params = Params()) -> Promise<JSON> {
return deleteRequest(url, params: params).fetch().resolveOnMainThread()
}

// MARK: Void calls

open func get(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<Void> {
open func get(_ url: String, params: Params = Params()) -> Promise<Void> {
let r = getRequest(url, params: params)
r.returnsJSON = false
return r.fetch().registerThen { (_: JSON) -> Void in }.resolveOnMainThread()
}

open func post(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<Void> {
open func post(_ url: String, params: Params = Params()) -> Promise<Void> {
let r = postRequest(url, params: params)
r.returnsJSON = false
return r.fetch().registerThen { (_:JSON) -> Void in }.resolveOnMainThread()
}

open func put(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<Void> {
open func put(_ url: String, params: Params = Params()) -> Promise<Void> {
let r = putRequest(url, params: params)
r.returnsJSON = false
return r.fetch().registerThen { (_:JSON) -> Void in }.resolveOnMainThread()
}

open func delete(_ url: String, params: [String: Any] = [String: Any]()) -> Promise<Void> {
open func delete(_ url: String, params: Params = Params()) -> Promise<Void> {
let r = deleteRequest(url, params: params)
r.returnsJSON = false
return r.fetch().registerThen { (_: JSON) -> Void in }.resolveOnMainThread()
Expand All @@ -124,7 +124,7 @@ open class WS {
// MARK: - Multipart

open func postMultipart(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
name: String,
data: Data,
fileName: String,
Expand All @@ -139,7 +139,7 @@ open class WS {
}

open func putMultipart(_ url: String,
params: [String: Any] = [String: Any](),
params: Params = Params(),
name: String,
data: Data,
fileName: String,
Expand Down

0 comments on commit 7e120e5

Please sign in to comment.