-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterAPICaller.swift
67 lines (59 loc) · 2.93 KB
/
TwitterAPICaller.swift
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
//
// APIManager.swift
// Twitter
//
// Created by Dan on 1/3/19.
// Copyright © 2019 Dan. All rights reserved.
//
import UIKit
import BDBOAuth1Manager
class TwitterAPICaller: BDBOAuth1SessionManager {
static let client = TwitterAPICaller(baseURL: URL(string: "https://api.twitter.com"), consumerKey: "5lUJuO5AUpPUCez4ewYDFrtgh", consumerSecret: "s5ynGqXzstUZwFPxVyMDkYh197qvHOcVM3kwv1o2TKhS1avCdS")
var loginSuccess: (() -> ())?
var loginFailure: ((Error) -> ())?
func handleOpenUrl(url: URL){
let requestToken = BDBOAuth1Credential(queryString: url.query)
TwitterAPICaller.client?.fetchAccessToken(withPath: "oauth/access_token", method: "POST", requestToken: requestToken, success: { (accessToken: BDBOAuth1Credential!) in
self.loginSuccess?()
}, failure: { (error: Error!) in
self.loginFailure?(error)
})
}
func login(url: String, success: @escaping () -> (), failure: @escaping (Error) -> ()){
loginSuccess = success
loginFailure = failure
TwitterAPICaller.client?.deauthorize()
TwitterAPICaller.client?.fetchRequestToken(withPath: url, method: "GET", callbackURL: URL(string: "alamoTwitter://oauth"), scope: nil, success: { (requestToken: BDBOAuth1Credential!) -> Void in
let url = URL(string: "https://api.twitter.com/oauth/authorize?oauth_token=\(requestToken.token!)")!
UIApplication.shared.open(url)
}, failure: { (error: Error!) -> Void in
print("Error: \(error.localizedDescription)")
self.loginFailure?(error)
})
}
func logout (){
deauthorize()
}
func getDictionaryRequest(url: String, parameters: [String:Any], success: @escaping (NSDictionary) -> (), failure: @escaping (Error) -> ()){
TwitterAPICaller.client?.get(url, parameters: parameters, progress: nil, success: { (task: URLSessionDataTask, response: Any?) in
success(response as! NSDictionary)
}, failure: { (task: URLSessionDataTask?, error: Error) in
failure(error)
})
}
func getDictionariesRequest(url: String, parameters: [String:Any], success: @escaping ([NSDictionary]) -> (), failure: @escaping (Error) -> ()){
print(parameters)
TwitterAPICaller.client?.get(url, parameters: parameters, progress: nil, success: { (task: URLSessionDataTask, response: Any?) in
success(response as! [NSDictionary])
}, failure: { (task: URLSessionDataTask?, error: Error) in
failure(error)
})
}
func postRequest(url: String, parameters: [Any], success: @escaping () -> (), failure: @escaping (Error) -> ()){
TwitterAPICaller.client?.post(url, parameters: parameters, progress: nil, success: { (task: URLSessionDataTask, response: Any?) in
success()
}, failure: { (task: URLSessionDataTask?, error: Error) in
failure(error)
})
}
}