MPRequest.h
2.95 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
//
// MPRequest.h
// MiPassportDemo
//
// Created by 李 业 on 13-7-12.
// Copyright (c) 2013年 李 业. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol MPRequestDelegate;
@interface MPRequest : NSObject{
NSURLConnection *_connection;
NSMutableData *_responseData;
}
@property(nonatomic,assign) id<MPRequestDelegate> delegate;
/**
* The URL which will be contacted to execute the request.
*/
@property(nonatomic, strong) NSString *url;
/**
* The API method which will be called.
*/
@property(nonatomic, strong) NSString *httpMethod;
/**
* The dictionary of parameters to pass to the method.
*
* These values in the dictionary will be converted to strings using the
* standard Objective-C object-to-string conversion facilities.
*/
@property(nonatomic,strong) NSMutableDictionary* params;
/**
* The encrypt method and key which will be used to generate a sign.
*/
@property(nonatomic, strong) NSString *encryptAlgorithm;
@property(nonatomic, strong) NSString *encryptKey;
- (id)initWithWithParams:(NSMutableDictionary *) params
httpMethod:(NSString *) httpMethod
delegate:(id<MPRequestDelegate>) delegate
requestURL:(NSString *) url
encryptAlgorithm:(NSString *)algorithm
encryptKey:(NSString *)key;
- (void)connect;
+ (MPRequest *)requestWithParams:(NSMutableDictionary *) params
httpMethod:(NSString *) httpMethod
delegate:(id<MPRequestDelegate>) delegate
requestURL:(NSString *) url
encryptAlgorithm:(NSString *)algorithm
encryptKey:(NSString *)key;
+ (NSString*)serializeURL:(NSString *)baseUrl
params:(NSDictionary *)params
httpMethod:(NSString *)httpMethod;
+ (NSString *)getParamValueFromUrl:(NSString*)url paramName:(NSString *)paramName;
@end
////////////////////////////////////////////////////////////////////////////////
/*
*Your application should implement this delegate
*/
@protocol MPRequestDelegate <NSObject>
@optional
/**
* Called just before the request is sent to the server.
*/
- (void)requestLoading:(MPRequest *)request;
/**
* Called when the server responds and begins to send back data.
*/
- (void)request:(MPRequest *)request didReceiveResponse:(NSURLResponse *)response;
/**
* Called when an error prevents the request from completing successfully.
*/
- (void)request:(MPRequest *)request didFailWithError:(NSError *)error;
/**
* Called when a request returns and its response has been parsed into
* an object.
*
* The resulting object may be a dictionary, an array, a string, or a number,
* depending on thee format of the API response.
*/
- (void)request:(MPRequest *)request didLoad:(id)result;
/**
* Called when a request returns a response.
*
* The result object is the raw response from the server of type NSData
*/
- (void)request:(MPRequest *)request didLoadRawResponse:(NSData *)data;
@end