info
This commit is contained in:
Vendored
+69
@@ -0,0 +1,69 @@
|
||||
const querystring = require('querystring')
|
||||
const {callHuaweiOpenApi} = require('../normalize')
|
||||
|
||||
const API_ENDPOINT = 'https://oauth-login.cloud.huawei.com'
|
||||
const ACCOUNT_API_ENDPOINT = 'https://account.cloud.huawei.com'
|
||||
const OAUTH_API_ENDPOINT = 'https://oauth-api.cloud.huawei.com'
|
||||
|
||||
class HuaweiAccount {
|
||||
constructor(options) {
|
||||
this.options = options
|
||||
}
|
||||
|
||||
async _request({url, data, method = "GET", headers = {}, name = ""}) {
|
||||
const options = Object.assign({}, {
|
||||
dataType: 'json',
|
||||
timeout: 300000
|
||||
}, {
|
||||
method,
|
||||
headers
|
||||
})
|
||||
|
||||
return callHuaweiOpenApi({
|
||||
name,
|
||||
url,
|
||||
data,
|
||||
options
|
||||
})
|
||||
}
|
||||
|
||||
async getUserAccessToken(code) {
|
||||
return this._request(
|
||||
{
|
||||
name: "getUserAccessToken",
|
||||
url: `${API_ENDPOINT}/oauth2/v3/token`,
|
||||
data: {
|
||||
grantType: "authorization_code",
|
||||
clientId: this.options.clientId,
|
||||
clientSecret: this.options.clientSecret,
|
||||
code
|
||||
},
|
||||
method: "POST"
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async getAccessTokenInfo(accessToken) {
|
||||
return this._request({
|
||||
url: `${OAUTH_API_ENDPOINT}/rest.php?nsp_fmt=JSON&nsp_svc=huawei.oauth2.user.getTokenInfo`,
|
||||
data: {
|
||||
accessToken,
|
||||
openId: "OPENID"
|
||||
},
|
||||
method: "POST"
|
||||
})
|
||||
}
|
||||
|
||||
async getUserInfo (accessToken) {
|
||||
return this._request({
|
||||
url: `${ACCOUNT_API_ENDPOINT}/rest.php?nsp_svc=GOpen.User.getInfo`,
|
||||
data: {
|
||||
accessToken,
|
||||
getNickName: "1"
|
||||
},
|
||||
method: "POST"
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HuaweiAccount
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class HuaweiBase {
|
||||
constructor(options) {
|
||||
this.options = options
|
||||
}
|
||||
|
||||
}
|
||||
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
const {
|
||||
UniCloudError
|
||||
} = require('../../../common/error')
|
||||
const {
|
||||
camel2snakeJson,
|
||||
snake2camelJson
|
||||
} = require('../../../common/utils')
|
||||
|
||||
function generateApiResult (apiName, data) {
|
||||
if (data.error) {
|
||||
throw new UniCloudError({
|
||||
code: -2,
|
||||
message: data.error || data.error_description || `${apiName} fail`
|
||||
})
|
||||
} else {
|
||||
delete data.error
|
||||
delete data.error_description
|
||||
delete data.sub_error
|
||||
return {
|
||||
...data,
|
||||
errMsg: `${apiName} ok`,
|
||||
errCode: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function nomalizeError (apiName, error) {
|
||||
throw new UniCloudError({
|
||||
code: error.code || -2,
|
||||
message: error.message || `${apiName} fail`
|
||||
})
|
||||
}
|
||||
|
||||
async function callHuaweiOpenApi ({
|
||||
name,
|
||||
url,
|
||||
data,
|
||||
options,
|
||||
}) {
|
||||
options = Object.assign({}, options, { data: camel2snakeJson(Object.assign({}, data)) })
|
||||
let result
|
||||
try {
|
||||
result = await uniCloud.httpclient.request(url, options)
|
||||
} catch (e) {
|
||||
return nomalizeError(name, e)
|
||||
}
|
||||
|
||||
const respData = result.data
|
||||
|
||||
if (result.status !== 200) {
|
||||
return nomalizeError(name, new UniCloudError({
|
||||
code: respData.error,
|
||||
message: respData.error_description || 'Request failed'
|
||||
}))
|
||||
}
|
||||
|
||||
return snake2camelJson(
|
||||
generateApiResult(
|
||||
name,
|
||||
respData || {
|
||||
errCode: -2,
|
||||
errMsg: 'Request failed'
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
callHuaweiOpenApi
|
||||
}
|
||||
Reference in New Issue
Block a user