1
1
import http from '@ohos.net.http'
2
- import uploadFile from '@ohos.request'
2
+ import requestApi from '@ohos.request'
3
3
import ohCommon from '@ohos.app.ability.common'
4
4
5
5
import * as common from '../@internal'
@@ -9,6 +9,28 @@ interface RequestOptions extends common.HttpClientOptions {
9
9
method : common . HttpMethod
10
10
}
11
11
12
+ function parseHeader ( header : string ) : {
13
+ statusCode : number
14
+ header : common . HttpHeader
15
+ } {
16
+
17
+ const newHeader : common . HttpHeader = { }
18
+ const delimiter = '\r\n'
19
+ const [ first , ...lines ] = header . split ( delimiter ) . filter ( v => v !== '' )
20
+ for ( let index = 0 ; index < lines . length ; index ++ ) {
21
+ const line = lines [ index ]
22
+ const [ key , value ] = line . split ( ':' )
23
+ newHeader [ key ] = value . trim ( )
24
+ }
25
+
26
+ return {
27
+ // HTTP/1.1 200 OK
28
+ statusCode : parseInt ( first . split ( ' ' ) [ 1 ] , 10 ) ,
29
+ header : newHeader
30
+ }
31
+ }
32
+
33
+
12
34
function transformRequestMethod ( method : common . HttpMethod ) : http . RequestMethod {
13
35
if ( method === 'PUT' ) return http . RequestMethod . PUT
14
36
if ( method === 'GET' ) return http . RequestMethod . GET
@@ -24,20 +46,14 @@ function shouldUseUploadFile(option: RequestOptions): boolean {
24
46
return files . length > 0
25
47
}
26
48
27
- function normalizeFormHeader ( header : common . HttpHeader = { } ) : common . HttpHeader {
28
- const boundary = `----WebKitFormBoundary${ common . generateRandomString ( 12 ) } `
29
- header [ 'content-type' ] = `multipart/form-data; boundary=${ boundary } `
30
- return header
31
- }
32
-
33
49
export class HttpClient implements HttpClient {
34
50
constructor ( private context : ohCommon . BaseContext ) { }
35
51
36
52
private async request ( url : string , options : RequestOptions ) : Promise < common . Result < common . HttpResponse > > {
37
- // 使用 uploadFile 接口发送请求
53
+ // 使用 requestApi 接口发送请求
38
54
if ( shouldUseUploadFile ( options ) ) {
39
- const files : uploadFile . File [ ] = [ ]
40
- const formData : uploadFile . RequestData [ ] = [ ]
55
+ const files : requestApi . File [ ] = [ ]
56
+ const formData : requestApi . RequestData [ ] = [ ]
41
57
const bodyEntries = ( options . body as common . HttpFormData ) . entries ( )
42
58
43
59
for ( const [ key , value ] of bodyEntries ) {
@@ -62,17 +78,16 @@ export class HttpClient implements HttpClient {
62
78
}
63
79
64
80
return new Promise ( resolve => {
65
- const uploadFileOptions : uploadFile . UploadConfig = {
66
- url,
81
+ const uploadFileOptions : requestApi . UploadConfig = {
67
82
files,
68
83
data : formData ,
69
- header : normalizeFormHeader ( options . headers ) ,
70
- method : transformRequestMethod ( options . method )
84
+ url : url . toLowerCase ( ) ,
85
+ method : options . method ,
86
+ header : options . headers
71
87
}
72
88
73
89
try {
74
- // FIXME: 这个 api 现在有问题,用不了一点
75
- uploadFile . uploadFile ( this . context , common . removeUndefinedKeys ( uploadFileOptions ) )
90
+ requestApi . uploadFile ( this . context , common . removeUndefinedKeys ( uploadFileOptions ) )
76
91
. then ( task => {
77
92
if ( options . abort ) {
78
93
options . abort . onAbort ( ( ) => task . delete ( ) )
@@ -85,21 +100,28 @@ export class HttpClient implements HttpClient {
85
100
) )
86
101
}
87
102
88
- task . on ( 'complete' , states => {
89
- const firstState = states [ 0 ]
90
- mockProgress . end ( )
103
+ let responseCode : number = 0
104
+ let responseHeader : common . HttpHeader = { }
105
+ task . on ( 'headerReceive' , header => {
106
+ if ( typeof header === 'string' ) {
107
+ const data = parseHeader ( header )
108
+ responseCode = data . statusCode
109
+ header = data . header
110
+ }
111
+ } )
112
+
113
+ task . on ( 'complete' , ( ) => {
91
114
return resolve ( {
92
115
result : {
93
- data : firstState . message ,
94
- code : firstState . responseCode ,
95
- reqId : 'UploadFile api cannot get this value'
116
+ data : '' , // TODO: 暂时不支持读取 body,next 版本将会支持
117
+ code : responseCode ,
118
+ reqId : responseHeader [ 'X-Reqid' ]
96
119
}
97
120
} )
98
121
} )
99
122
100
123
task . on ( 'fail' , states => {
101
124
const firstState = states [ 0 ]
102
- mockProgress . end ( )
103
125
return resolve ( {
104
126
result : {
105
127
data : firstState . message ,
0 commit comments