-
Notifications
You must be signed in to change notification settings - Fork 2
/
SCNetworkRequest.h
115 lines (92 loc) · 4.47 KB
/
SCNetworkRequest.h
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
//
// SCNetworkRequest.h
//
// -- Software License --
//
// Copyright (C) 2012, Steam Clock Software, Ltd.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// ----------------------
//
#import <Foundation/Foundation.h>
typedef enum {
SCNetworkRequestMethod_Get,
SCNetworkRequestMethod_Put,
SCNetworkRequestMethod_Post,
SCNetworkRequestMethod_Delete
} SCNetworkRequestMethod;
typedef void (^SCNetworkError)(NSURL* url, NSUInteger httpResponse, NSError* error, NSData* data);
//---------------------------------------------------------------------------------------------------------------------
// Store a set of default values to be used for the properties of the request, without having to set them individually
// all the time
@interface SCNetworkRequestDefaults : NSObject
// If set, this is prepended to the request URL. Note: does not try to do anything clever with slashes/ path components,
// just prepends the string
@property (strong) NSString* baseURL;
// Set of HTTP headers, both keys and values should be strings
@property (strong) NSDictionary* headers;
// Set of a text fragment to be appended to the query string. i.e. @"val1=a&val2=b"
@property (strong) NSString* additionalQueryString;
// Control whether NSNulls are automatically stripped out for JSON related requests
@property BOOL stripDictionaryNulls;
@property BOOL stripArrayNulls; // more dangerous, because it will change the array indexes
// Error reporting block
@property (strong) SCNetworkError error;
@end
//---------------------------------------------------------------------------------------------------------------------
// Basic network request
@interface SCNetworkRequest : NSObject
// The set of headers to be sent with the current request
@property (readonly, strong) NSMutableDictionary* headers;
// A block to call if the current request errors out
@property (strong) SCNetworkError error;
// Create a new request
-(id)initWithURL:(NSString*)url
defaults:(SCNetworkRequestDefaults*)defaults
method:(SCNetworkRequestMethod)method
content:(NSData*)content
response:(void (^)(NSData* data))response;
-(void)start;
@end
//---------------------------------------------------------------------------------------------------------------------
// Network request for JSON APIs
@interface SCJSONRequest : SCNetworkRequest
// Create a new request
-(id)initWithURL:(NSString*)url
defaults:(SCNetworkRequestDefaults*)defaults
method:(SCNetworkRequestMethod)method
contentJSON:(NSObject*)content
autoStart:(BOOL)start
response:(void (^)(NSObject* data))response;
@end
//---------------------------------------------------------------------------------------------------------------------
// Simple server exposing JSON APIS
@interface SCJSONServer : NSObject
// Persistent properties for the server are just a network defaults object
@property (strong) SCNetworkRequestDefaults* defaults;
// Make api calls
-(void)get:(NSString*)apiPath response:(void (^)(NSObject* data))responseBlock error:(SCNetworkError)error;
-(void)put:(NSString*)apiPath content:(NSObject*)content response:(void (^)(NSObject* data))responseBlock;
-(void)post:(NSString*)apiPath content:(NSObject*)content response:(void (^)(NSObject* data))responseBlock;
-(void)delete:(NSString*)apiPath response:(void (^)(NSObject* data))responseBlock;
@end