This topic describes the PDSClient class, including its construction method, public methods, and event listeners.
PDSClient construction method
Creates a PDSClient instance that can call all the API operations of Photo and Drive Service (PDS).
const client = new PDSClient(config, context);Request parameters
Parameter | Type | Required | Description |
config | IClientParams | Yes | For more information, see the |
context | IContext | The context of the runtime environment. For more information, see the |
IClientParams
Parameter | Type | Required | Description |
token_info | TokenInfo | The information about the | |
share_token | string | The share token. | |
api_endpoint | string | Y1 | The endpoint of the PDS API. In most cases, the format is |
path_type | string | The data storage mode of the domain. Only | |
version | string | The version number. Only | |
refresh_token_fun | Function | The callback method to invoke when the | |
refresh_share_token_fun | Function | The callback method to invoke when the |
You must specify one of the
api_endpointandauth_endpointparameters.
TokenInfo
Parameter | Type | Required | Description |
access_token | string | Yes | The |
refresh_token | string | The | |
expire_time | string | The expiration time of the | |
expires_in | string | The validity period of the | |
token_type | string | The type of the | |
user_id | string | The unique ID of the current user. | |
role | string | The role that is assumed by the current user. | |
Other parameters | For more information, see Token. |
IContext
Parameter | Type | Required | Description |
isNode | boolean | Yes | Specifies whether the environment is a Node.js environment. |
Axios | object | Yes | For more information, see axios - npm. |
AxiosNodeAdapter | object | The | |
platform | string | The Valid values: | |
os | object | The native | |
fs | object | The native | |
path | object | The native | |
cp | object | The native | |
http | object | The native | |
https | object | The native | |
crypto | object | The native | |
worker_threads | object | The native |
Obtain an access token
You can obtain an access token in multiple ways based on the application type. For more information, see Application overview.
Refresh an access token
An access token is valid for 2 hours, and a refresh token is valid for seven days. During the validity period of a refresh token, you can refresh an access token regardless of whether the access token expires.
After you refresh the access token, the old access token and refresh token become invalid.
Procedure
When you initialize a
PDSClientinstance, specify therefresh_token_funparameter.
const client = new PDSClient({
refresh_token_fun: async ()=>{
// Call the backend API to refresh the access token. my_refresh_token_fun is a custom method used to refresh an access token.
const token_info = await my_refresh_token_fun()
return token_info
}
});For information about how to encapsulate a method that is used to refresh an
access token, see Token.
Basic public methods
setToken
Specifies an access token. If you do not specify the token_info parameter when you call the construction method, you can call this method to specify the parameter.
Make sure that the token_info parameter in the current PDSClient instance is valid before you call this method.
client.setToken(token_info)Request parameters
Parameter | Type | Required | Description |
token_info | TokenInfo | Yes | The information about the access token. |
Response parameters
None
setShareToken
Specifies a share token when you call share-related methods.
If you do not specify the share_token parameter when you call the construction method, you can call this method to specify the parameter.
Make sure that the share_token parameter in the current PDSClient instance is valid before you call this method.
client.setShareToken(share_token)Request parameters
Parameter | Type | Required | Description |
share_token | string | Yes | The share token. |
Response parameters
None
postAPI
Calls the basic API operations of PDS. Based on the postAPI method, you can encapsulate methods to call all the API operations of PDS.
const result = await client.postAPI(pathname, data={}, options={})The options parameter of the IPDSRequestConfig type is encapsulated based on the request config options of axios. For more information, see AxiosRequestConfig.
Request parameters
Parameter | Type | Required | Description |
pathname | string | Yes | The path to the API operation that you want to call. If you want to call the |
data | object | The JSON-formatted data used to call the API operation specified by the | |
options | IPDSRequestConfig | Other request parameters. |
Response parameters
Parameter | Type | Required | Description |
result | object | The returned data. |
Sample code
let {items =[]} = await client.postAPI('/file/list', {
drive_id: '1050',
parent_file_id: 'root',
})
console.log(items)Event listeners
You can listen to only error events.
error event
client.on('error', (error, req_opt)=> {
console.log('Error', error)
console.log('Request Options:', req_opt)
})Callback parameters
Parameter | Type | Required | Description |
error | PDSError | Yes | The error message. This parameter is of the |
req_opt | IPDSRequestConfig | The request parameters. |
PDSError
Parameter | Type | Required | Description |
status | number | The | |
code | string | The | |
message | string | Yes | The |
reqId | string | The | |
stack | string | The error stack that is used for debugging. | |
type | string | The type of the error. Valid values: |