mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-10 05:16:22 +10:00
add DribbbleShot; GET dribbbleShotsWithToken
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
import Foundation
|
||||
|
||||
private let githubBaseURL = NSURL(string: "https://api.github.com")!
|
||||
private let dribbbleBaseURL = NSURL(string: "https://api.dribbble.com")!
|
||||
private let instagramBaseURL = NSURL(string: "https://api.instagram.com")!
|
||||
|
||||
private func githubResource<A>(token token: String, path: String, method: Method, requestParameters: JSONDictionary, parse: JSONDictionary -> A?) -> Resource<A> {
|
||||
@@ -30,6 +31,25 @@ private func githubResource<A>(token token: String, path: String, method: Method
|
||||
return Resource(path: path, method: method, requestBody: jsonBody, headers: headers, parse: jsonParse)
|
||||
}
|
||||
|
||||
private func dribbbleResource<A>(token token: String, path: String, method: Method, requestParameters: JSONDictionary, parse: JSONDictionary -> A?) -> Resource<A> {
|
||||
|
||||
let jsonParse: NSData -> A? = { data in
|
||||
if let json = decodeJSON(data) {
|
||||
return parse(json)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let jsonBody = encodeJSON(requestParameters)
|
||||
var headers = [
|
||||
"Content-Type": "application/json",
|
||||
]
|
||||
|
||||
headers["Authorization"] = "Bearer \(token)"
|
||||
|
||||
return Resource(path: path, method: method, requestBody: jsonBody, headers: headers, parse: jsonParse)
|
||||
}
|
||||
|
||||
private func instagramResource<A>(token token: String, path: String, method: Method, requestParameters: JSONDictionary, parse: JSONDictionary -> A?) -> Resource<A> {
|
||||
|
||||
let jsonParse: NSData -> A? = { data in
|
||||
@@ -47,6 +67,8 @@ private func instagramResource<A>(token token: String, path: String, method: Met
|
||||
return Resource(path: path, method: method, requestBody: jsonBody, headers: headers, parse: jsonParse)
|
||||
}
|
||||
|
||||
// MARK: Github Repo
|
||||
|
||||
struct GithubRepo {
|
||||
let ID: Int
|
||||
let name: String
|
||||
@@ -104,6 +126,79 @@ func githubReposWithToken(token: String, failureHandler: ((Reason, String?) -> V
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Dribbble Shot
|
||||
|
||||
struct DribbbleShot {
|
||||
let ID: Int
|
||||
let title: String
|
||||
let description: String
|
||||
let htmlURLString: String
|
||||
|
||||
struct Images {
|
||||
let hidpi: String?
|
||||
let normal: String
|
||||
let teaser: String
|
||||
}
|
||||
let images: Images
|
||||
|
||||
let likesCount: Int
|
||||
let commentsCount: Int
|
||||
}
|
||||
|
||||
func dribbbleShotsWithToken(token: String, failureHandler: ((Reason, String?) -> Void)?, completion: [DribbbleShot] -> Void) {
|
||||
|
||||
let requestParameters = [
|
||||
"timeframe": "month",
|
||||
"sort": "recent",
|
||||
]
|
||||
|
||||
let parse: JSONDictionary -> [DribbbleShot]? = { data in
|
||||
|
||||
println("dribbbleShotsWithToken data: \(data)")
|
||||
|
||||
guard let shotsData = data["data"] as? [JSONDictionary] else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var shots = [DribbbleShot]()
|
||||
|
||||
for shotInfo in shotsData {
|
||||
if let
|
||||
ID = shotInfo["id"] as? Int,
|
||||
title = shotInfo["title"] as? String,
|
||||
description = shotInfo["description"] as? String,
|
||||
htmlURLString = shotInfo["html_url"] as? String,
|
||||
imagesInfo = shotInfo["images"] as? JSONDictionary,
|
||||
likesCount = shotInfo["likes_count"] as? Int,
|
||||
commentsCount = shotInfo["comments_count"] as? Int {
|
||||
if let
|
||||
normal = imagesInfo["normal"] as? String,
|
||||
teaser = imagesInfo["teaser"] as? String {
|
||||
let hidpi = imagesInfo["hidpi"] as? String
|
||||
|
||||
let images = DribbbleShot.Images(hidpi: hidpi, normal: normal, teaser: teaser)
|
||||
|
||||
let shot = DribbbleShot(ID: ID, title: title, description: description, htmlURLString: htmlURLString, images: images, likesCount: likesCount, commentsCount: commentsCount)
|
||||
|
||||
shots.append(shot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shots
|
||||
}
|
||||
|
||||
let resource = dribbbleResource(token: token, path: "/v1/shots", method: .GET, requestParameters: requestParameters, parse: parse)
|
||||
|
||||
if let failureHandler = failureHandler {
|
||||
apiRequest({_ in}, baseURL: dribbbleBaseURL, resource: resource, failure: failureHandler, completion: completion)
|
||||
} else {
|
||||
apiRequest({_ in}, baseURL: dribbbleBaseURL, resource: resource, failure: defaultFailureHandler, completion: completion)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Instagram Media
|
||||
|
||||
struct InstagramMedia {
|
||||
let ID: String
|
||||
let linkURLString: String
|
||||
|
||||
@@ -149,6 +149,13 @@ class ConversationsViewController: UIViewController {
|
||||
})
|
||||
}
|
||||
|
||||
if let dribbbleToken = tokensOfSocialAccounts.dribbbleToken {
|
||||
|
||||
dribbbleShotsWithToken(dribbbleToken, failureHandler: nil, completion: { dribbbleShots in
|
||||
println("dribbbleShots count: \(dribbbleShots.count)")
|
||||
})
|
||||
}
|
||||
|
||||
if let instagramToken = tokensOfSocialAccounts.instagramToken {
|
||||
|
||||
instagramMediasWithToken(instagramToken, failureHandler: nil, completion: { instagramMedias in
|
||||
|
||||
Reference in New Issue
Block a user