This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Pull out NISimpleRequest into the core.
- Loading branch information
Showing
6 changed files
with
227 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// Copyright 2011 Jeff Verkoeyen | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@protocol NISimpleRequestDelegate; | ||
|
||
/** | ||
* A simple network request object. | ||
* | ||
* This object is designed to be simple and light-weight. It does not cache requests, handle | ||
* redirecting, and many other common features found in more powerful networking objects. | ||
*/ | ||
@interface NISimpleRequest : NSObject <NSURLConnectionDataDelegate> { | ||
@private | ||
NSURLConnection* _connection; | ||
NSMutableData* _data; | ||
NSURL* _url; | ||
id<NISimpleRequestDelegate> _delegate; | ||
} | ||
|
||
+ (id)requestWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval; | ||
- (id)initWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval; | ||
- (void)send; | ||
- (void)cancel; | ||
|
||
@property (nonatomic, readwrite, retain) NSURL* url; | ||
@property (nonatomic, readwrite, assign) id<NISimpleRequestDelegate> delegate; | ||
@end | ||
|
||
/** | ||
* The delegate for NISimpleRequest. | ||
*/ | ||
@protocol NISimpleRequestDelegate <NSObject> | ||
@optional | ||
|
||
/** | ||
* The request failed with a given error. | ||
*/ | ||
- (void)requestDidFail:(NISimpleRequest *)request withError:(NSError *)error; | ||
|
||
/** | ||
* The request finished successfully. | ||
*/ | ||
- (void)requestDidFinish:(NISimpleRequest *)request withData:(NSData *)data; | ||
|
||
@end | ||
|
||
/** | ||
* Returns an autoreleased request initialized with the given URL and timeout. | ||
* | ||
* @fn NISimpleRequest::requestWithURL:timeoutInterval: | ||
*/ | ||
|
||
/** | ||
* Initializes a newly allocated request with a given url and timeout. | ||
* | ||
* @fn NISimpleRequest::initWithURL:timeoutInterval: | ||
*/ | ||
|
||
/** | ||
* Initiates the NSURLConnection. | ||
* | ||
* @fn NISimpleRequest::send | ||
*/ | ||
|
||
/** | ||
* Cancels the NSURLConnection. | ||
* | ||
* @fn NISimpleRequest::cancel | ||
*/ | ||
|
||
/** | ||
* The URL this request is fetching. | ||
* | ||
* @fn NISimpleRequest::url | ||
*/ | ||
|
||
/** | ||
* This request's delegate. | ||
* | ||
* @fn NISimpleRequest::delegate | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// Copyright 2011 Jeff Verkoeyen | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import "NISimpleRequest.h" | ||
|
||
#import "NIPreprocessorMacros.h" | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
@implementation NISimpleRequest | ||
|
||
@synthesize url = _url; | ||
@synthesize delegate = _delegate; | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (void)dealloc { | ||
[_connection cancel]; | ||
NI_RELEASE_SAFELY(_connection); | ||
NI_RELEASE_SAFELY(_data); | ||
NI_RELEASE_SAFELY(_url); | ||
|
||
[super dealloc]; | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
+ (id)requestWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval { | ||
return [[[self alloc] initWithURL:url timeoutInterval:timeoutInterval] autorelease]; | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (id)initWithURL:(NSURL *)url timeoutInterval:(NSTimeInterval)timeoutInterval { | ||
if ((self = [super init])) { | ||
_url = [url retain]; | ||
|
||
NSURLRequest* request = [NSURLRequest requestWithURL: url | ||
cachePolicy: NSURLRequestUseProtocolCachePolicy | ||
timeoutInterval: timeoutInterval]; | ||
|
||
_connection = [[NSURLConnection alloc] initWithRequest:request | ||
delegate:self | ||
startImmediately:NO]; | ||
|
||
_data = [[NSMutableData alloc] init]; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (void)send { | ||
[_connection start]; | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (void)cancel { | ||
[_connection cancel]; | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
#pragma mark - NSURLConnectionDataDelegate | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { | ||
[_data setLength:0]; | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { | ||
[_data appendData:data]; | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | ||
if ([self.delegate respondsToSelector:@selector(requestDidFail:withError:)]) { | ||
[self.delegate requestDidFail:self withError:error]; | ||
} | ||
NI_RELEASE_SAFELY(_data); | ||
NI_RELEASE_SAFELY(_connection); | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////////////////////////////////////////////////////// | ||
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { | ||
if ([self.delegate respondsToSelector:@selector(requestDidFinish:withData:)]) { | ||
[self.delegate requestDidFinish:self withData:_data]; | ||
} | ||
NI_RELEASE_SAFELY(_data); | ||
NI_RELEASE_SAFELY(_connection); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.