-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUUHttpSession.h
More file actions
executable file
·108 lines (80 loc) · 5.19 KB
/
UUHttpSession.h
File metadata and controls
executable file
·108 lines (80 loc) · 5.19 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
//
// UUHttpSession.h
// Useful Utilities - Lightweight Objective C HTTP Client
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@threejacks.com
//
// UUHttpSession provides a simple
//
#import <Foundation/Foundation.h>
#import "UUHttpResponseHandler.h"
@class UUHttpResponse;
typedef void (^UUHttpSessionResponseHandler)(UUHttpResponse* response);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construct a UUHttpRequest to pass to UUHttpSession
@interface UUHttpRequest : NSObject
- (id) initWithUrl:(NSString*)url;
@property (atomic, strong) NSString* url;
@property (atomic, assign) UUHttpMethod httpMethod;
@property (atomic, strong) NSDictionary* queryArguments;
@property (atomic, strong) NSDictionary* headerFields;
@property (atomic, strong) NSData* body;
@property (atomic, assign) NSTimeInterval timeout;
@property (atomic, strong) NSURLCredential* credentials;
@property (atomic, assign) BOOL processMimeTypes;
@property (atomic, strong, readonly) NSURLRequest* httpRequest;
// Static helper functions for the most common cases
+ (instancetype) getRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments;
+ (instancetype) deleteRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments;
+ (instancetype) putRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments body:(NSData*)body contentType:(NSString*)contentType;
+ (instancetype) postRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments body:(NSData*)body contentType:(NSString*)contentType;
+ (instancetype) patchRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments body:(NSData*)body contentType:(NSString*)contentType;
//Basic auth helper functions
+ (instancetype) getRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments user:(NSString*)user password:(NSString*)password;
+ (instancetype) deleteRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments user:(NSString*)user password:(NSString*)password;
+ (instancetype) putRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments body:(NSData*)body contentType:(NSString*)contentType user:(NSString*)user password:(NSString*)password;
+ (instancetype) postRequest:(NSString*)url queryArguments:(NSDictionary*)queryArguments body:(NSData*)body contentType:(NSString*)contentType user:(NSString*)user password:(NSString*)password;
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// UUHttpResponse encapsulates the relevant info for an app to query after a UUHttpRequest has completed
@interface UUHttpResponse : NSObject
@property (atomic, strong) NSError* httpError;
@property (atomic, strong) NSURLRequest* httpRequest;
@property (atomic, strong) NSHTTPURLResponse* httpResponse;
@property (atomic, strong) id parsedResponse;
@property (atomic, strong) NSData* rawResponse;
@property (atomic, strong) NSString* rawResponsePath;
@property (atomic, assign) NSTimeInterval downloadTime;
- (NSString *)description;
- (NSString *)debugDescription;
@end
@interface UUHttpSession : NSObject
+ (void) setRequestTimeout:(NSTimeInterval)requestTimeout;
+ (UUHttpRequest*) executeRequest:(UUHttpRequest*)request completionHandler:(UUHttpSessionResponseHandler)completionHandler;
+ (UUHttpRequest*) get:(NSString*)url queryArguments:(NSDictionary*)queryArguments completionHandler:(UUHttpSessionResponseHandler)completionHandler;
+ (UUHttpRequest*) delete:(NSString*)url queryArguments:(NSDictionary*)queryArguments completionHandler:(UUHttpSessionResponseHandler)completionHandler;
+ (UUHttpRequest*) put:(NSString*)url queryArguments:(NSDictionary*)queryArguments putBody:(NSData*)putBody contentType:(NSString*)contentType completionHandler:(UUHttpSessionResponseHandler)completionHandler;
+ (UUHttpRequest*) post:(NSString*)url queryArguments:(NSDictionary*)queryArguments postBody:(NSData*)postBody contentType:(NSString*)contentType completionHandler:(UUHttpSessionResponseHandler)completionHandler;
+ (void) registerResponseHandler:(NSObject<UUHttpResponseHandler>*)handler;
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Error constants
typedef enum
{
// Returned when NSURLSession completion block returns a non-nil NSError.
// In this case, the underlying NSError is wrapped in the user info block
// using the NSUnderlyingError key
UUHttpSessionErrorHttpFailure = -1,
// Returned when the NSURLSession completion block returns with a nil NSError
// and an HTTP return code that is not 2xx
UUHttpSessionErrorHttpError = -2,
// Returned when a user cancels an operation
UUHttpSessionErrorUserCancelled = -3,
} UUHttpSessionError;
extern NSString * const kUUHttpSessionErrorDomain;
extern NSString * const kUUHttpSessionHttpErrorCodeKey;
extern NSString * const kUUHttpSessionHttpErrorMessageKey;
extern NSString * const kUUHttpSessionAppResponseKey;