From 76246bbea43fe850ccc2941402a873d6c9d99e00 Mon Sep 17 00:00:00 2001 From: maosiping Date: Sat, 19 Feb 2022 16:45:58 +0800 Subject: [PATCH 1/2] add some interfaces Signed-off-by: maosiping --- api/@ohos.net.connection.d.ts | 75 +++++++++++++++++++ api/@ohos.net.http.d.ts | 136 +++++++++++++++++++++++++++++++++ api/@ohos.net.socket.d.ts | 137 ++++++++++++++++++++++++++++++++++ 3 files changed, 348 insertions(+) create mode 100644 api/@ohos.net.connection.d.ts create mode 100644 api/@ohos.net.http.d.ts create mode 100644 api/@ohos.net.socket.d.ts diff --git a/api/@ohos.net.connection.d.ts b/api/@ohos.net.connection.d.ts new file mode 100644 index 0000000000..9f10919dfd --- /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.NetManager.Core + * @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 new file mode 100644 index 0000000000..10f66cc1ae --- /dev/null +++ b/api/@ohos.net.http.d.ts @@ -0,0 +1,136 @@ +/* + * 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"; + +/** + * Provides http related APIs. + * + * @since 6 + * @sysCap SystemCapability.Communication.NetStack + * @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 ArrayBuffer(API 8). + */ + extraData?: string | Object | ArrayBuffer; + header?: Object; // default is 'content-type': 'application/json' + readTimeout?: number; // default is 60000ms + connectTimeout?: number; // default is 60000ms. + } + + 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; + + /** + * @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 { + 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 ArrayBuffer(API 8). + */ + result: string | Object | ArrayBuffer; + responseCode: ResponseCode | number; + header: Object; + /** + * @since 8 + */ + cookies: string; + } +} + +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..6e42d6ccda --- /dev/null +++ b/api/@ohos.net.socket.d.ts @@ -0,0 +1,137 @@ +/* +* 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, ErrorCallback} from "./basic"; +import connection from "./@ohos.net.connection"; + +/** + * Provides TCP and UDP Socket APIs. + * + * @since 7 + * @sysCap SystemCapability.Communication.NetStack + * @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 a8eaa31cb53ca3cfce952f088c81b4972dd95f32 Mon Sep 17 00:00:00 2001 From: maosiping Date: Mon, 21 Feb 2022 10:06:50 +0800 Subject: [PATCH 2/2] 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 | 2 +- api/@ohos.net.http.d.ts | 2 +- api/@ohos.net.socket.d.ts | 2 +- 7 files changed, 33 insertions(+), 3 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 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 index 9f10919dfd..ddd3241934 100644 --- a/api/@ohos.net.connection.d.ts +++ b/api/@ohos.net.connection.d.ts @@ -21,7 +21,7 @@ import socket from "./@ohos.net.socket"; * Provides interfaces to manage and use data networks. * * @since 8 - * @sysCap SystemCapability.Communication.NetManager.Core + * @syscap SystemCapability.Communication.NetManager.Core * @devices phone, tablet, tv, wearable, car */ declare namespace connection { diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts index 10f66cc1ae..ad39066279 100644 --- a/api/@ohos.net.http.d.ts +++ b/api/@ohos.net.http.d.ts @@ -19,7 +19,7 @@ import {AsyncCallback, Callback} from "./basic"; * Provides http related APIs. * * @since 6 - * @sysCap SystemCapability.Communication.NetStack + * @syscap SystemCapability.Communication.NetStack * @devices phone, tablet, tv, wearable, car */ declare namespace http { diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts index 6e42d6ccda..ac4f4692cf 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.NetStack + * @syscap SystemCapability.Communication.NetStack * @devices phone, tablet, tv, wearable, car */ declare namespace socket { -- Gitee