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