-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSTHTTPNetTask.m
More file actions
139 lines (112 loc) · 3.67 KB
/
STHTTPNetTask.m
File metadata and controls
139 lines (112 loc) · 3.67 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// STHTTPNetTask.m
// STNetTaskQueue
//
// Created by Kevin Lin on 29/11/14.
// Copyright (c) 2014 Sth4Me. All rights reserved.
//
#import "STHTTPNetTask.h"
#import "STHTTPNetTaskParametersPacker.h"
NSString *const STHTTPNetTaskServerError = @"STHTTPNetTaskServerError";
NSString *const STHTTPNetTaskResponseParsedError = @"STHTTPNetTaskResponseParsedError";
NSString *const STHTTPNetTaskErrorStatusCodeUserInfoKey = @"statusCode";
NSString *const STHTTPNetTaskErrorResponseDataUserInfoKey = @"responseData";
NSString *STHTTPNetTaskRequestObjectDefaultSeparator = @"_";
@interface STHTTPNetTask ()
@property (atomic, assign) NSInteger statusCode;
@property (atomic, strong) NSDictionary *responseHeaders;
@end
@implementation STHTTPNetTask
- (STHTTPNetTaskMethod)method
{
return STHTTPNetTaskGet;
}
- (STHTTPNetTaskRequestType)requestType
{
return STHTTPNetTaskRequestKeyValueString;
}
- (STHTTPNetTaskResponseType)responseType
{
return STHTTPNetTaskResponseJSON;
}
- (NSDictionary *)headers
{
return nil;
}
- (NSDictionary *)parameters
{
return nil;
}
- (NSDictionary *)datas
{
return nil;
}
- (void)didResponse:(id)response
{
if ([response isKindOfClass:[NSDictionary class]]) {
[self didResponseDictionary:response];
}
else if ([response isKindOfClass:[NSArray class]]) {
[self didResponseArray:response];
}
else if ([response isKindOfClass:[NSString class]]) {
[self didResponseString:response];
}
else if ([response isKindOfClass:[NSData class]]) {
[self didResponseData:response];
}
else {
NSAssert(NO, @"Invalid response");
}
}
- (void)didResponseDictionary:(NSDictionary *)dictionary
{
}
- (void)didResponseArray:(NSArray *)array
{
}
- (void)didResponseString:(NSString *)string
{
}
- (void)didResponseData:(NSData *)data
{
}
- (NSArray *)ignoredProperties
{
return nil;
}
- (NSString *)description
{
NSDictionary *methodMap = @{ @(STHTTPNetTaskGet): @"GET",
@(STHTTPNetTaskDelete): @"DELETE",
@(STHTTPNetTaskHead): @"HEAD",
@(STHTTPNetTaskPatch): @"PATCH",
@(STHTTPNetTaskPost): @"POST",
@(STHTTPNetTaskPut): @"PUT" };
NSDictionary *requestTypeMap = @{ @(STHTTPNetTaskRequestJSON): @"JSON",
@(STHTTPNetTaskRequestKeyValueString): @"Key-Value String",
@(STHTTPNetTaskRequestFormData): @"Form Data" };
NSDictionary *responseTypeMap = @{ @(STHTTPNetTaskResponseJSON): @"JSON",
@(STHTTPNetTaskResponseString): @"String",
@(STHTTPNetTaskResponseRawData): @"Raw Data" };
NSMutableString *desc = [NSMutableString new];
[desc appendFormat:@"URI: %@\n", self.uri];
[desc appendFormat:@"Method: %@\n", methodMap[@(self.method)]];
[desc appendFormat:@"Request Type: %@\n", requestTypeMap[@(self.requestType)]];
[desc appendFormat:@"Response Type: %@\n", responseTypeMap[@(self.responseType)]];
NSDictionary *headers = self.headers;
if (headers.count) {
[desc appendFormat:@"Custom Headers:\n%@\n", headers];
}
NSDictionary *datas = self.datas;
if (datas.count) {
[desc appendFormat:@"Form Datas:\n"];
for (NSString *name in datas) {
NSData *data = datas[name];
[desc appendFormat:@"%@: %td bytes\n", name, data.length];
}
}
[desc appendFormat:@"Parameters:\n%@\n", [[[STHTTPNetTaskParametersPacker alloc] initWithNetTask:self] pack]];
return desc;
}
@end