• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import http from '@ohos.net.http'
17
18class Http {
19  url: string
20  extraData: Object
21  options: http.HttpRequestOptions
22
23  constructor() {
24    this.url = ''
25    this.options = {
26      method:  http.RequestMethod.GET,
27      extraData: this.extraData,
28      header: { 'Content-Type': 'application/json' },
29      readTimeout: 50000,
30      connectTimeout: 50000
31    }
32  }
33
34  setUrl(url: string) {
35    this.url = url
36  }
37
38  setMethod(method: string) {
39    switch (method) {
40      case 'GET':
41        this.options.method = http.RequestMethod.GET
42        break
43      case 'POST':
44        this.options.method = http.RequestMethod.POST
45        break
46      case 'PUT':
47        this.options.method = http.RequestMethod.PUT
48        break
49      case 'DELETE':
50        this.options.method = http.RequestMethod.DELETE
51        break
52      default:
53        this.options.method = http.RequestMethod.GET
54        break
55    }
56  }
57
58  setExtraData(extraData: Object) {
59    this.options.extraData = extraData
60  }
61
62  setOptions(option: Object) {
63    this.options = option
64  }
65
66  setList(list: number[], flag: number) {
67    list = []
68    for (let i = 0; i < flag; i++) {
69      list[i] = i
70    }
71    return list
72  }
73
74  setParameter(keys: string[], values: string[]) {
75    let result = {}
76    for (let i = 0; i <= keys.length - 1; i++) {
77      let key = keys[i]
78      let value = values[i]
79      result[key] = value
80    }
81    return result
82  }
83
84  async request() {
85    let httpRequest = http.createHttp()
86    let result = await httpRequest.request(this.url, this.options)
87    return result
88  }
89}
90
91export default new Http()