mirror of
https://github.com/samsonjs/Marshmallows.git
synced 2026-04-27 15:07:42 +00:00
use ARC, remove retain/release/autorelease
This commit is contained in:
parent
0fe10f0abb
commit
348909fc04
3 changed files with 33 additions and 40 deletions
|
|
@ -9,7 +9,6 @@
|
||||||
#import "MMHTTPClient.h"
|
#import "MMHTTPClient.h"
|
||||||
#import "NSString+marshmallows.h"
|
#import "NSString+marshmallows.h"
|
||||||
|
|
||||||
MMHTTPClient *_client;
|
|
||||||
// Encode a string to embed in an URL.
|
// Encode a string to embed in an URL.
|
||||||
NSString* MMHTTPURLEncode(NSString *string) {
|
NSString* MMHTTPURLEncode(NSString *string) {
|
||||||
return (__bridge NSString *)
|
return (__bridge NSString *)
|
||||||
|
|
@ -20,6 +19,7 @@ NSString* MMHTTPURLEncode(NSString *string) {
|
||||||
kCFStringEncodingUTF8);
|
kCFStringEncodingUTF8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MMHTTPClient *_sharedMMHTTPClient;
|
||||||
|
|
||||||
NSString *JoinURLComponents(NSString *first, va_list args)
|
NSString *JoinURLComponents(NSString *first, va_list args)
|
||||||
{
|
{
|
||||||
|
|
@ -39,25 +39,25 @@ NSString *JoinURLComponents(NSString *first, va_list args)
|
||||||
|
|
||||||
+ (MMHTTPClient *) sharedClient
|
+ (MMHTTPClient *) sharedClient
|
||||||
{
|
{
|
||||||
if (!_client) {
|
if (!_sharedMMHTTPClient) {
|
||||||
_client = [[self alloc] init];
|
_sharedMMHTTPClient = [[self alloc] init];
|
||||||
}
|
}
|
||||||
return _client;
|
return _sharedMMHTTPClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (id) client
|
+ (id) client
|
||||||
{
|
{
|
||||||
return [[[self alloc] init] autorelease];
|
return [[self alloc] init];
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (id) clientWithBaseURL: (NSString *)baseURL
|
+ (id) clientWithBaseURL: (NSString *)baseURL
|
||||||
{
|
{
|
||||||
return [[[self alloc] initWithBaseURL: baseURL] autorelease];
|
return [[self alloc] initWithBaseURL: baseURL];
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (id) clientWithBaseURL: (NSString *)baseURL timeout: (NSUInteger)timeout
|
+ (id) clientWithBaseURL: (NSString *)baseURL timeout: (NSUInteger)timeout
|
||||||
{
|
{
|
||||||
return [[[self alloc] initWithBaseURL: baseURL timeout: timeout] autorelease];
|
return [[self alloc] initWithBaseURL: baseURL timeout: timeout];
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (NSString *) pathFor: (NSString *)first, ...
|
+ (NSString *) pathFor: (NSString *)first, ...
|
||||||
|
|
@ -173,7 +173,11 @@ NSString *JoinURLComponents(NSString *first, va_list args)
|
||||||
|
|
||||||
- (MMHTTPRequest *) getImage: (NSString *)url then: (MMHTTPImageCallback)callback
|
- (MMHTTPRequest *) getImage: (NSString *)url then: (MMHTTPImageCallback)callback
|
||||||
{
|
{
|
||||||
return [self request: [NSDictionary dictionary] then: (MMHTTPCallback)callback];
|
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||||
|
url, @"url",
|
||||||
|
@"image", @"type",
|
||||||
|
nil];
|
||||||
|
return [self request: options then: (MMHTTPCallback)callback];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MMHTTPRequest *) getText: (NSString *)url then: (MMHTTPTextCallback)callback
|
- (MMHTTPRequest *) getText: (NSString *)url then: (MMHTTPTextCallback)callback
|
||||||
|
|
@ -203,7 +207,6 @@ NSString *JoinURLComponents(NSString *first, va_list args)
|
||||||
return [self request: options then: callback];
|
return [self request: options then: callback];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MMHTTPRequest *) post: (NSString *)url fields: (NSDictionary *)fields then: (MMHTTPCallback)callback
|
|
||||||
- (NSString *) encodeFields: (NSDictionary *)fields withPrefix: (NSString *)prefix
|
- (NSString *) encodeFields: (NSDictionary *)fields withPrefix: (NSString *)prefix
|
||||||
{
|
{
|
||||||
NSString *suffix = @"";
|
NSString *suffix = @"";
|
||||||
|
|
@ -283,10 +286,4 @@ NSString *JoinURLComponents(NSString *first, va_list args)
|
||||||
return [MMHTTPRequest requestWithOptions: options callback: callback];
|
return [MMHTTPRequest requestWithOptions: options callback: callback];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) dealloc
|
|
||||||
{
|
|
||||||
[_baseURL release];
|
|
||||||
[super dealloc];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,10 @@ typedef void (^MMHTTPImageCallback)(NSInteger status, UIImage *image);
|
||||||
@property (nonatomic, retain) NSMutableDictionary *headers;
|
@property (nonatomic, retain) NSMutableDictionary *headers;
|
||||||
@property (nonatomic, retain) NSData *data;
|
@property (nonatomic, retain) NSData *data;
|
||||||
@property (nonatomic, retain) NSString *type;
|
@property (nonatomic, retain) NSString *type;
|
||||||
@property (nonatomic, retain) MMHTTPCallback callback;
|
@property (nonatomic, copy) MMHTTPCallback callback;
|
||||||
@property NSUInteger timeout;
|
@property NSUInteger timeout;
|
||||||
@property (readonly) NSInteger statusCode;
|
@property (readonly) NSInteger statusCode;
|
||||||
@property (readonly) NSDictionary *responseHeaders;
|
@property (strong, readonly) NSDictionary *responseHeaders;
|
||||||
@property (readonly) NSData *responseData;
|
@property (readonly) NSData *responseData;
|
||||||
@property (readonly) NSString *responseText;
|
@property (readonly) NSString *responseText;
|
||||||
@property (readonly) UIImage *responseImage;
|
@property (readonly) UIImage *responseImage;
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
+ (id) requestWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback
|
+ (id) requestWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback
|
||||||
{
|
{
|
||||||
return [[[self alloc] initWithOptions: options callback: callback] autorelease];
|
return [[self alloc] initWithOptions: options callback: callback];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) initWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback
|
- (id) initWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback
|
||||||
|
|
@ -52,6 +52,10 @@
|
||||||
- (void) cancel
|
- (void) cancel
|
||||||
{
|
{
|
||||||
[_connection cancel];
|
[_connection cancel];
|
||||||
|
if (self.callback) {
|
||||||
|
Block_release(self.callback);
|
||||||
|
self.callback = nil;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSData *) responseData
|
- (NSData *) responseData
|
||||||
|
|
@ -61,9 +65,9 @@
|
||||||
|
|
||||||
- (NSString *) responseText
|
- (NSString *) responseText
|
||||||
{
|
{
|
||||||
return [[[NSString alloc] initWithBytes: _responseData.bytes
|
return [[NSString alloc] initWithBytes: _responseData.bytes
|
||||||
length: _responseData.length
|
length: _responseData.length
|
||||||
encoding: NSUTF8StringEncoding] autorelease];
|
encoding: NSUTF8StringEncoding];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (UIImage *) responseImage
|
- (UIImage *) responseImage
|
||||||
|
|
@ -96,10 +100,11 @@
|
||||||
|
|
||||||
- (void) connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
|
- (void) connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
|
||||||
{
|
{
|
||||||
|
// NSLog(@"didReceiveResponse: %@",response);
|
||||||
if ([response respondsToSelector: @selector(statusCode)])
|
if ([response respondsToSelector: @selector(statusCode)])
|
||||||
{
|
{
|
||||||
_statusCode = [(NSHTTPURLResponse *)response statusCode];
|
_statusCode = [(NSHTTPURLResponse *)response statusCode];
|
||||||
_responseHeaders = [[(NSHTTPURLResponse *)response allHeaderFields] retain];
|
_responseHeaders = [(NSHTTPURLResponse *)response allHeaderFields];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
NSLog(@"Not an HTTP response? connection: %@ response: %@", connection, response);
|
NSLog(@"Not an HTTP response? connection: %@ response: %@", connection, response);
|
||||||
|
|
@ -112,19 +117,23 @@
|
||||||
|
|
||||||
- (void) connection: (NSURLConnection *)connection didReceiveData: (NSData *)data
|
- (void) connection: (NSURLConnection *)connection didReceiveData: (NSData *)data
|
||||||
{
|
{
|
||||||
|
// NSLog(@"didReceiveData: %@", data);
|
||||||
[_responseData appendData: data];
|
[_responseData appendData: data];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) connection: (NSURLConnection *)connection didFailWithError: (NSError *)error
|
- (void) connection: (NSURLConnection *)connection didFailWithError: (NSError *)error
|
||||||
{
|
{
|
||||||
[_responseData release];
|
NSLog(@"didFailWithError: %@", error);
|
||||||
_responseData = nil;
|
_responseData = nil;
|
||||||
_statusCode = MMHTTPRequestStatusError;
|
_statusCode = MMHTTPRequestStatusError;
|
||||||
self.callback(self.statusCode, error);
|
if (self.callback) {
|
||||||
|
self.callback(self.statusCode, error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) connectionDidFinishLoading: (NSURLConnection *)connection
|
- (void) connectionDidFinishLoading: (NSURLConnection *)connection
|
||||||
{
|
{
|
||||||
|
// NSLog(@"didFinishLoading: %d", self.statusCode);
|
||||||
id data = nil;
|
id data = nil;
|
||||||
if (self.statusCode == 200) {
|
if (self.statusCode == 200) {
|
||||||
if ([self.type isEqualToString: @"text"]) {
|
if ([self.type isEqualToString: @"text"]) {
|
||||||
|
|
@ -137,23 +146,10 @@
|
||||||
data = self.responseData;
|
data = self.responseData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.callback(self.statusCode, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) dealloc
|
if (self.callback) {
|
||||||
{
|
self.callback(self.statusCode, data);
|
||||||
[_connection release];
|
}
|
||||||
[_request release];
|
|
||||||
[_method release];
|
|
||||||
[_url release];
|
|
||||||
[_headers release];
|
|
||||||
[_data release];
|
|
||||||
[_type release];
|
|
||||||
[_callback release];
|
|
||||||
[_responseHeaders release];
|
|
||||||
[_responseData release];
|
|
||||||
[super dealloc];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue