commit | author | age
|
a0a843
|
1 |
// |
H |
2 |
// TWTRAPIClient.h |
|
3 |
// |
|
4 |
// Copyright (c) 2015 Twitter. All rights reserved. |
|
5 |
// |
|
6 |
|
|
7 |
@class TWTRUser; |
|
8 |
@class TWTRTweet; |
|
9 |
@class TWTRAuthConfig; |
|
10 |
@class TWTRGuestSession; |
|
11 |
@protocol TWTRAuthSession; |
|
12 |
@protocol TWTRSessionStore; |
|
13 |
|
|
14 |
NS_ASSUME_NONNULL_BEGIN |
|
15 |
|
|
16 |
FOUNDATION_EXPORT NSString *const TWTRTweetsNotLoadedKey; |
|
17 |
|
|
18 |
/** |
|
19 |
* @name Completion Block Types |
|
20 |
*/ |
|
21 |
|
|
22 |
/** |
|
23 |
* Completion block called when the load user request succeeds or fails. |
|
24 |
* |
|
25 |
* @param user The Twitter User. |
|
26 |
* @param error Error that will be set if the API request failed. |
|
27 |
*/ |
|
28 |
typedef void (^TWTRLoadUserCompletion)(TWTRUser *_Nullable user, NSError *_Nullable error); |
|
29 |
|
|
30 |
/** |
|
31 |
* Completion block called when the load Tweet request succeeds or fails. |
|
32 |
* |
|
33 |
* @param tweet The Twitter Tweet. |
|
34 |
* @param error Error that will be set if the API request failed. |
|
35 |
*/ |
|
36 |
typedef void (^TWTRLoadTweetCompletion)(TWTRTweet *_Nullable tweet, NSError *_Nullable error); |
|
37 |
|
|
38 |
/** |
|
39 |
* Completion block called when the load Tweets request succeeds or fails. |
|
40 |
* |
|
41 |
* @param tweets Tweets that were successfully retrieved. |
|
42 |
* @param error Error that will be set if the API request failed. |
|
43 |
*/ |
|
44 |
typedef void (^TWTRLoadTweetsCompletion)(NSArray<TWTRTweet *> *_Nullable tweets, NSError *_Nullable error); |
|
45 |
|
|
46 |
/** |
|
47 |
* Completion block called when the network request succeeds or fails. |
|
48 |
* |
|
49 |
* @param response Metadata associated with the response to a URL load request. |
|
50 |
* @param data Content data of the response. |
|
51 |
* @param connectionError Error object describing the network error that occurred. |
|
52 |
*/ |
|
53 |
typedef void (^TWTRNetworkCompletion)(NSURLResponse *_Nullable response, NSData *_Nullable data, NSError *_Nullable connectionError); |
|
54 |
|
|
55 |
/** |
|
56 |
* Completion block called when a JSON request to the Twitter API succeeds or fails. |
|
57 |
* |
|
58 |
* @param response Metadata associated with the response to a URL load request. |
|
59 |
* @param responseObject Content data of the response. |
|
60 |
* @param error Error object describing the network error that occurred. |
|
61 |
*/ |
|
62 |
typedef void (^TWTRJSONRequestCompletion)(NSURLResponse *_Nullable response, id _Nullable responseObject, NSError *_Nullable error); |
|
63 |
|
|
64 |
/** |
|
65 |
* Completion block called when a Tweet action (favorite/retweet) is performed. |
|
66 |
* |
|
67 |
* @param tweet The Tweet object representing the new state of this Tweet from |
|
68 |
* the perspective of the currently-logged in user. |
|
69 |
* @param error Error object describing the error that occurred. This will be either a |
|
70 |
* network error or an NSError with an errorCode corresponding to |
|
71 |
* TWTRAPIErrorCodeAlreadyFavorited or TWTRAPIErrorCodeAlreadyRetweeted |
|
72 |
* for an attempted action that has already been taken from the servers |
|
73 |
* point of view for this logged-in user. |
|
74 |
*/ |
|
75 |
typedef void (^TWTRTweetActionCompletion)(TWTRTweet *_Nullable tweet, NSError *_Nullable error); |
|
76 |
|
|
77 |
/** |
|
78 |
* Completion block called when a media upload request to the Twitter API succeeds or fails. |
|
79 |
* |
|
80 |
* @param mediaID The media ID of the object that was uploaded which can be used when tweeting. |
|
81 |
* @param error Error object describing the network error that occurred. |
|
82 |
*/ |
|
83 |
typedef void (^TWTRMediaUploadResponseCompletion)(NSString *_Nullable mediaID, NSError *_Nullable error); |
|
84 |
|
|
85 |
/** |
|
86 |
* Completion block called when the send Tweet request succeeds or fails. |
|
87 |
* |
|
88 |
* @param tweet The Twitter Tweet created. |
|
89 |
* @param error Error that will be set if the API request failed. |
|
90 |
*/ |
|
91 |
typedef void (^TWTRSendTweetCompletion)(TWTRTweet *_Nullable tweet, NSError *_Nullable error); |
|
92 |
|
|
93 |
/** |
|
94 |
* Completion block called when a request for the user's email succeeds or fails. |
|
95 |
* |
|
96 |
* @param email The email of the user |
|
97 |
* @param error Error object describing the error that occurred. |
|
98 |
*/ |
|
99 |
typedef void (^TWTRRequestEmailCompletion)(NSString *_Nullable email, NSError *_Nullable error); |
|
100 |
|
|
101 |
/** |
|
102 |
* Client for consuming the Twitter REST API. Provides methods for common API requests, as well as the ability to create and send custom requests. |
|
103 |
*/ |
|
104 |
@interface TWTRAPIClient : NSObject |
|
105 |
|
|
106 |
/** |
|
107 |
* The Twitter user ID this client is making API requests on behalf of or |
|
108 |
* nil if it is a guest user. |
|
109 |
*/ |
|
110 |
@property (nonatomic, copy, readonly, nullable) NSString *userID; |
|
111 |
|
|
112 |
/** |
|
113 |
* Constructs a `TWTRAPIClient` object to perform authenticated API requests with user authentication. |
|
114 |
* |
|
115 |
* @param userID (optional) ID of the user to make requests on behalf of. If the ID is nil requests will be made using guest authentication. |
|
116 |
* |
|
117 |
* @return Fully initialized API client to make authenticated requests against the Twitter REST API. |
|
118 |
*/ |
|
119 |
- (instancetype)initWithUserID:(nullable NSString *)userID; |
|
120 |
|
|
121 |
/** |
|
122 |
* Constructs a `TWTRAPIClient` with the last logged-in user. If no user has been |
|
123 |
* logged in yet this falls back to Guest authentication. |
|
124 |
* |
|
125 |
* @return Fully initialized API client to make Guest or User authenticated requests to the Twitter REST API. |
|
126 |
*/ |
|
127 |
+ (instancetype)clientWithCurrentUser; |
|
128 |
|
|
129 |
/** |
|
130 |
* @name Making Requests |
|
131 |
*/ |
|
132 |
|
|
133 |
/** |
|
134 |
* Returns a signed URL request. |
|
135 |
* |
|
136 |
* @param method Request method, GET, POST, PUT, DELETE, etc. |
|
137 |
* @param URLString Request URL. This is the full Twitter API URL. E.g. https://api.twitter.com/1.1/statuses/user_timeline.json |
|
138 |
* @param parameters Request parameters. |
|
139 |
* @param error Error that will be set if there was an error signing the request. |
|
140 |
* |
|
141 |
* @note If the request is not sent with the -[TWTRAPIClient sendTwitterRequest:completion:] method it is the developers responsibility to ensure that there is a valid guest session before this method is called. |
|
142 |
*/ |
|
143 |
- (NSURLRequest *)URLRequestWithMethod:(NSString *)method URL:(NSString *)URLString parameters:(nullable NSDictionary *)parameters error:(NSError **)error; |
|
144 |
|
|
145 |
/** |
|
146 |
* Sends a Twitter request. |
|
147 |
* |
|
148 |
* @param request The request that will be sent asynchronously. |
|
149 |
* @param completion Completion block to be called on response. Called on main queue. |
|
150 |
* @return an NSProgress object which can be used to cancel the request. |
|
151 |
*/ |
|
152 |
- (NSProgress *)sendTwitterRequest:(NSURLRequest *)request completion:(TWTRNetworkCompletion)completion; |
|
153 |
|
|
154 |
/** |
|
155 |
* @name Common API Actions |
|
156 |
*/ |
|
157 |
|
|
158 |
/** |
|
159 |
* Loads a Twitter User. |
|
160 |
* |
|
161 |
* @param userID (required) The Twitter user ID of the desired user. |
|
162 |
* @param completion Completion block to be called on response. Called on main queue. |
|
163 |
*/ |
|
164 |
- (void)loadUserWithID:(NSString *)userID completion:(TWTRLoadUserCompletion)completion; |
|
165 |
|
|
166 |
/** |
|
167 |
* Loads a single Tweet from the network or cache. |
|
168 |
* |
|
169 |
* @param tweetID (required) The ID of the desired Tweet. |
|
170 |
* @param completion Completion bock to be called on response. Called on main queue. |
|
171 |
*/ |
|
172 |
- (void)loadTweetWithID:(NSString *)tweetID completion:(TWTRLoadTweetCompletion)completion; |
|
173 |
|
|
174 |
/** |
|
175 |
* Loads a series of Tweets in a batch. The completion block will be passed an array of zero or more |
|
176 |
* Tweets that loaded successfully. If some Tweets fail to load the array will contain less Tweets than |
|
177 |
* number of requested IDs. If any Tweets fail to load, the IDs of the Tweets that did not load will |
|
178 |
* be provided in the userInfo dictionary property of the error parameter under `TWTRTweetsNotLoadedKey`. |
|
179 |
* |
|
180 |
* @param tweetIDStrings (required) An array of Tweet IDs. |
|
181 |
* @param completion Completion block to be called on response. Called on main queue. |
|
182 |
*/ |
|
183 |
- (void)loadTweetsWithIDs:(NSArray *)tweetIDStrings completion:(TWTRLoadTweetsCompletion)completion; |
|
184 |
|
|
185 |
/** |
|
186 |
* Uploads media to the media server. Returns a media ID to be used when tweeting. |
|
187 |
* |
|
188 |
* @param media The media to upload |
|
189 |
* @param contentType The HTTP content type of the media that you are uploading. |
|
190 |
* @param completion The completion handler to invoke. |
|
191 |
*/ |
|
192 |
- (void)uploadMedia:(NSData *)media contentType:(NSString *)contentType completion:(TWTRMediaUploadResponseCompletion)completion; |
|
193 |
|
|
194 |
/** |
|
195 |
* Create and send a Tweet. |
|
196 |
* |
|
197 |
* @param tweetText (required) The text for a Tweet |
|
198 |
* @param completion Completion block to be called on response. Called on main queue. |
|
199 |
*/ |
|
200 |
- (void)sendTweetWithText:(NSString *)tweetText completion:(TWTRSendTweetCompletion)completion; |
|
201 |
|
|
202 |
/** |
|
203 |
* Upload media and create a Tweet. Returns TWTRTweet to be used when debugging. |
|
204 |
* |
|
205 |
* @param tweetText The text for a Tweet |
|
206 |
* @param image UIImage to upload |
|
207 |
* @param completion The completion handler to invoke. |
|
208 |
*/ |
|
209 |
- (void)sendTweetWithText:(NSString *)tweetText image:(UIImage *)image completion:(TWTRSendTweetCompletion)completion; |
|
210 |
|
|
211 |
/** |
|
212 |
* Create a Tweet with a video. Returns TWTRTweet to be used when debugging. |
|
213 |
* |
|
214 |
* Note: there are several requirements of the video being uploaded: |
|
215 |
* - Duration should be between 0.5 seconds and 30 seconds |
|
216 |
* - File size should not exceed 15 mb |
|
217 |
* - Dimensions should be between 32x32 and 1280x1024 |
|
218 |
* - Aspect ratio should be between 1:3 and 3:1 |
|
219 |
* - Frame rate should be 40fps or less |
|
220 |
* |
|
221 |
* @param tweetText The text for a Tweet |
|
222 |
* @param videoData The video to be uploaded. Please follow guideline https://dev.twitter.com/rest/media/uploading-media |
|
223 |
* @param completion The completion handler to invoke. |
|
224 |
*/ |
|
225 |
- (void)sendTweetWithText:(NSString *)tweetText videoData:(NSData *)videoData completion:(TWTRSendTweetCompletion)completion; |
|
226 |
|
|
227 |
/** |
|
228 |
* Requests the email for the user id which the API client was instantiated with. |
|
229 |
* This method requires that you are using an API Client which was instantiated with |
|
230 |
* a logged in user otherwise you will receive a "Request failed: forbidden (403)" error. |
|
231 |
* |
|
232 |
* @param completion A completion block to invoke when the request completes. The email address may |
|
233 |
* be a nil if the user does not have an email address, the email address |
|
234 |
* is unverified or you do not have the correct permissions to request the email address. |
|
235 |
* |
|
236 |
* @note Requesting a user’s email address requires your application to be whitelisted by Twitter. |
|
237 |
* To request access, please visit https://support.twitter.com/forms/platform |
|
238 |
*/ |
|
239 |
- (void)requestEmailForCurrentUser:(TWTRRequestEmailCompletion)completion; |
|
240 |
|
|
241 |
@end |
|
242 |
|
|
243 |
NS_ASSUME_NONNULL_END |