API 说明

SDK API 概览

import http from '@ohos.net.http'
export interface HttpRequestWrapper {
  (options: HttpRequestWrapperOptions, callback: AsyncCallback<http.HttpResponse>): void
  (options: HttpRequestWrapperOptions): Promise<http.HttpResponse>
}

export type Tingyun = {
  // 初始化SDK
  init: (options: InitConfig) => void
  // 设置用户id
  setUserId: (userId: string) => void
  // 切换会话
  startNextSession: () => void
  // http request封装
  request: HttpRequestWrapper
  // 设置纬度,经度
  setLatLon: (latitude: number, longitude: number) => void
}

网络请求

Axios

SDK支持@ohos/axios库网络请求采集。配置方式为在SDK初始化时传入axios对象,如果首次启动需要采集网络请求,需要额外打开网络请求采集开关。

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服务器地址>',
      appKey: '<AppKey>',
      context: this.context,
      // 传入axios对象
      axios: axios,
      network: {
        // 首次启动打开网络请求监控
        enabled: true
      }
    })
  }
  // ... 
}

RCP

SDK通过提供拦截器的方式支持对RCP(remote communication platform)的监控。用户需要在调用createSession时加入SDK提供的拦截器

import tingyun from '@tingyun/sdk-core'

const session = rcp.createSession({
  // ...
  // 加入拦截器
  interceptors: [new tingyun.RCPInterceptor()]
});

// ...

SDK网络请求接口

SDK对系统@ohos.net.httpHttpRequest.request接口进行了封装,对外暴露了request接口。调用这个接口发送的网络请求会被SDK采集。

  • 接口
import http from '@ohos.net.http';

export type HttpRequestWrapperOptions = {
  url: string
  options?: http.HttpRequestOptions
  // http.createHttp() 返回的request对象,如果不传SDK内部会创建。不传httpRequest时,SDK内部只会在用户传入callback时在用户callback执行完后同步调用httpRequest.destroy。传入httpRequest时, SDK除了调用httpRequest.request之外不会调用其他方法。如果需要调用httpRequest其他方法、自行控制destroy时机以及使用promise方式时,需要传入httpRequest
  httpRequest?: http.HttpRequest
}
export interface HttpRequestWrapper {
  (options: HttpRequestWrapperOptions, callback: AsyncCallback<http.HttpResponse>): void
  (options: HttpRequestWrapperOptions): Promise<http.HttpResponse>
}

export type Tingyun = {
  // ...
  // http request封装
  request: HttpRequestWrapper
}
  • 示例
import tingyun from '@tingyun/sdk-core'

// 1. 传递URL (promise)
tingyun.request({url: 'xxx'})
  .then((res) => {
    // ...
  })
  .catch((err) => {
    // ...
  })

// 2. 传递URL (callback)
tingyun.request({url: 'xxx'}, (err, res) => {
  // ...
})

// 3. 传递URL和options (promise)
tingyun.request({url: 'xxx', options: {
  method: http.RequestMethod.POST,
  header: {
    'Content-Type': 'application/json'
  },
  extraData: {
    // ...
  }
}})
.then((res) => {
  // ...
})
.catch((err) => {
})

// 4. 传递URL和options (callback)
tingyun.request({url: 'xxx', options: {
  method: http.RequestMethod.POST,
  header: {
    'Content-Type': 'application/json'
  },
  extraData: {
    // ...
  }
}}, (err, res) => {
  // ...
})

// 5. 使用自定义请求对象
const httpRequest = http.createHttp()
tingyun.request({
  url: 'xxx', 
  httpRequest: httpRequest 
}, (err, res) => {
  // ...
})

用户ID

import tingyun from '@tingyun/sdk-core'

tingyun.setUserId('<USER_ID>')

注:SDK初始化结束之前如果多次设置用户ID,只会取最后一次设置的值

会话切换

import tingyun from '@tingyun/sdk-core'

tingyun.startNextSession()

经纬度

import tingyun from '@tingyun/sdk-core'

tingyun.setLatLon(<纬度>, <经度>)

注:

  • 纬度有效值为 [-90, 90]
  • 经度有效值为 [-180, 180]
  • 经纬度其中之一传无效值,API调用无效
© 2007-2024 北京基调网络股份有限公司 all right reserved,powered by Gitbook本文档更新于: 2024-12-13 15:14

results matching ""

    No results matching ""