API Description
SDK API Overview
import http from '@ohos.net.http'
export interface HttpRequestWrapper {
(options: HttpRequestWrapperOptions, callback: AsyncCallback<http.HttpResponse>): void
(options: HttpRequestWrapperOptions): Promise<http.HttpResponse>
}
export type Tingyun = {
// Initialize SDK
init: (options: InitConfig) => void
// Set user id
setUserId: (userId: string) => void
// Switch session
startNextSession: () => void
// http request encapsulation
request: HttpRequestWrapper
// Set latitude and longitude
setLatLon: (latitude: number, longitude: number) => void
}
Network Request
Axios
SDK supports @ohos/axios
library network request collection. The configuration method is to pass in the axios object when the SDK is initialized. If you need to collect network requests for the first startup, you need to turn on the network request collection switch.
import tingyun from '@tingyun/sdk-core'
import axios from '@ohos/axios'
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
tingyun.init({
redirectHost: '<Redirect server address>',
appKey: '<AppKey>',
context: this.context,
// Pass in axios object
axios: axios,
network: {
// Enable network request monitoring for the first launch
enabled: true
}
})
}
// ...
}
RCP
The SDK supports monitoring of RCP (Remote Communication Platform) by providing interceptors. Users need to incorporate the interceptors provided by the SDK when calling createSession
.
import tingyun from '@tingyun/sdk-core'
const session = rcp.createSession({
// ...
// Add interceptors
interceptors: [new tingyun.RCPInterceptor()]
});
// ...
SDK network request interface
SDK encapsulates the HttpRequest.request
interface of the @ohos.net.http
package of the system and exposes the request
interface to the outside. The network request sent by calling this interface will be collected by the SDK.
- Interface
import http from '@ohos.net.http';
export type HttpRequestWrapperOptions = {
url: string
options?: http.HttpRequestOptions
// The request object returned by http.createHttp() will be created internally by the SDK if it is not passed. When httpRequest is not passed, the SDK will only call httpRequest.destroy synchronously after the callback is executed when the user passes in the callback. When httpRequest is passed in, the SDK will not call other methods except httpRequest.request. If you need to call other methods of httpRequest, control the destroy timing by yourself, and use the promise method, you need to pass in httpRequest
httpRequest?: http.HttpRequest
}
export interface HttpRequestWrapper {
(options: HttpRequestWrapperOptions, callback: AsyncCallback<http.HttpResponse>): void
(options: HttpRequestWrapperOptions): Promise<http.HttpResponse>
}
export type Tingyun = {
// ...
// http request encapsulation
request: HttpRequestWrapper
}
- Example
import tingyun from '@tingyun/sdk-core'
// 1. Pass URL (promise)
tingyun.request({url: 'xxx'})
.then((res) => {
// ...
})
.catch((err) => {
// ...
})
// 2. Pass URL (callback)
tingyun.request({url: 'xxx'}, (err, res) => {
// ...
})
// 3. Pass URL and options (promise)
tingyun.request({url: 'xxx', options: {
method: http.RequestMethod.POST,
header: {
'Content-Type': 'application/json'
},
extraData: {
// ...
}
}})
.then((res) => {
// ...
})
.catch((err) => {
})
// 4. Pass URL and options (callback)
tingyun.request({url: 'xxx', options: {
method: http.RequestMethod.POST,
header: {
'Content-Type': 'application/json'
},
extraData: {
// ...
}
}}, (err, res) => {
// ...
})
// 5. Use custom request object
const httpRequest = http.createHttp()
tingyun.request({
url: 'xxx',
httpRequest: httpRequest
}, (err, res) => {
// ...
})
User ID
import tingyun from '@tingyun/sdk-core'
tingyun.setUserId('<USER_ID>')
Note: If the user ID is set multiple times before the SDK initialization is completed, only the last set value will be used
Session switching
import tingyun from '@tingyun/sdk-core'
tingyun.startNextSession()
Latitude and longitude
import tingyun from '@tingyun/sdk-core'
tingyun.setLatLon(<latitude>, <longitude>)
Note:
- Valid values for latitude are [-90, 90]
- Valid values for longitude are [-180, 180]
- If either latitude or longitude is invalid, the API call will be invalid