Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit096a044

Browse files
committed
add client methods to replace call urllib in rpc
1 parent52a9cae commit096a044

File tree

5 files changed

+263
-77
lines changed

5 files changed

+263
-77
lines changed

‎index.d.ts‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import{Callback,RequestOptions}from'urllib';
77
import{AgentasHttpAgent,IncomingMessage}from'http';
88
import{AgentasHttpsAgent}from'https';
9+
import{Readable}from"stream";
910

1011
exportdeclaretypecallback=(e?:Error,respBody?:any,respInfo?:any)=>void;
1112

@@ -429,11 +430,19 @@ export declare namespace httpc {
429430
urllibOptions:RequestOptions;
430431
}
431432

432-
interfaceRespWrapper<T=any>{
433+
interfaceRespWrapperOptions<T=any>{
433434
data:T;
434435
resp:IncomingMessage;
435436
}
436437

438+
classRespWrapper<T=any>{
439+
data:T;
440+
resp:IncomingMessage;
441+
constructor(options:RespWrapperOptions);
442+
ok():boolean;
443+
needRetry():boolean;
444+
}
445+
437446
namespacemiddleware{
438447
interfaceMiddleware{
439448
send<T>(
@@ -522,16 +531,36 @@ export declare namespace httpc {
522531
}
523532
}
524533

525-
namespaceclient{
526-
interfaceHttpClientOptions{
527-
httpAgent?:HttpAgent;
528-
httpsAgent?:HttpsAgent;
529-
middlewares?:middleware.Middleware[];
530-
}
531-
classHttpClient{
532-
constructor(options:HttpClientOptions)
533-
sendRequest(requestOptions:ReqOpts):Promise<RespWrapper>
534-
}
534+
interfaceHttpClientOptions{
535+
httpAgent?:HttpAgent;
536+
httpsAgent?:HttpsAgent;
537+
middlewares?:middleware.Middleware[];
538+
}
539+
540+
interfaceGetOptions<T=any>extendsReqOpts<T>{
541+
params:Record<string,string>;
542+
headers:Record<string,string>;
543+
}
544+
545+
interfacePostOptions<T=any>extendsReqOpts<T>{
546+
data:string|Buffer|Readable;
547+
headers:Record<string,string>;
548+
}
549+
550+
interfacePutOptions<T=any>extendsReqOpts<T>{
551+
data:string|Buffer|Readable;
552+
headers:Record<string,string>
553+
}
554+
555+
classHttpClient{
556+
httpAgent:HttpAgent;
557+
httpsAgent:HttpsAgent;
558+
middlewares:middleware.Middleware[];
559+
constructor(options:HttpClientOptions)
560+
sendRequest(requestOptions:ReqOpts):Promise<RespWrapper>
561+
get(getOptions:GetOptions):Promise<RespWrapper>
562+
post(postOptions:PostOptions):Promise<RespWrapper>
563+
put(putOptions:PutOptions):Promise<RespWrapper>
535564
}
536565
}
537566

@@ -546,7 +575,7 @@ export declare namespace rpc {
546575
mac:auth.digest.Mac;
547576
}
548577

549-
constqnHttpClient:httpc.client.HttpClient;
578+
constqnHttpClient:httpc.HttpClient;
550579

551580
/**
552581
*

‎index.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module.exports = {
1010
conf:require('./qiniu/conf.js'),
1111
httpc:{
1212
middleware:require('./qiniu/httpc/middleware'),
13-
client:require('./qiniu/httpc/client')
13+
client:require('./qiniu/httpc/client'),
14+
responseWrapper:require('./qiniu/httpc/responseWrapper')
1415
},
1516
rpc:require('./qiniu/rpc.js'),
1617
util:require('./qiniu/util.js'),

‎qiniu/httpc/client.js‎

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const https = require('https');
44
consturllib=require('urllib');
55

66
constmiddleware=require('./middleware');
7+
const{ ResponseWrapper}=require('./responseWrapper');
78

89
/**
910
*
@@ -29,7 +30,7 @@ HttpClient.prototype._handleRequest = function (req) {
2930
reject(err);
3031
return;
3132
}
32-
resolve({ data, resp});
33+
resolve(newResponseWrapper({ data, resp}));
3334
});
3435
}catch(e){
3536
reject(e);
@@ -91,4 +92,150 @@ HttpClient.prototype.sendRequest = function (requestOptions) {
9192
returnresPromise;
9293
};
9394

95+
/**
96+
*@param {Object} reqOptions
97+
*@param {string} reqOptions.url
98+
*@param {http.Agent} [reqOptions.agent]
99+
*@param {https.Agent} [reqOptions.httpsAgent]
100+
*@param {Object} [reqOptions.params]
101+
*@param {Object} [reqOptions.headers]
102+
*@param {middleware.Middleware[]} [reqOptions.middlewares]
103+
*@param {urllib.RequestOptions} [urllibOptions]
104+
*@return {Promise<RespWrapper>}
105+
*/
106+
HttpClient.prototype.get=function(reqOptions,urllibOptions){
107+
const{
108+
url,
109+
params,
110+
headers,
111+
middlewares,
112+
agent,
113+
httpsAgent,
114+
callback
115+
}=reqOptions;
116+
117+
urllibOptions=urllibOptions||{};
118+
urllibOptions.method='GET';
119+
urllibOptions.headers=Object.assign(
120+
{
121+
Connection:'keep-alive'
122+
},
123+
headers,
124+
urllibOptions.headers||{}
125+
);
126+
urllibOptions.data=params;
127+
urllibOptions.followRedirect=true;
128+
129+
returnthis.sendRequest({
130+
url:url,
131+
middlewares:middlewares||[],
132+
agent:agent,
133+
httpsAgent:httpsAgent,
134+
callback:callback,
135+
urllibOptions:urllibOptions
136+
});
137+
};
138+
139+
/**
140+
*@param {Object} reqOptions
141+
*@param {string} reqOptions.url
142+
*@param {http.Agent} [reqOptions.agent]
143+
*@param {https.Agent} [reqOptions.httpsAgent]
144+
*@param {string | Buffer | Readable} [reqOptions.data]
145+
*@param {Object} [reqOptions.headers]
146+
*@param {middleware.Middleware[]} [reqOptions.middlewares]
147+
*@param {urllib.RequestOptions} [urllibOptions]
148+
*@return {Promise<RespWrapper>}
149+
*/
150+
HttpClient.prototype.post=function(reqOptions,urllibOptions){
151+
const{
152+
url,
153+
data,
154+
headers,
155+
middlewares,
156+
agent,
157+
httpsAgent,
158+
callback
159+
}=reqOptions;
160+
161+
urllibOptions=urllibOptions||{};
162+
urllibOptions.method='POST';
163+
urllibOptions.headers=Object.assign(
164+
{
165+
Connection:'keep-alive'
166+
},
167+
headers,
168+
urllibOptions.headers||{}
169+
);
170+
urllibOptions.gzip=true;
171+
172+
if(Buffer.isBuffer(data)||typeofdata==='string'){
173+
urllibOptions.content=data;
174+
}elseif(data){
175+
urllibOptions.stream=data;
176+
}else{
177+
urllibOptions.headers['Content-Length']='0';
178+
}
179+
180+
returnthis.sendRequest({
181+
url:url,
182+
middlewares:middlewares||[],
183+
agent:agent,
184+
httpsAgent:httpsAgent,
185+
callback:callback,
186+
urllibOptions:urllibOptions
187+
});
188+
};
189+
190+
/**
191+
*@param {Object} reqOptions
192+
*@param {string} reqOptions.url
193+
*@param {http.Agent} [reqOptions.agent]
194+
*@param {https.Agent} [reqOptions.httpsAgent]
195+
*@param {string | Buffer | ReadableStream} [reqOptions.data]
196+
*@param {Object} [reqOptions.headers]
197+
*@param {middleware.Middleware[]} [reqOptions.middlewares]
198+
*@param {urllib.RequestOptions} [urllibOptions]
199+
*@return {Promise<RespWrapper>}
200+
*/
201+
HttpClient.prototype.put=function(reqOptions,urllibOptions){
202+
const{
203+
url,
204+
data,
205+
headers,
206+
middlewares,
207+
agent,
208+
httpsAgent,
209+
callback
210+
}=reqOptions;
211+
212+
urllibOptions=urllibOptions||{};
213+
urllibOptions.method='PUT';
214+
urllibOptions.headers=Object.assign(
215+
{
216+
Connection:'keep-alive'
217+
},
218+
headers,
219+
urllibOptions.headers||{}
220+
);
221+
urllibOptions.gzip=true;
222+
223+
if(Buffer.isBuffer(data)||typeofdata==='string'){
224+
urllibOptions.content=data;
225+
}elseif(data){
226+
urllibOptions.stream=data;
227+
}else{
228+
urllibOptions.headers['Content-Length']='0';
229+
}
230+
231+
returnthis.sendRequest({
232+
url:url,
233+
middlewares:middlewares||[],
234+
agent:agent,
235+
httpsAgent:httpsAgent,
236+
callback:callback,
237+
urllibOptions:urllibOptions
238+
});
239+
};
240+
94241
exports.HttpClient=HttpClient;

‎qiniu/httpc/responseWrapper.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
functionResponseWrapper({
2+
data,
3+
resp
4+
}){
5+
this.data=data;
6+
this.resp=resp;
7+
}
8+
9+
/**
10+
*@return {boolean}
11+
*/
12+
ResponseWrapper.prototype.ok=function(){
13+
returnthis.resp&&Math.floor(this.resp.statusCode/100)===2;
14+
};
15+
16+
/**
17+
*@return {boolean}
18+
*/
19+
ResponseWrapper.prototype.needRetry=function(){
20+
if(!this.resp||!this.resp.statusCode){
21+
returntrue;
22+
}
23+
24+
if(this.ok()){
25+
returnfalse;
26+
}
27+
28+
conststatusCode=this.resp.statusCode;
29+
if([612,631].includes(statusCode)){
30+
returnfalse;
31+
}
32+
33+
conststatusFirstDigit=Math.floor(statusCode/100);
34+
if([5].includes(statusFirstDigit)){
35+
returntrue;
36+
}
37+
38+
if([4].includes(statusFirstDigit)){
39+
returnfalse;
40+
}
41+
42+
returnfalse;
43+
};
44+
45+
exports.ResponseWrapper=ResponseWrapper;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp