Skip to content

Commit 8f9a4f7

Browse files
committed
Added STIgnore protocol to easily ignore property in request.
1 parent b322c34 commit 8f9a4f7

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] init
5757
@property (nonatomic, strong) NSString *body;
5858
@property (nonatomic, strong) NSDate *date;
5959
@property (nonatomic, assign) int userId;
60+
@property (nonatomic, strong) NSString<STIgnore> *ignored; // This property is ignored when packing the request.
6061
@property (nonatomic, strong, readonly) NSDictionary *post;
6162
6263
@end
@@ -129,8 +130,29 @@ testPostTask.title = @"Test Post Net Task Title";
129130
testPostTask.body = @"Test Post Net Task Body";
130131
testPostTask.userId = 1;
131132
testPostTask.date = [NSDate new];
133+
testPostTask.ignored = @"test";
132134
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:testPostTask.uri];
133135
[[STNetTaskQueue sharedQueue] addTask:testPostTask];
136+
137+
// The net task will be sent as described below.
138+
/*
139+
URI: posts
140+
Method: POST
141+
Request Type: Key-Value String
142+
Response Type: JSON
143+
Custom Headers:
144+
{
145+
"custom_header" = value;
146+
}
147+
Parameters:
148+
{
149+
body = "Test Post Net Task Body";
150+
date = "1452239110.829915";
151+
"other_parameter" = value;
152+
title = "Test Post Net Task Title";
153+
"user_id" = 1;
154+
}
155+
*/
134156
```
135157

136158
#### Use subscription block

STNetTaskQueue/STHTTPNetTask.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ typedef NS_ENUM(NSUInteger, STHTTPNetTaskResponseType) {
4141
STHTTPNetTaskResponseRawData
4242
};
4343

44+
/**
45+
STIgnore marks a property as "ignore" so that the property will be ignored when packing the request.
46+
STHTTPNetTaskRequestObject-ignoredProperties will do the same.
47+
48+
@see STHTTPNetTaskRequestObject
49+
*/
50+
@protocol STIgnore
51+
52+
@end
53+
54+
/**
55+
To avoid complier warnings
56+
*/
57+
@interface NSObject (STHTTPNetTaskRequestObject) <STIgnore>
58+
59+
@end
60+
4461
/**
4562
If a class conforms to this protocol, it means the instance of this class will be converted to a dictionary and passed as parameter in a HTTP request.
4663
*/

STNetTaskQueue/STHTTPNetTaskParametersPacker.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ - (NSString *)separatorFromRequestObject:(id<STHTTPNetTaskRequestObject>)request
158158

159159
- (BOOL)shouldPackPropertyWithAttributes:(NSArray *)attributes
160160
{
161-
// Only pack non-readonly property
162-
return ![attributes containsObject:@"R"];
161+
// Only pack non-readonly property and property which is not ignored.
162+
return ![attributes containsObject:@"R"] && [attributes[0] rangeOfString:NSStringFromProtocol(@protocol(STIgnore))].location == NSNotFound;
163163
}
164164

165165
@end

STNetTaskQueueTest/STNetTaskQueueTest/STNetTaskQueueTestHTTP.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ - (void)testPostNetTask
9999
testPostTask.body = @"Test Post Net Task Body";
100100
testPostTask.userId = 1;
101101
testPostTask.date = [NSDate new];
102+
testPostTask.ignored = @"test";
102103
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:testPostTask.uri];
103104
[[STNetTaskQueue sharedQueue] addTask:testPostTask];
104105

@@ -273,7 +274,8 @@ - (void)netTaskDidEnd:(STNetTask *)task
273274
![testPostTask.post[@"title"] isEqualToString:testPostTask.title] ||
274275
![testPostTask.post[@"body"] isEqualToString:testPostTask.body] ||
275276
[testPostTask.post[@"user_id"] intValue] != testPostTask.userId ||
276-
[testPostTask.post[@"date"] doubleValue] != testPostTask.date.timeIntervalSince1970) {
277+
[testPostTask.post[@"date"] doubleValue] != testPostTask.date.timeIntervalSince1970 ||
278+
testPostTask.post[@"ignored"] != nil) {
277279
XCTFail(@"%@ failed", _expectation.description);
278280
}
279281
}

STNetTaskQueueTest/STNetTaskQueueTest/STTestPostNetTask.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@property (nonatomic, strong) NSString *body;
1515
@property (nonatomic, strong) NSDate *date;
1616
@property (nonatomic, assign) int userId;
17+
@property (nonatomic, strong) NSString<STIgnore> *ignored; // This property is ignored when packing the request.
1718
@property (nonatomic, strong, readonly) NSDictionary *post;
1819

1920
@end

0 commit comments

Comments
 (0)