-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from NobodyNada/feature/apiresponse-class
Feature/apiresponse class
- Loading branch information
Showing
11 changed files
with
279 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// | ||
// APIResponse.swift | ||
// SwiftStack | ||
// | ||
// Created by FelixSFD on 11.12.16. | ||
// | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
The common wrapper object that is returned by the StackExchange API. | ||
|
||
- authors: FelixSFD, NobodyNada | ||
|
||
- seealso: [StackExchange API](https://api.stackexchange.com/docs/wrapper) | ||
*/ | ||
public class APIResponse<T: JsonConvertible>: JsonConvertible { | ||
|
||
// - MARK: Items | ||
|
||
/** | ||
The items that are returned of the generic type `T`. | ||
|
||
- note: It's always an array. Even if only a single item was requested. | ||
|
||
- author: FelixSFD | ||
*/ | ||
public var items: [T]? | ||
|
||
|
||
// - MARK: Initializers | ||
|
||
/** | ||
Basic initializer without default values | ||
*/ | ||
public init() { | ||
|
||
} | ||
|
||
public required convenience init?(jsonString json: String) { | ||
do { | ||
guard let dictionary = try JSONSerialization.jsonObject(with: json.data(using: String.Encoding.utf8)!) as? [String: Any] else { | ||
return nil | ||
} | ||
|
||
self.init(dictionary: dictionary) | ||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
public required init(dictionary: [String: Any]) { | ||
self.backoff = dictionary["backoff"] as? Int | ||
self.error_id = dictionary["error_id"] as? Int | ||
self.error_message = dictionary["error_message"] as? String | ||
self.error_name = dictionary["error_name"] as? String | ||
self.has_more = dictionary["has_more"] as? Bool | ||
self.page = dictionary["page"] as? Int | ||
self.page_size = dictionary["page_size"] as? Int | ||
self.quota_remaining = dictionary["quota_remaining"] as? Int | ||
self.quota_max = dictionary["quota_max"] as? Int | ||
self.total = dictionary["total"] as? Int | ||
self.type = dictionary["type"] as? String | ||
|
||
|
||
//items | ||
if let array = dictionary["items"] as? [[String: Any]] { | ||
items = array.map { T(dictionary: $0) } | ||
} | ||
|
||
} | ||
|
||
// - MARK: JsonConvertible | ||
|
||
public var dictionary: [String: Any] { | ||
var dict = [String: Any]() | ||
|
||
dict["backoff"] = backoff | ||
dict["error_id"] = error_id | ||
dict["error_message"] = error_message | ||
dict["error_name"] = error_name | ||
dict["has_more"] = has_more | ||
dict["page"] = page | ||
dict["page_size"] = page_size | ||
dict["quota_max"] = quota_max | ||
dict["quota_remaining"] = quota_remaining | ||
dict["total"] = total | ||
dict["type"] = type | ||
|
||
return dict | ||
} | ||
|
||
public var jsonString: String? { | ||
return (try? JsonHelper.jsonString(from: self)) ?? nil | ||
} | ||
|
||
|
||
// - MARK: Fields | ||
|
||
public var backoff: Int? | ||
|
||
public var error_id: Int? | ||
|
||
public var error_message: String? | ||
|
||
public var error_name: String? | ||
|
||
public var has_more: Bool? | ||
|
||
public var page: Int? | ||
|
||
public var page_size: Int? | ||
|
||
public var quota_max: Int? | ||
|
||
public var quota_remaining: Int? | ||
|
||
public var total: Int? | ||
|
||
public var type: String? | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// RequestsSites.swift | ||
// SwiftStack | ||
// | ||
// Created by FelixSFD on 11.12.16. | ||
// | ||
// | ||
|
||
import Foundation | ||
import Dispatch | ||
|
||
/** | ||
This extension contains all requests in the SITES section of the StackExchange API Documentation. | ||
|
||
- authors: FelixSFD, NobodyNada | ||
*/ | ||
public extension APIClient { | ||
|
||
// - MARK: /sites | ||
/** | ||
Fetches all `Sites` in the Stack Exchange network synchronously. | ||
|
||
- parameter parameters: The dictionary of parameters used in the request | ||
|
||
- parameter backoffBehavior: The behavior when an APIRequest has a backoff | ||
|
||
- returns: The list of sites as `APIResponse<Site>` | ||
|
||
- author: NobodyNada | ||
*/ | ||
public func fetchSites( | ||
_ parameters: [String:String] = [:], | ||
backoffBehavior: BackoffBehavior = .wait) throws -> APIResponse<Site> { | ||
|
||
|
||
var params = parameters | ||
params["site"] = "" | ||
|
||
return try performAPIRequest( | ||
"sites", | ||
params, | ||
backoffBehavior: backoffBehavior | ||
) | ||
} | ||
|
||
/** | ||
Fetches all `Sites` in the Stack Exchange network asynchronously. | ||
|
||
- parameter parameters: The dictionary of parameters used in the request | ||
|
||
- parameter backoffBehavior: The behavior when an APIRequest has a backoff | ||
|
||
- author: FelixSFD | ||
*/ | ||
public func fetchSites(_ parameters: [String: String] = [:], backoffBehavior: BackoffBehavior = .wait, completionHandler: @escaping (APIResponse<Site>?, Error?) -> ()) { | ||
|
||
queue.async { | ||
var params = parameters | ||
params["site"] = "" | ||
|
||
do { | ||
let response: APIResponse<Site> = try self.performAPIRequest("sites", params, backoffBehavior: backoffBehavior) | ||
completionHandler(response, nil) | ||
} catch { | ||
completionHandler(nil, error) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.