-
Notifications
You must be signed in to change notification settings - Fork 0
/
RESTModel.m
126 lines (105 loc) · 3.65 KB
/
RESTModel.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// RESTModel.m
// storiesiOS
//
#import "RESTModel.h"
#import "NSDictionary+UrlEncoding.h"
@implementation RESTModel
+(NSString *) postPath:(NSString *)path withParams:(NSMutableDictionary *)params isSynchronous:(BOOL)isSynch {
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:path]];
for (id key in [params allKeys]) {
[request addPostValue:[params objectForKey:key] forKey:key];
}
if (isSynch) {
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
return response;
}
else {
//TODO: INTERPRET ERRORS
return [NSString stringWithFormat:@"%@", [error description]];
}
}
else {
[request setDelegate:self];
[request startAsynchronous];
return @"Asynch";
}
}
+(NSString *) getPath:(NSString *)path withParams:(NSMutableDictionary *)params isSynchronous:(BOOL)isSynch error:(NSError*) error{
NSString *urlString = [NSString stringWithFormat:@"%@?%@", path, [params urlEncodedString]];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTimeOutSeconds:30];
if (isSynch) {
[request startSynchronous];
error = [request error];
if (!error) {
NSString *response = [request responseString];
return response;
}
else {
//TODO: INTERPRET ERRORS
if ([request responseStatusCode] >= 400) {
return [NSString stringWithFormat:@"{\"error\":\"%d\"}", [request responseStatusCode]];
} else {
return [NSString stringWithFormat:@"%@", [error description]];
}
}
}
else {
[request setDelegate:self];
[request startAsynchronous];
return @"Asynch";
}
}
+(NSString *) deletePath:(NSString *)path withParams:(NSMutableDictionary *)params isSynchronous:(BOOL)isSynch {
NSString *urlString = [NSString stringWithFormat:@"%@?%@", path, [params urlEncodedString]];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"DELETE"];
if (isSynch) {
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
return response;
}
else {
//TODO: INTERPRET ERRORS
return [NSString stringWithFormat:@"%@", [error description]];
}
}
else {
[request setDelegate:self];
[request startAsynchronous];
return @"Asynch";
}
}
-(NSString *) putPath:(NSString *)path withParams:(NSMutableDictionary *)params isSynchronous:(BOOL)isSynch {
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:path]];
for (id key in [params allKeys]) {
[request addPostValue:[params objectForKey:key] forKey:key];
}
[request setRequestMethod:@"PUT"];
if (isSynch) {
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
return response;
}
else {
//TODO: INTERPRET ERRORS
return [NSString stringWithFormat:@"%@", [error description]];
}
}
else {
[request setDelegate:self];
[request startAsynchronous];
return @"Asynch";
}
}
@end