Skip to content

Commit

Permalink
added uploadProgressBlock to keep track of media upload progres, rena…
Browse files Browse the repository at this point in the history
…med existring progressBlock into downloadProgressBlock, added st_ prefix to some categories
  • Loading branch information
nst committed Nov 18, 2013
1 parent 26a9fbd commit 0e15245
Show file tree
Hide file tree
Showing 15 changed files with 521 additions and 392 deletions.
3 changes: 2 additions & 1 deletion STTwitter/STHTTPRequest+STTwitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
@interface STHTTPRequest (STTwitter)

+ (STHTTPRequest *)twitterRequestWithURLString:(NSString *)urlString
stTwitterProgressBlock:(void(^)(id json))progressBlock
stTwitterUploadProgressBlock:(void(^)(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))uploadProgressBlock
stTwitterDownloadProgressBlock:(void(^)(id json))downloadProgressBlock
stTwitterSuccessBlock:(void(^)(NSDictionary *requestHeaders, NSDictionary *responseHeaders, id json))successBlock
stTwitterErrorBlock:(void(^)(NSDictionary *requestHeaders, NSDictionary *responseHeaders, NSError *error))errorBlock;

Expand Down
25 changes: 14 additions & 11 deletions STTwitter/STHTTPRequest+STTwitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ + (NSError *)errorFromResponseData:(NSData *)responseData {

NSError *jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError];

NSString *message = nil;
NSInteger code = 0;

if([json isKindOfClass:[NSDictionary class]]) {
// assume {"errors":[{"message":"Bad Authentication data","code":215}]}

id errors = [json valueForKey:@"errors"];
if([errors isKindOfClass:[NSArray class]] && [(NSArray *)errors count] > 0) {
NSDictionary *errorDictionary = [errors lastObject];
Expand All @@ -40,7 +40,7 @@ + (NSError *)errorFromResponseData:(NSData *)responseData {
message = errors;
}
}

if(message) {
NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:code userInfo:@{NSLocalizedDescriptionKey : message}];
return error;
Expand All @@ -50,26 +50,29 @@ + (NSError *)errorFromResponseData:(NSData *)responseData {
}

+ (STHTTPRequest *)twitterRequestWithURLString:(NSString *)urlString
stTwitterProgressBlock:(void(^)(id json))progressBlock
stTwitterUploadProgressBlock:(void(^)(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))uploadProgressBlock
stTwitterDownloadProgressBlock:(void(^)(id json))downloadProgressBlock
stTwitterSuccessBlock:(void(^)(NSDictionary *requestHeaders, NSDictionary *responseHeaders, id json))successBlock
stTwitterErrorBlock:(void(^)(NSDictionary *requestHeaders, NSDictionary *responseHeaders, NSError *error))errorBlock {

__block STHTTPRequest *r = [self requestWithURLString:urlString];
__weak STHTTPRequest *wr = r;

r.ignoreSharedCookiesStorage = YES;

r.timeoutSeconds = 0;

r.uploadProgressBlock = uploadProgressBlock;

r.downloadProgressBlock = ^(NSData *data, NSInteger totalBytesReceived, NSInteger totalBytesExpectedToReceive) {

if(progressBlock == nil) return;
if(downloadProgressBlock == nil) return;

NSError *jsonError = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];

if(json) {
progressBlock(json);
downloadProgressBlock(json);
return;
}

Expand All @@ -87,10 +90,10 @@ + (STHTTPRequest *)twitterRequestWithURLString:(NSString *)urlString
NSError *jsonError = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
if(json == nil) {
// errorBlock(wr.responseHeaders, jsonError);
// errorBlock(wr.responseHeaders, jsonError);
return; // not enough information to say it's an error
}
progressBlock(json);
downloadProgressBlock(json);
}
};

Expand Down Expand Up @@ -132,8 +135,8 @@ + (STHTTPRequest *)twitterRequestWithURLString:(NSString *)urlString

STLog(@"-- body: %@", wr.responseString);

// BOOL isCancellationError = [[error domain] isEqualToString:@"STHTTPRequest"] && ([error code] == kSTHTTPRequestCancellationError);
// if(isCancellationError) return;
// BOOL isCancellationError = [[error domain] isEqualToString:@"STHTTPRequest"] && ([error code] == kSTHTTPRequestCancellationError);
// if(isCancellationError) return;

