-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTRestRequest.m
102 lines (86 loc) · 3 KB
/
OTRestRequest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// OTRestRequest.m
// OTRestFramework
//
// Created by Jeremy Ellison on 7/27/09.
// Copyright 2009 Two Toasters. All rights reserved.
//
#import "OTRestRequest.h"
#import "OTRestResponse.h"
#import "NSDictionary+OTRestRequestSerialization.h"
#import "OTRestNotifications.h"
@implementation OTRestRequest
@synthesize URL = _URL, URLRequest = _URLRequest, delegate = _delegate, callback = _callback, additionalHTTPHeaders = _additionalHTTPHeaders,
params = _params, userData = _userData, username = _username, password = _password;
+ (OTRestRequest*)requestWithURL:(NSURL*)URL delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:URL delegate:delegate callback:callback];
[request autorelease];
return request;
}
- (id)initWithURL:(NSURL*)URL delegate:(id)delegate callback:(SEL)callback {
if (self = [self init]) {
_URL = [URL retain];
_URLRequest = [[NSMutableURLRequest alloc] initWithURL:_URL];
_delegate = [delegate retain];
_callback = callback;
}
return self;
}
- (void)dealloc {
[_URL release];
[_URLRequest release];
[_delegate release];
[_params release];
[_additionalHTTPHeaders release];
[_username release];
[_password release];
[super dealloc];
}
- (NSString*)HTTPMethod {
return [_URLRequest HTTPMethod];
}
- (void)addHeadersToRequest {
NSString* header;
for (header in _additionalHTTPHeaders) {
[_URLRequest setValue:[_additionalHTTPHeaders valueForKey:header] forHTTPHeaderField:header];
}
if (_params != nil) {
[_URLRequest setValue:[_params ContentTypeHTTPHeader] forHTTPHeaderField:@"Content-Type"];
}
NSLog(@"Headers: %@", [_URLRequest allHTTPHeaderFields]);
}
- (void)send {
NSString* body = [[NSString alloc] initWithData:[_URLRequest HTTPBody] encoding:NSUTF8StringEncoding];
NSLog(@"Sending %@ request to URL %@. HTTP Body: %@", [self HTTPMethod], [[self URL] absoluteString], body);
[body release];
NSDate* sentAt = [NSDate date];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod", [self URL], @"URL", sentAt, @"sentAt", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kOTRestRequestSentNotification object:self userInfo:userInfo];
OTRestResponse* response = [[[OTRestResponse alloc] initWithRestRequest:self] autorelease];
[[NSURLConnection connectionWithRequest:_URLRequest delegate:response] retain];
}
- (void)get {
[_URLRequest setHTTPMethod:@"GET"];
[self addHeadersToRequest];
[self send];
}
- (void)postParams:(NSObject<OTRestRequestSerializable>*)params {
[_URLRequest setHTTPMethod:@"POST"];
_params = [params retain];
[_URLRequest setHTTPBody:[_params HTTPBody]];
[self addHeadersToRequest];
[self send];
}
- (void)putParams:(NSObject<OTRestRequestSerializable>*)params {
[_URLRequest setHTTPMethod:@"PUT"];
_params = [params retain];
[_URLRequest setHTTPBody:[_params HTTPBody]];
[self addHeadersToRequest];
[self send];
}
- (void)delete {
[_URLRequest setHTTPMethod:@"DELETE"];
[self addHeadersToRequest];
[self send];
}
@end