FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SVHTTPRequest/SVHTTPRequest/SVHTTPClient.m at master · shizu2014/SVHTTPRequest · GitHub
shizu2014
/
SVHTTPRequest
Public
forked from
TransitApp/SVHTTPRequest
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
SVHTTPRequest
/
SVHTTPRequest
/
SVHTTPClient.m
Copy path
More file actions
More file actions
Latest commit
History
History
History
175 lines (130 loc) · 7.12 KB
Breadcrumbs
SVHTTPRequest
/
SVHTTPRequest
/
SVHTTPClient.m
Copy path
File metadata and controls
175 lines (130 loc) · 7.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
//
SVHTTPClient.m
//
//
Created by Sam Vermette on 15.12.11.
//
Copyright 2011 samvermette.com. All rights reserved.
//
//
https://github.com/samvermette/SVHTTPRequest
//
#
import
"
SVHTTPClient.h
"
#
import
"
SVHTTPRequest.h
"
@interface
SVHTTPClient
()
@property
(
nonatomic
,
strong
)
NSOperationQueue
*operationQueue;
- (SVHTTPRequest*)
queueRequest
:
(
NSString
*)
path
method
:
(SVHTTPRequestMethod)
method
parameters
:
(
NSDictionary
*)
parameters
saveToPath
:
(
NSString
*)
savePath
progress
:
(
void
(^)(
float
))
progressBlock
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
;
@property
(
nonatomic
,
strong
)
NSMutableDictionary
*HTTPHeaderFields;
@end
@implementation
SVHTTPClient
+ (
instancetype
)
sharedClient
{
return
[
self
sharedClientWithIdentifier:
@"
master
"
];
}
+ (
instancetype
)
sharedClientWithIdentifier
:
(
NSString
*)
identifier
{
SVHTTPClient *sharedClient = [[
self
sharedClients
]
objectForKey:
identifier];
if
(!sharedClient) {
sharedClient = [[
self
alloc
]
init
];
[[
self
sharedClients
]
setObject:
sharedClient
forKey:
identifier];
}
return
sharedClient;
}
+ (
id
)
sharedClients
{
static
NSMutableDictionary
*_sharedClients =
nil
;
static
dispatch_once_t
oncePredicate;
dispatch_once
(&oncePredicate, ^{ _sharedClients = [[
NSMutableDictionary
alloc
]
init
]; });
return
_sharedClients;
}
- (
id
)
init
{
if
(self = [
super
init
]) {
self.
operationQueue
= [[
NSOperationQueue
alloc
]
init
];
self.
basePath
=
@"
"
;
}
return
self;
}
#
pragma mark
- Setters
- (
void
)
setBasicAuthWithUsername
:
(
NSString
*)
newUsername
password
:
(
NSString
*)
newPassword
{
self.
username
= newUsername;
self.
password
= newPassword;
}
#
pragma mark
- Request Methods
- (SVHTTPRequest*)
GET
:
(
NSString
*)
path
parameters
:
(
NSDictionary
*)
parameters
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
{
return
[
self
queueRequest:
path
method:
SVHTTPRequestMethodGET
parameters:
parameters
saveToPath:
nil
progress:
nil
completion:
completionBlock];
}
- (SVHTTPRequest*)
GET
:
(
NSString
*)
path
parameters
:
(
NSDictionary
*)
parameters
saveToPath
:
(
NSString
*)
savePath
progress
:
(
void
(^)(
float
))
progressBlock
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
{
return
[
self
queueRequest:
path
method:
SVHTTPRequestMethodGET
parameters:
parameters
saveToPath:
savePath
progress:
progressBlock
completion:
completionBlock];
}
- (SVHTTPRequest*)
POST
:
(
NSString
*)
path
parameters
:
(
NSDictionary
*)
parameters
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
{
return
[
self
queueRequest:
path
method:
SVHTTPRequestMethodPOST
parameters:
parameters
saveToPath:
nil
progress:
nil
completion:
completionBlock];
}
- (SVHTTPRequest*)
POST
:
(
NSString
*)
path
parameters
:
(
NSDictionary
*)
parameters
progress
:
(
void
(^)(
float
))
progressBlock
completion
:
(
void
(^)(
id
,
NSHTTPURLResponse
*,
NSError
*))
completionBlock
{
return
[
self
queueRequest:
path
method:
SVHTTPRequestMethodPOST
parameters:
parameters
saveToPath:
nil
progress:
progressBlock
completion:
completionBlock];
}
- (SVHTTPRequest*)
PUT
:
(
NSString
*)
path
parameters
:
(
NSDictionary
*)
parameters
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
{
return
[
self
queueRequest:
path
method:
SVHTTPRequestMethodPUT
parameters:
parameters
saveToPath:
nil
progress:
nil
completion:
completionBlock];
}
- (SVHTTPRequest*)
DELETE
:
(
NSString
*)
path
parameters
:
(
NSDictionary
*)
parameters
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
{
return
[
self
queueRequest:
path
method:
SVHTTPRequestMethodDELETE
parameters:
parameters
saveToPath:
nil
progress:
nil
completion:
completionBlock];
}
- (SVHTTPRequest*)
HEAD
:
(
NSString
*)
path
parameters
:
(
NSDictionary
*)
parameters
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
{
return
[
self
queueRequest:
path
method:
SVHTTPRequestMethodHEAD
parameters:
parameters
saveToPath:
nil
progress:
nil
completion:
completionBlock];
}
#
pragma mark
- Operation Cancelling
- (
void
)
cancelRequestsWithPath
:
(
NSString
*)
path
{
[
self
.operationQueue.operations
enumerateObjectsUsingBlock:
^(
id
request,
NSUInteger
idx,
BOOL
*stop) {
NSString
*requestPath = [request
valueForKey:
@"
requestPath
"
];
if
([requestPath
isEqualToString:
path])
[request
cancel
];
}];
}
- (
void
)
cancelAllRequests
{
[
self
.operationQueue
cancelAllOperations
];
}
#
pragma mark
-
- (
NSMutableDictionary
*)
HTTPHeaderFields
{
if
(_HTTPHeaderFields ==
nil
)
_HTTPHeaderFields = [
NSMutableDictionary
new
];
return
_HTTPHeaderFields;
}
- (
void
)
setValue
:
(
NSString
*)
value
forHTTPHeaderField
:
(
NSString
*)
field
{
[
self
.HTTPHeaderFields
setValue:
value
forKey:
field];
}
- (SVHTTPRequest*)
queueRequest
:
(
NSString
*)
path
method
:
(SVHTTPRequestMethod)
method
parameters
:
(
NSDictionary
*)
parameters
saveToPath
:
(
NSString
*)
savePath
progress
:
(
void
(^)(
float
))
progressBlock
completion
:
(SVHTTPRequestCompletionHandler)
completionBlock
{
NSString
*completeURLString = [
NSString
stringWithFormat:
@"
%@%@
"
,
self
.basePath, path];
id
mergedParameters;
if
((method == SVHTTPRequestMethodPOST || method == SVHTTPRequestMethodPUT) && self.
sendParametersAsJSON
&& ![parameters
isKindOfClass:
[
NSDictionary
class
]])
mergedParameters = parameters;
else
{
mergedParameters = [
NSMutableDictionary
dictionary
];
[mergedParameters
addEntriesFromDictionary:
parameters];
[mergedParameters
addEntriesFromDictionary:
self
.baseParameters];
}
SVHTTPRequest *requestOperation = [(
id
<SVHTTPRequestPrivateMethods>)[SVHTTPRequest
alloc
]
initWithAddress:
completeURLString
method:
method
parameters:
mergedParameters
saveToPath:
savePath
progress:
progressBlock
completion:
completionBlock];
return
[
self
queueRequest:
requestOperation];
}
- (SVHTTPRequest*)
queueRequest
:
(SVHTTPRequest*)
requestOperation
{
requestOperation.
sendParametersAsJSON
= self.
sendParametersAsJSON
;
requestOperation.
cachePolicy
= self.
cachePolicy
;
requestOperation.
userAgent
= self.
userAgent
;
requestOperation.
timeoutInterval
= self.
timeoutInterval
;
[(
id
<SVHTTPRequestPrivateMethods>)requestOperation
setClient:
self
];
[
self
.HTTPHeaderFields
enumerateKeysAndObjectsUsingBlock:
^(
NSString
*field,
NSString
*value,
BOOL
*stop) {
[requestOperation
setValue:
value
forHTTPHeaderField:
field];
}];
if
(self.
username
&& self.
password
)
[(
id
<SVHTTPRequestPrivateMethods>)requestOperation
signRequestWithUsername:
self
.username
password:
self
.password];
[
self
.operationQueue
addOperation:
requestOperation];
return
requestOperation;
}
@end
Back
|
FazBrowse Home
|
New Git URL