-
Notifications
You must be signed in to change notification settings - Fork 0
/
VKTwitterAPIManager.m
142 lines (126 loc) · 6.35 KB
/
VKTwitterAPIManager.m
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
//
// VKTwitterAPIManager.m
// Somarufoi2
//
// Created by Kasajima Yasuo on 2013/04/27.
// Copyright (c) 2013年 kasajei. All rights reserved.
//
#import "VKTwitterAPIManager.h"
#import <Social/Social.h>
#import <Twitter/Twitter.h>
#import "VKTwitterAccountManager.h"
#import "VKSocialKit.h"
@implementation VKTwitterAPIManager
#pragma mark - ChangeRequestMethdo
+ (SLRequestMethod)getSLRequestMethodFromVKRequestMethod:(VKRequestMethod)requestMethod{
switch (requestMethod) {
case kVKRequestGET:
return SLRequestMethodGET;
break;
case kVKRequestPOST:
return SLRequestMethodPOST;
break;
default:
break;
}
}
+ (TWRequestMethod)getTWRequestMethodFromVKRequestMethod:(VKRequestMethod)requestMethod{
switch (requestMethod) {
case kVKRequestGET:
return TWRequestMethodGET;
break;
case kVKRequestPOST:
return TWRequestMethodPOST;
break;
default:
break;
}
}
#pragma mark - Base Request
+ (void)performRequestWithAPIURL:(NSString *)urlString requestMethod:(VKRequestMethod)requestMethod params:(NSDictionary *)params complete:(void(^)(NSData *responseData))complete failure:(void(^)(NSError *error))failure{
NSString *fullRUL = [NSString stringWithFormat:@"https://api.twitter.com/1.1/%@", urlString];
NSURL *url = [NSURL URLWithString:fullRUL];
id request;
if ([VKSocialKit hasSocialFramework]) {
request =[SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:[self getSLRequestMethodFromVKRequestMethod:requestMethod]
URL:url
parameters:params
];
}else if( [VKSocialKit hasTwitterFramework]){
request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:[self getTWRequestMethodFromVKRequestMethod:requestMethod]
];
}else{
return;
}
if (![VKTwitterAccountManager sharedInstance].selectedAccount) {
NSError *error = [NSError errorWithDomain:@"VKTwitter.AccountError" code:400 userInfo:nil];
failure(error);
return;
}
ACAccount *account = [VKTwitterAccountManager sharedInstance].selectedAccount;
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccount *account2 = [accountStore accountWithIdentifier:account.identifier];
[request setAccount:account2];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (error) {
failure(error);
}else{
if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
NSError *jsonError;
id JSON = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
if (JSON) {
complete(JSON);
}else {
NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
failure(jsonError);
}
}else{
failure(error);
}
}
}];
}
#pragma mark - APIs
// Twitter API https://dev.twitter.com/docs/api/1.1
#pragma mark - Timelines
+ (void)statusesMentionsTimeline:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError* error))failure{
NSString *urlString = @"statuses/mentions_timeline.json";
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestGET params:param complete:complete failure:failure];
}
+ (void)statusesUserTimeline:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError* error))failure{
NSString *urlString = @"statuses/user_timeline.json";
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestGET params:param complete:complete failure:failure];
}
+ (void)statusesRetweetsOfMe:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError* error))failure{
NSString *urlString = @"statuses/retweets_of_me.json";
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestGET params:param complete:complete failure:failure];
}
#pragma mark - Tweets
+ (void)statusesRetweets:(NSString *)tweetId param:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError* error))failure{
NSString *urlString = [NSString stringWithFormat:@"statuses/retweets/%@",tweetId];
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestGET params:param complete:complete failure:failure];
}
+ (void)statusesShow:(NSString *)tweetId param:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError* error))failure{
NSString *urlString = [NSString stringWithFormat:@"statuses/show/%@",tweetId];
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestGET params:param complete:complete failure:failure];
}
+ (void)statusesDestroy:(NSString *)tweetId param:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError* error))failure{
NSString *urlString = [NSString stringWithFormat:@"statuses/destroy/%@",tweetId];
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestPOST params:param complete:complete failure:failure];
}
+ (void)statusesUpdate:(NSString *)status param:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError *error))failure{
NSString *urlString = @"statuses/update.json";
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:param];
[params setObject:status forKey:@"status"];
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestPOST params:params complete:complete failure:failure];
}
+ (void)statusesRetweet:(NSString *)tweetId param:(NSDictionary *)param complete:(void(^)(id JSON))complete failure:(void(^)(NSError* error))failure{
NSString *urlString = [NSString stringWithFormat:@"statuses/retweet/%@",tweetId];
[self performRequestWithAPIURL:urlString requestMethod:kVKRequestPOST params:param complete:complete failure:failure];
}
@end