diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..6c720d52f77941bd8b3781129adac426643c6d9f --- /dev/null +++ b/api/@ohos.net.http.d.ts @@ -0,0 +1,191 @@ +/* + * 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.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 0000000000000000000000000000000000000000..c52a5951c5b4d729af6ed87c09e2faee651e6e35 --- /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.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