-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterClient.swift
More file actions
171 lines (154 loc) · 8.13 KB
/
TwitterClient.swift
File metadata and controls
171 lines (154 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// TwitterClient.swift
// twitterApp
//
// Created by Deepak on 9/27/14.
// Copyright (c) 2014 Deepak. All rights reserved.
//
import UIKit
let TWITTER_API_CONSUMER_KEY = "9zVFEKgKCVsj2kR57TH7Kavqe"
let TWITTER_API_CONSUMER_SECRET = "duiofpcQ32pZGCYwts447WKV15QmgXQvkwB4KV4BJnwsDXi3Mb"
let TWITTER_API_TOKEN = "116844089-93VBvdAw78KJysZdYoWAzcgIDzIWPHdGfF5sQppU"
let TWITTER_API_TOKEN_SECRET = "wh7hbfVkx0V9z4923ClXRS2nOsmFTHEF8lJ7toPCSri5s"
let TWITTER_API_OAUTH1_AUTHENTICATE_URL = NSURL(string: "https://api.twitter.com/oauth/authenticate")
let TWITTER_API_OAUTH2_TOKEN_URL = NSURL(string: "https://api.twitter.com/oauth2/token")
let TWITTER_API_USER_TIMELINE_URL = NSURL(string: "https://api.twitter.com/1.1/statuses/user_timeline.json")
let TWITTER_API_BASE_URL = NSURL(string: "https://api.twitter.com")
class TwitterClient: BDBOAuth1RequestOperationManager {
var loginCompletion : ((user: User?, error: NSError?) -> ())?
class var sharedInstance : TwitterClient {
struct Static {
static let instance = TwitterClient(baseURL: TWITTER_API_BASE_URL, consumerKey: TWITTER_API_CONSUMER_KEY, consumerSecret: TWITTER_API_CONSUMER_SECRET)
}
return Static.instance
}
func homeTimeLineWithParams(params: NSDictionary?,
completion: (tweets: [Tweet]?, error: NSError?) -> ()) {
GET("1.1/statuses/home_timeline.json" ,
parameters: params, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
//println("timeline : \(response)")
var tweets = Tweet.tweetsWithArray(response as [NSDictionary])
completion(tweets: tweets, error: nil)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println("error getting timeline")
//self.loginCompletion? (user: nil, error: error)
completion(tweets: nil, error: error)
})
}
func userTimeLineWithParams(params: NSDictionary?,
completion: (tweets: [Tweet]?, error: NSError?) -> ()) {
GET("1.1/statuses/user_timeline.json" ,
parameters: params, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
//println("mentions : \(response)")
var tweets = Tweet.tweetsWithArray(response as [NSDictionary])
completion(tweets: tweets, error: nil)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println("error getting timeline")
//self.loginCompletion? (user: nil, error: error)
completion(tweets: nil, error: error)
})
}
func mentionsWithParams(params: NSDictionary?,
completion: (tweets: [Tweet]?, error: NSError?) -> ()) {
GET("1.1/statuses/mentions_timeline.json" ,
parameters: params, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
//println("mentions : \(response)")
var tweets = Tweet.tweetsWithArray(response as [NSDictionary])
completion(tweets: tweets, error: nil)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println("error getting timeline")
//self.loginCompletion? (user: nil, error: error)
completion(tweets: nil, error: error)
})
}
func loginWithCompletion(completion: (user: User?, error: NSError?) -> ()) {
loginCompletion = completion
//Fetch request token and redirect to authorization page
requestSerializer.removeAccessToken()
fetchRequestTokenWithPath("oauth/request_token", method: "GET", callbackURL: NSURL(string: "cptwitterdemo://oauth"), scope: nil, success: { (requestToken : BDBOAuthToken!) -> Void in
println("Got the request Token")
var authURL = NSURL(string: "https://api.twitter.com/oauth/authorize?oauth_token=\(requestToken.token)")
UIApplication.sharedApplication().openURL(authURL)
}) { (error : NSError!) -> Void in
println("Unable to get the request Token")
self.loginCompletion? (user: nil, error: error)
}
}
func openURL(url: NSURL) {
fetchAccessTokenWithPath("oauth/access_token", method: "POST", requestToken: BDBOAuthToken(queryString: url.query), success: { (accessToken: BDBOAuthToken!) -> Void in
println("Suceesfully got the Access Token")
TwitterClient.sharedInstance.GET("1.1/account/verify_credentials.json" ,
parameters: nil, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
//println("User : \(response)")
var user = User(dictionary: response as NSDictionary)
User.currentUser = user
//println("User : \(user.name)")
self.loginCompletion? (user: user, error: nil)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println("error getting user")
self.loginCompletion? (user: nil, error: error)
})
}) { (error : NSError!) -> Void in
println("Failed to recieve Access Token")
self.loginCompletion? (user: nil, error: error)
}
}
func favoriteWithID(tweet_id: String, callback: (response: AnyObject!, error: NSError!) -> Void) {
sendPostRequest("1.1/favorites/create.json",
parameters: [ "id": tweet_id ],
callback: callback)
}
func unfavoriteWithID(tweet_id: String, callback: (response: AnyObject!, error: NSError!) -> Void) {
sendPostRequest("1.1/favorites/destroy.json",
parameters: [ "id": tweet_id ],
callback: callback)
}
func tweet(status: String, callback: (response: AnyObject!, error: NSError!) -> Void) {
sendPostRequest("1.1/statuses/update.json",
parameters: [ "status": status ],
callback: callback)
}
func retweet(tweet_id: String, callback: (response: AnyObject!, error: NSError!) -> Void) {
sendPostRequest("1.1/statuses/retweet/\(tweet_id).json",
parameters: nil,
callback: callback)
}
func destroy(tweet_id: String, callback: (response: AnyObject!, error: NSError!) -> Void) {
sendPostRequest("1.1/statuses/destroy/\(tweet_id).json",
parameters: nil,
callback: callback)
}
func getRetweets(tweet_id: String, callback: (response: AnyObject!, error: NSError!) -> Void) {
sendPostRequest("1.1/statuses/retweets/\(tweet_id).json",
parameters: nil,
callback: callback)
}
func sendPostRequest(endpoint: String, parameters: [String: String]!, callback: (response: AnyObject!, error: NSError!) -> Void) {
POST(endpoint,
parameters: parameters,
success: {
// Success
(operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
callback(response: response, error: nil)
},
failure: {
// Failure
(operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
callback(response: nil, error: error)
})
}
func sendGetRequest(endpoint: String, parameters: [String: String]!, callback: (response: AnyObject!, error: NSError!) -> Void) {
GET(endpoint,
parameters: parameters,
success: {
// Success
(operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
callback(response: response, error: nil)
},
failure: {
// Failure
(operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
callback(response: nil, error: error)
})
}
}