errorBlock(wr.requestHeaders, wr.responseHeaders, error);
};
Expand Down
100 changes: 52 additions & 48 deletions STTwitter/STTwitterAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,28 @@ NS_ENUM(NSUInteger, STTwitterAPIErrorCode) {
#pragma mark Generic methods to GET and POST

- (id)fetchResource:(NSString *)resource
HTTPMethod:(NSString *)HTTPMethod
baseURLString:(NSString *)baseURLString
parameters:(NSDictionary *)params
progressBlock:(void (^)(id request, id response))progressBlock // TODO: handle progressBlock?
successBlock:(void (^)(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, id response))successBlock
errorBlock:(void (^)(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, NSError *error))errorBlock;
HTTPMethod:(NSString *)HTTPMethod
baseURLString:(NSString *)baseURLString
parameters:(NSDictionary *)params
uploadProgressBlock:(void(^)(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))uploadProgressBlock
downloadProgressBlock:(void (^)(id request, id response))downloadProgressBlock
successBlock:(void (^)(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, id response))successBlock
errorBlock:(void (^)(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, NSError *error))errorBlock;

- (id)getResource:(NSString *)resource
baseURLString:(NSString *)baseURLString
parameters:(NSDictionary *)parameters
progressBlock:(void(^)(id json))progressBlock
successBlock:(void(^)(NSDictionary *rateLimits, id json))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;
baseURLString:(NSString *)baseURLString
parameters:(NSDictionary *)parameters
downloadProgressBlock:(void(^)(id json))progredownloadProgressBlockssBlock
successBlock:(void(^)(NSDictionary *rateLimits, id json))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;

- (id)postResource:(NSString *)resource
baseURLString:(NSString *)baseURLString
parameters:(NSDictionary *)parameters
progressBlock:(void(^)(id json))progressBlock
successBlock:(void(^)(NSDictionary *rateLimits, id response))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;
baseURLString:(NSString *)baseURLString
parameters:(NSDictionary *)parameters
uploadProgressBlock:(void(^)(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))uploadProgressBlock
downloadProgressBlock:(void(^)(id json))downloadProgressBlock
successBlock:(void(^)(NSDictionary *rateLimits, id response))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;

#pragma mark Timelines

Expand Down Expand Up @@ -372,6 +374,7 @@ NS_ENUM(NSUInteger, STTwitterAPIErrorCode) {
longitude:(NSString *)longitude
placeID:(NSString *)placeID
displayCoordinates:(NSNumber *)displayCoordinates
uploadProgressBlock:(void(^)(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))uploadProgressBlock
successBlock:(void(^)(NSDictionary *status))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;

Expand All @@ -382,6 +385,7 @@ NS_ENUM(NSUInteger, STTwitterAPIErrorCode) {
placeID:(NSString *)placeID
latitude:(NSString *)latitude
longitude:(NSString *)longitude
uploadProgressBlock:(void(^)(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))uploadProgressBlock
successBlock:(void(^)(NSDictionary *status))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;

Expand Down Expand Up @@ -455,18 +459,18 @@ NS_ENUM(NSUInteger, STTwitterAPIErrorCode) {
*/

- (id)postStatusesFilterUserIDs:(NSArray *)userIDs
keywordsToTrack:(NSArray *)keywordsToTrack
locationBoundingBoxes:(NSArray *)locationBoundingBoxes
delimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;
keywordsToTrack:(NSArray *)keywordsToTrack
locationBoundingBoxes:(NSArray *)locationBoundingBoxes
delimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;

// convenience
- (id)postStatusesFilterKeyword:(NSString *)keyword
progressBlock:(void(^)(id response))progressBlock
errorBlock:(void(^)(NSError *error))errorBlock;
progressBlock:(void(^)(id response))progressBlock
errorBlock:(void(^)(NSError *error))errorBlock;

/*
GET statuses/sample
Expand All @@ -475,10 +479,10 @@ NS_ENUM(NSUInteger, STTwitterAPIErrorCode) {
*/

- (id)getStatusesSampleDelimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;
stallWarnings:(NSNumber *)stallWarnings
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;

/*
GET statuses/firehose
Expand All @@ -489,11 +493,11 @@ NS_ENUM(NSUInteger, STTwitterAPIErrorCode) {
*/

- (id)getStatusesFirehoseWithCount:(NSString *)count
delimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;
delimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;

/*
GET user
Expand All @@ -502,14 +506,14 @@ NS_ENUM(NSUInteger, STTwitterAPIErrorCode) {
*/

- (id)getUserStreamDelimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
stallWarnings:(NSNumber *)stallWarnings
includeMessagesFromFollowedAccounts:(NSNumber *)includeMessagesFromFollowedAccounts
includeReplies:(NSNumber *)includeReplies
keywordsToTrack:(NSArray *)keywordsToTrack
locationBoundingBoxes:(NSArray *)locationBoundingBoxes
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;
includeReplies:(NSNumber *)includeReplies
keywordsToTrack:(NSArray *)keywordsToTrack
locationBoundingBoxes:(NSArray *)locationBoundingBoxes
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;

/*
GET site
Expand All @@ -518,13 +522,13 @@ includeMessagesFromFollowedAccounts:(NSNumber *)includeMessagesFromFollowedAccou
*/

- (id)getSiteStreamForUserIDs:(NSArray *)userIDs
delimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
restrictToUserMessages:(NSNumber *)restrictToUserMessages
includeReplies:(NSNumber *)includeReplies
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;
delimited:(NSNumber *)delimited
stallWarnings:(NSNumber *)stallWarnings
restrictToUserMessages:(NSNumber *)restrictToUserMessages
includeReplies:(NSNumber *)includeReplies
progressBlock:(void(^)(id response))progressBlock
stallWarningBlock:(void(^)(NSString *code, NSString *message, NSUInteger percentFull))stallWarningBlock
errorBlock:(void(^)(NSError *error))errorBlock;

#pragma mark Direct Messages

Expand Down
Loading

0 comments on commit 0e15245

Please sign in to comment.