Skip to content

Commit

Permalink
Merge pull request #11 from NobodyNada/feature/swifty-api
Browse files Browse the repository at this point in the history
Swiftier API
  • Loading branch information
NobodyNada authored Dec 20, 2016
2 parents 776de9f + 442334e commit b103a22
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Sources/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ open class APIClient: NSObject, URLSessionDataDelegate {
///- parameter parameters: Parameters to be URLEncoded into the request.
open func performAPIRequest<T>(
_ request: String,
_ parameters: [String:String] = [:],
parameters: [String:String] = [:],
backoffBehavior: BackoffBehavior = .wait
) throws -> APIResponse<T> {

Expand Down Expand Up @@ -249,7 +249,7 @@ open class APIClient: NSObject, URLSessionDataDelegate {
/// - fields: The data to POST.
///
///- returns: The data and response returned by the request.
open func post(_ url: String, _ fields: [String:String]) throws -> (Data, HTTPURLResponse) {
open func post(_ url: String, fields: [String:String]) throws -> (Data, HTTPURLResponse) {
guard let nsUrl = URL(string: url) else {
throw RequestError.invalidURL(url: url)
}
Expand Down Expand Up @@ -326,8 +326,8 @@ open class APIClient: NSObject, URLSessionDataDelegate {
/// - fields: The data to POST.
///
///- returns: The text returned by the request.
open func post(_ url: String, _ fields: [String:String]) throws -> String {
let (data, _) = try post(url, fields)
open func post(_ url: String, fields: [String:String]) throws -> String {
let (data, _) = try post(url, fields: fields)
guard let string = String(data: data, encoding: String.Encoding.utf8) else {
throw RequestError.notUTF8
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/RequestsQuestions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public extension APIClient {
*/
public func fetchQuestions(
_ ids: [Int],
_ parameters: [String:String] = [:],
parameters: [String:String] = [:],
backoffBehavior: BackoffBehavior = .wait) throws -> APIResponse<Question> {

guard !ids.isEmpty else {
Expand All @@ -42,7 +42,7 @@ public extension APIClient {

return try performAPIRequest(
"questions/\(ids.map {String($0)}.joined(separator: ";"))",
parameters,
parameters: parameters,
backoffBehavior: backoffBehavior
)
}
Expand All @@ -62,15 +62,15 @@ public extension APIClient {
*/
public func fetchQuestions(
_ ids: [Int],
_ parameters: [String: String] = [:],
parameters: [String: String] = [:],
backoffBehavior: BackoffBehavior = .wait,
completionHandler: @escaping (APIResponse<Question>?, Error?) -> ()) {

queue.async {
do {
let response: APIResponse<Question> = try self.fetchQuestions(
ids,
parameters,
parameters: parameters,
backoffBehavior: backoffBehavior
)

Expand All @@ -97,10 +97,10 @@ public extension APIClient {
*/
public func fetchQuestion(
_ id: Int,
_ parameters: [String:String] = [:],
parameters: [String:String] = [:],
backoffBehavior: BackoffBehavior = .wait) throws -> APIResponse<Question> {

return try fetchQuestions([id], parameters, backoffBehavior: backoffBehavior)
return try fetchQuestions([id], parameters: parameters, backoffBehavior: backoffBehavior)
}

/**
Expand All @@ -118,11 +118,11 @@ public extension APIClient {
*/
public func fetchQuestion(
_ id: Int,
_ parameters: [String: String] = [:],
parameters: [String: String] = [:],
backoffBehavior: BackoffBehavior = .wait,
completionHandler: @escaping (APIResponse<Question>?, Error?) -> ()) {

fetchQuestions([id], parameters, backoffBehavior: backoffBehavior, completionHandler: completionHandler)
fetchQuestions([id], parameters: parameters, backoffBehavior: backoffBehavior, completionHandler: completionHandler)
}

}
8 changes: 4 additions & 4 deletions Sources/RequestsSites.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public extension APIClient {
- author: NobodyNada
*/
public func fetchSites(
_ parameters: [String:String] = [:],
parameters: [String:String] = [:],
backoffBehavior: BackoffBehavior = .wait) throws -> APIResponse<Site> {


Expand All @@ -38,7 +38,7 @@ public extension APIClient {

return try performAPIRequest(
"sites",
params,
parameters: params,
backoffBehavior: backoffBehavior
)
}
Expand All @@ -52,12 +52,12 @@ public extension APIClient {

- author: FelixSFD
*/
public func fetchSites(_ parameters: [String: String] = [:], backoffBehavior: BackoffBehavior = .wait, completionHandler: @escaping (APIResponse<Site>?, Error?) -> ()) {
public func fetchSites(parameters: [String: String] = [:], backoffBehavior: BackoffBehavior = .wait, completionHandler: @escaping (APIResponse<Site>?, Error?) -> ()) {

queue.async {

do {
let response: APIResponse<Site> = try self.fetchSites(parameters, backoffBehavior: backoffBehavior)
let response: APIResponse<Site> = try self.fetchSites(parameters: parameters, backoffBehavior: backoffBehavior)
completionHandler(response, nil)
} catch {
completionHandler(nil, error)
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftStackTests/APITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class APITests: XCTestCase {
return ("{}".data(using: .utf8), self.blankResponse(task), nil)
}

let _ = try client.performAPIRequest("info", ["page":"1"]) as APIResponse<Site>
let _ = try client.performAPIRequest("info", parameters: ["page":"1"]) as APIResponse<Site>
}


Expand All @@ -151,7 +151,7 @@ class APITests: XCTestCase {
return ("{}".data(using: .utf8), self.blankResponse(task), nil)
}

let _ = try client.performAPIRequest("info", parameters) as APIResponse<Site>
let _ = try client.performAPIRequest("info", parameters: parameters) as APIResponse<Site>
//not actually a Site, but Info isn't implemented yet.
}

Expand Down Expand Up @@ -269,7 +269,7 @@ class APITests: XCTestCase {
return ("{\"items\": [{\"name\": \"Test Site\"}]}".data(using: .utf8), self.blankResponse(task), nil)
}

client.fetchSites([:], backoffBehavior: .wait) {
client.fetchSites(parameters: [:], backoffBehavior: .wait) {
response, error in
if error != nil {
print(error!)
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftStackTests/BasicRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BasicRequestTests: XCTestCase {
let client = APIClient()

let url = "https://httpbin.org/post"
let result = try? client.post(url, ["test":"123456"]) as String
let result = try? client.post(url, fields: ["test":"123456"]) as String
XCTAssert(result != nil)

let json = (try? client.parseJSON(result!)) as? [String:Any]
Expand Down

0 comments on commit b103a22

Please sign in to comment.