From 587fcab7f7df5cb5667b10da0e9c59cc33059eb4 Mon Sep 17 00:00:00 2001 From: maosiping Date: Thu, 17 Feb 2022 17:04:50 +0800 Subject: [PATCH 1/4] add net.http and net.socket Signed-off-by: maosiping --- api/@ohos.net.http.d.ts | 191 ++++++++++++++++++++++++++++++++++++++ api/@ohos.net.socket.d.ts | 137 +++++++++++++++++++++++++++ 2 files changed, 328 insertions(+) create mode 100644 api/@ohos.net.http.d.ts create mode 100644 api/@ohos.net.socket.d.ts diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts new file mode 100644 index 0000000000..31a66b7561 --- /dev/null +++ b/api/@ohos.net.http.d.ts @@ -0,0 +1,191 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {AsyncCallback, Callback} from "./basic"; + +/** + * Provides http related APIs. + * + * @since 6 + * @sysCap SystemCapability.Communication.NetManager + * @devices phone, tablet, tv, wearable, car + */ +declare namespace http { + function createHttp(): HttpRequest; + + export interface HttpRequestOptions { + method?: RequestMethod; // default is GET + /** + * extraData can be a string or an Object (API 6) or an ArrayBuffer(API 8). + */ + extraData?: string | Object | ArrayBuffer; + header?: Object; // default is 'content-type': 'application/json' + readTimeout?: number; // default is 60s + connectTimeout?: number; // default is 60s. + /** + * @since 8 + */ + ifModifiedSince?: number; // "If-Modified-Since", default is 0. + /** + * @since 8 + */ + fixedLengthStreamingMode?: number; // default is -1 means disabled. + } + + export interface HttpRequest { + request(url: string, callback: AsyncCallback): void; + request(url: string, options: HttpRequestOptions, callback: AsyncCallback): void; + request(url: string, options?: HttpRequestOptions): Promise; + + destroy(): void; + + /** + * @deprecated use once() instead since 8. + */ + on(type: "headerReceive", callback: AsyncCallback): void; + /** + * @since 8 + */ + once(type: "headerReceive", callback: Callback): void; + /** + * @deprecated use once() instead since 8. + */ + off(type: "headerReceive", callback?: AsyncCallback): void; + } + + export enum RequestMethod { + OPTIONS = "OPTIONS", + GET = "GET", + HEAD = "HEAD", + POST = "POST", + PUT = "PUT", + DELETE = "DELETE", + TRACE = "TRACE", + CONNECT = "CONNECT" + } + + export enum ResponseCode { + OK = 200, + CREATED, + ACCEPTED, + NOT_AUTHORITATIVE, + NO_CONTENT, + RESET, + PARTIAL, + MULT_CHOICE = 300, + MOVED_PERM, + MOVED_TEMP, + SEE_OTHER, + NOT_MODIFIED, + USE_PROXY, + BAD_REQUEST = 400, + UNAUTHORIZED, + PAYMENT_REQUIRED, + FORBIDDEN, + NOT_FOUND, + BAD_METHOD, + NOT_ACCEPTABLE, + PROXY_AUTH, + CLIENT_TIMEOUT, + CONFLICT, + GONE, + LENGTH_REQUIRED, + PRECON_FAILED, + ENTITY_TOO_LARGE, + REQ_TOO_LONG, + UNSUPPORTED_TYPE, + INTERNAL_ERROR = 500, + NOT_IMPLEMENTED, + BAD_GATEWAY, + UNAVAILABLE, + GATEWAY_TIMEOUT, + VERSION + } + + export interface HttpResponse { + /** + * result can be a string or an Object (API 6) or an ArrayBuffer(API 8). + */ + result: string | Object | ArrayBuffer; + responseCode: ResponseCode | number; + header: Object; + /** + * @since 8 + */ + cookies: string; + } + + /** + * Creates a default {@code HttpResponseCache} object to store the responses of HTTP access requests. + * + * @param options {@code HttpResponseCacheOptions} + * @param callback the newly-installed cache + * @since 8 + */ + function createHttpResponseCache(options: HttpResponseCacheOptions, callback: AsyncCallback): void; + function createHttpResponseCache(options: HttpResponseCacheOptions): Promise; + + /** + * Obtains the {@code HttpResponseCache} object. + * + * @param callback Returns the {@code HttpResponseCache} object. + * @since 8 + */ + function getInstalledHttpResponseCache(callback: AsyncCallback): void; + function getInstalledHttpResponseCache(): Promise; + + /** + * @since 8 + */ + export interface HttpResponseCacheOptions { + /** + * Indicates the full directory for storing the cached data. + */ + filePath: string; + /** + * Indicates the child directory for storing the cached data. It is an optional parameter. + */ + fileChildPath?: string; + /** + * Indicates the maximum size of the cached data. + */ + cacheSize: number; + } + + /** + * @since 8 + */ + export interface HttpResponseCache { + /** + * Writes data in the cache to the file system so that all the cached data can be accessed in the next HTTP request. + */ + flush(callback: AsyncCallback): void; + flush(): Promise; + + /** + * Disables a cache without changing the data in it. + */ + close(callback: AsyncCallback): void; + close(): Promise; + + /** + * Disables a cache and deletes the data in it. + */ + delete(callback: AsyncCallback): void; + delete(): Promise; + } +} + +export default http; \ No newline at end of file diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts new file mode 100644 index 0000000000..e44b6e6f52 --- /dev/null +++ b/api/@ohos.net.socket.d.ts @@ -0,0 +1,137 @@ +/* +* Copyright (C) 2021 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import {AsyncCallback, Callback, ErrorCallback} from "./basic"; +import connection from "./@ohos.net.connection"; + +/** + * Provides TCP and UDP Socket APIs. + * + * @since 7 + * @sysCap SystemCapability.Communication.NetManager + * @devices phone, tablet, tv, wearable, car + */ +declare namespace socket { + export import NetAddress = connection.NetAddress; + + function constructUDPSocketInstance(): UDPSocket; + function constructTCPSocketInstance(): TCPSocket; + + export interface UDPSendOptions { + data: string | ArrayBuffer; + address: NetAddress; + } + + export interface ExtraOptionsBase { + receiveBufferSize?: number; + sendBufferSize?: number; + reuseAddress?: boolean; + socketTimeout?: number; + } + + export interface UDPExtraOptions extends ExtraOptionsBase { + broadcast?: boolean; + } + + export interface SocketStateBase { + isBound: boolean; + isClose: boolean; + isConnected: boolean; + } + + export interface SocketRemoteInfo { + address: string; + family: 'IPv4' | 'IPv6'; + port: number; + size: number; + } + + export interface UDPSocket { + bind(address: NetAddress, callback: AsyncCallback): void; + bind(address: NetAddress): Promise; + + send(options: UDPSendOptions, callback: AsyncCallback): void; + send(options: UDPSendOptions): Promise; + + close(callback: AsyncCallback): void; + close(): Promise; + + getState(callback: AsyncCallback): void; + getState(): Promise; + + setExtraOptions(options: UDPExtraOptions, callback: AsyncCallback): void; + setExtraOptions(options: UDPExtraOptions): Promise; + + on(type: 'message', callback: Callback<{message: ArrayBuffer, remoteInfo: SocketRemoteInfo}>): void; + off(type: 'message', callback?: Callback<{message: ArrayBuffer, remoteInfo: SocketRemoteInfo}>): void; + + on(type: 'listening' | 'close', callback: Callback): void; + off(type: 'listening' | 'close', callback?: Callback): void; + + on(type: 'error', callback: ErrorCallback): void; + off(type: 'error', callback?: ErrorCallback): void; + } + + export interface TCPConnectOptions { + address: NetAddress; + timeout?: number; + } + + export interface TCPSendOptions { + data: string | ArrayBuffer; + encoding?: string; + } + + export interface TCPExtraOptions extends ExtraOptionsBase { + keepAlive?: boolean; + OOBInline?: boolean; + TCPNoDelay?: boolean; + socketLinger: {on: boolean, linger: number}; + } + + export interface TCPSocket { + bind(address: NetAddress, callback: AsyncCallback): void; + bind(address: NetAddress): Promise; + + connect(options: TCPConnectOptions, callback: AsyncCallback): void; + connect(options: TCPConnectOptions): Promise; + + send(options: TCPSendOptions, callback: AsyncCallback): void; + send(options: TCPSendOptions): Promise; + + close(callback: AsyncCallback): void; + close(): Promise; + + getRemoteAddress(callback: AsyncCallback): void; + getRemoteAddress(): Promise; + + getState(callback: AsyncCallback): void; + getState(): Promise; + + setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback): void; + setExtraOptions(options: TCPExtraOptions): Promise; + + on(type: 'message', callback: Callback<{message: ArrayBuffer, remoteInfo: SocketRemoteInfo}>): void; + off(type: 'message', callback?: Callback<{message: ArrayBuffer, remoteInfo: SocketRemoteInfo}>): void; + + on(type: 'connect' | 'close', callback: Callback): void; + off(type: 'connect' | 'close', callback?: Callback): void; + + on(type: 'error', callback: ErrorCallback): void; + off(type: 'error', callback?: ErrorCallback): void; + } +} + +export default socket; \ No newline at end of file -- Gitee From b9c7ce57f4abf547cf208ced104190d4eafff81e Mon Sep 17 00:00:00 2001 From: maosiping Date: Thu, 17 Feb 2022 17:06:24 +0800 Subject: [PATCH 2/4] change year Signed-off-by: maosiping --- api/@ohos.net.http.d.ts | 2 +- api/@ohos.net.socket.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts index 31a66b7561..6c720d52f7 100644 --- a/api/@ohos.net.http.d.ts +++ b/api/@ohos.net.http.d.ts @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts index e44b6e6f52..c52a5951c5 100644 --- a/api/@ohos.net.socket.d.ts +++ b/api/@ohos.net.socket.d.ts @@ -1,5 +1,5 @@ /* -* Copyright (C) 2021 Huawei Device Co., Ltd. +* Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at -- Gitee From 1242978a9d54871dfc42a47a8a2ec09939c47aed Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 18 Feb 2022 16:19:54 +0800 Subject: [PATCH 3/4] add some interfaces Signed-off-by: maosiping --- .idea/.gitignore | 8 ++++ .idea/interface_sdk-js.iml | 8 ++++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ api/@ohos.net.connection.d.ts | 75 +++++++++++++++++++++++++++++ api/@ohos.net.http.d.ts | 89 +++++++---------------------------- api/@ohos.net.socket.d.ts | 2 +- 7 files changed, 123 insertions(+), 73 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/interface_sdk-js.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 api/@ohos.net.connection.d.ts diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..73f69e0958 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/interface_sdk-js.iml b/.idea/interface_sdk-js.iml new file mode 100644 index 0000000000..bc2cd87409 --- /dev/null +++ b/.idea/interface_sdk-js.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..732dc43fc9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..94a25f7f4c --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/@ohos.net.connection.d.ts b/api/@ohos.net.connection.d.ts new file mode 100644 index 0000000000..3080101bf9 --- /dev/null +++ b/api/@ohos.net.connection.d.ts @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {AsyncCallback, Callback} from "./basic"; +import http from "./@ohos.net.http"; +import socket from "./@ohos.net.socket"; + +/** + * Provides interfaces to manage and use data networks. + * + * @since 8 + * @sysCap SystemCapability.Communication.NetStack + * @devices phone, tablet, tv, wearable, car + */ +declare namespace connection { + type HttpRequest = http.HttpRequest; + type TCPSocket = socket.TCPSocket; + type UDPSocket = socket.UDPSocket; + + /** + * Obtains the data network that is activated by default. + * + *

To call this method, you must have the {@code ohos.permission.GET_NETWORK_INFO} permission. + * + * @param callback Returns the {@link NetHandle} object; + * returns {@code null} if the default network is not activated. + * @permission ohos.permission.GET_NETWORK_INFO + */ + function getDefaultNet(callback: AsyncCallback): void; + function getDefaultNet(): Promise; + + export interface NetHandle { + + /** + * Resolves a host name to obtain all IP addresses based on the specified NetHandle. + * + * @param host Indicates the host name or the domain. + * @param callback Returns the NetAddress list. + */ + getAddressesByName(host: string, callback: AsyncCallback>): void; + getAddressesByName(host: string): Promise>; + + /** + * Resolves a host name to obtain the first IP address based on the specified NetHandle. + * + * @param host Indicates the host name or the domain. + * @return Returns the first NetAddress. + */ + getAddressByName(host: string, callback: AsyncCallback): void; + getAddressByName(host: string): Promise; + } + + /** + * @since 7 + */ + export interface NetAddress { + address: string; + family?: number; // IPv4 = 1; IPv6 = 2, default is IPv4 + port?: number; // [0, 65535] + } +} + +export default connection; \ No newline at end of file diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts index 6c720d52f7..944b399214 100644 --- a/api/@ohos.net.http.d.ts +++ b/api/@ohos.net.http.d.ts @@ -28,20 +28,12 @@ declare namespace http { export interface HttpRequestOptions { method?: RequestMethod; // default is GET /** - * extraData can be a string or an Object (API 6) or an ArrayBuffer(API 8). + * extraData can be a string or an ArrayBuffer(API 8). */ extraData?: string | Object | ArrayBuffer; header?: Object; // default is 'content-type': 'application/json' - readTimeout?: number; // default is 60s - connectTimeout?: number; // default is 60s. - /** - * @since 8 - */ - ifModifiedSince?: number; // "If-Modified-Since", default is 0. - /** - * @since 8 - */ - fixedLengthStreamingMode?: number; // default is -1 means disabled. + readTimeout?: number; // default is 60000ms + connectTimeout?: number; // default is 60000ms. } export interface HttpRequest { @@ -63,6 +55,19 @@ declare namespace http { * @deprecated use once() instead since 8. */ off(type: "headerReceive", callback?: AsyncCallback): void; + + /** + * @since 8 + */ + on(type: "headersReceive", callback: Callback): void; + /** + * @since 8 + */ + once(type: "headersReceive", callback: Callback): void; + /** + * @since 8 + */ + off(type: "headersReceive", callback?: Callback): void; } export enum RequestMethod { @@ -116,7 +121,7 @@ declare namespace http { export interface HttpResponse { /** - * result can be a string or an Object (API 6) or an ArrayBuffer(API 8). + * result can be a string or an ArrayBuffer(API 8). */ result: string | Object | ArrayBuffer; responseCode: ResponseCode | number; @@ -126,66 +131,6 @@ declare namespace http { */ cookies: string; } - - /** - * Creates a default {@code HttpResponseCache} object to store the responses of HTTP access requests. - * - * @param options {@code HttpResponseCacheOptions} - * @param callback the newly-installed cache - * @since 8 - */ - function createHttpResponseCache(options: HttpResponseCacheOptions, callback: AsyncCallback): void; - function createHttpResponseCache(options: HttpResponseCacheOptions): Promise; - - /** - * Obtains the {@code HttpResponseCache} object. - * - * @param callback Returns the {@code HttpResponseCache} object. - * @since 8 - */ - function getInstalledHttpResponseCache(callback: AsyncCallback): void; - function getInstalledHttpResponseCache(): Promise; - - /** - * @since 8 - */ - export interface HttpResponseCacheOptions { - /** - * Indicates the full directory for storing the cached data. - */ - filePath: string; - /** - * Indicates the child directory for storing the cached data. It is an optional parameter. - */ - fileChildPath?: string; - /** - * Indicates the maximum size of the cached data. - */ - cacheSize: number; - } - - /** - * @since 8 - */ - export interface HttpResponseCache { - /** - * Writes data in the cache to the file system so that all the cached data can be accessed in the next HTTP request. - */ - flush(callback: AsyncCallback): void; - flush(): Promise; - - /** - * Disables a cache without changing the data in it. - */ - close(callback: AsyncCallback): void; - close(): Promise; - - /** - * Disables a cache and deletes the data in it. - */ - delete(callback: AsyncCallback): void; - delete(): Promise; - } } export default http; \ No newline at end of file diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts index c52a5951c5..6e42d6ccda 100644 --- a/api/@ohos.net.socket.d.ts +++ b/api/@ohos.net.socket.d.ts @@ -20,7 +20,7 @@ import connection from "./@ohos.net.connection"; * Provides TCP and UDP Socket APIs. * * @since 7 - * @sysCap SystemCapability.Communication.NetManager + * @sysCap SystemCapability.Communication.NetStack * @devices phone, tablet, tv, wearable, car */ declare namespace socket { -- Gitee From b09df95103579505769f0aa5236c6e98bc7effee Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 18 Feb 2022 16:21:22 +0800 Subject: [PATCH 4/4] add some interfaces Signed-off-by: maosiping --- .idea/.gitignore | 8 -------- .idea/interface_sdk-js.iml | 8 -------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 4 files changed, 30 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/interface_sdk-js.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0958..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/interface_sdk-js.iml b/.idea/interface_sdk-js.iml deleted file mode 100644 index bc2cd87409..0000000000 --- a/.idea/interface_sdk-js.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 732dc43fc9..0000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f4c..0000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file -- Gitee