2026-04-21 22:07:29 +08:00
|
|
|
import http from '@ohos.net.http'
|
|
|
|
|
import type { ApiResponse } from './Types'
|
|
|
|
|
import { SDKContext } from './SDKContext'
|
|
|
|
|
|
|
|
|
|
export class HttpClient {
|
2026-04-28 16:55:11 +08:00
|
|
|
static async request<T>(
|
|
|
|
|
method: http.RequestMethod,
|
|
|
|
|
path: string,
|
|
|
|
|
body?: object,
|
|
|
|
|
query?: Record<string, string | number | boolean | Date | null | undefined>,
|
|
|
|
|
): Promise<T> {
|
2026-04-21 22:07:29 +08:00
|
|
|
const config = SDKContext.getConfig()
|
|
|
|
|
const token = SDKContext.getToken()
|
2026-04-28 16:55:11 +08:00
|
|
|
const queryPairs: string[] = []
|
|
|
|
|
if (query) {
|
|
|
|
|
for (const key of Object.keys(query)) {
|
|
|
|
|
const value = query[key]
|
|
|
|
|
if (value === undefined || value === null || value === '') continue
|
|
|
|
|
queryPairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value instanceof Date ? value.toISOString() : String(value))}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const url = config.apiBaseUrl.replace(/\/$/, '') + path + (queryPairs.length > 0 ? `?${queryPairs.join('&')}` : '')
|
2026-04-21 22:07:29 +08:00
|
|
|
|
|
|
|
|
const client = http.createHttp()
|
|
|
|
|
try {
|
|
|
|
|
const options: http.HttpRequestOptions = {
|
|
|
|
|
method,
|
|
|
|
|
header: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
|
|
|
|
},
|
|
|
|
|
extraData: body ? JSON.stringify(body) : undefined,
|
|
|
|
|
connectTimeout: 15000,
|
|
|
|
|
readTimeout: 15000,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res = await client.request(url, options)
|
|
|
|
|
const json: ApiResponse<T> = JSON.parse(res.result as string) as ApiResponse<T>
|
|
|
|
|
if (json.code !== 200) throw new Error(json.message)
|
|
|
|
|
return json.data
|
|
|
|
|
} finally {
|
|
|
|
|
client.destroy()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 16:55:11 +08:00
|
|
|
static get<T>(path: string, query?: Record<string, string | number | boolean | Date | null | undefined>): Promise<T> {
|
|
|
|
|
return HttpClient.request<T>(http.RequestMethod.GET, path, undefined, query)
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 16:55:11 +08:00
|
|
|
static post<T>(path: string, body?: object, query?: Record<string, string | number | boolean | Date | null | undefined>): Promise<T> {
|
|
|
|
|
return HttpClient.request<T>(http.RequestMethod.POST, path, body, query)
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 16:55:11 +08:00
|
|
|
static put<T>(path: string, body?: object, query?: Record<string, string | number | boolean | Date | null | undefined>): Promise<T> {
|
|
|
|
|
return HttpClient.request<T>(http.RequestMethod.PUT, path, body, query)
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 16:55:11 +08:00
|
|
|
static delete<T>(path: string, query?: Record<string, string | number | boolean | Date | null | undefined>): Promise<T> {
|
|
|
|
|
return HttpClient.request<T>(http.RequestMethod.DELETE, path, undefined, query)
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
}
|