HTTP API
Manage Cloud Code scripts using raw HTTP APIs and the Admin API.
阅读时间4 分钟最后更新于 1 个月前
您可以使用原始 HTTP API 来管理 Cloud Code 脚本。
使用 API
- Admin API 文档包含管理操作(例如创建、读取、更新和删除脚本)的详细描述。
- Client API 文档包含面向客户端的操作(例如运行脚本)的详细描述。
Authorization 标头
要对请求进行身份验证,请使用基本身份验证)。创建一个服务帐户,对<KEY_ID>:<SECRET_KEY>Authorization--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
部署脚本
您可以通过发送以下请求来创建脚本:为了使脚本可供游戏客户端使用,必须发布脚本:curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts' \--header 'Content-Type: application/json' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \--data '{ "name": "test-script", "type": "API", "code": "module.exports = () => {return \"Result from standalone API script\";}", "language": "JS"}'
每次发布时,脚本版本都会递增。成功的响应可能如下所示:curl --request POST 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>/publish' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
无法发布自上次发布以来未发生任何更改的脚本。{ "version": 1, "datePublished": "2023-06-23T10:39:20Z"}
更新脚本
您可以通过发送以下请求来更新脚本代码和/或参数:为了使脚本的更新版本可供游戏客户端使用,必须再次发布,从而创建版本 2。curl --request PATCH 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \--header 'Content-Type: application/json' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \--data '{ "params": [ { "name": "parameter", "type": "STRING", "required": false } ], "code": "module.exports = () => {return \"Updated result from standalone API script\";}"}'
{ "version": 2, "datePublished": "2023-06-23T10:40:00Z"}
回滚脚本
如果您发布了脚本,而又对新版本不满意,则可以通过发送含指定版本号的请求来回滚到前一个版本:现在,第一个脚本版本重新发布为版本 3:curl --request POST 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>/publish' \--header 'Content-Type: application/json' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'--data '{ "version": 1}'
{ "version": 3, "datePublished": "2023-06-23T10:50:00Z"}
获取脚本
要查看脚本的内容,可以发送以下请求:响应可能如下所示:curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
响应包含以下信息:{ "name": "test", "type": "API", "language": "JS", "activeScript": { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "version": 2, "datePublished": "2021-07-23T16:08:35Z" }, "versions": [ { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "isDraft": true, "version": null, "dateUpdated": "2021-07-23T15:59:32Z" }, { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "isDraft": false, "version": 2, "dateUpdated": "2021-07-23T16:08:35Z", "dateCreated": "2021-07-23T16:08:35Z" }, { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "isDraft": false, "version": 1, "dateUpdated": "2021-07-23T15:59:58Z", "dateCreated": "2021-07-23T15:59:58Z" } ], "params": [ { "name": "someThing", "type": "STRING", "required": true } ]}
- :脚本的名称。
name - :脚本的类型。Cloud Code 脚本仅支持 API 脚本。
type - :脚本的语言。Cloud Code 脚本仅支持 JavaScript。
language
activeScriptversionsisDrafttrueversion此列表响应可能如下所示:curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
响应中包含已创建的所有脚本的快速概览。要使用分页功能,可以使用查询参数来缩小结果的范围,并获取指向下一页结果的链接。{ "results": [ { "name": "script", "language": "JS", "type": "API", "published": true, "lastPublishedDate": "2022-03-08T12:37:48Z", "lastPublishedVersion": 40 }, { "name": "test", "language": "JS", "type": "API", "published": true, "lastPublishedDate": "2023-06-20T15:14:58Z", "lastPublishedVersion": 4 } ], "links": { "next": null }}
下一个链接可能如下所示:curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts?limit=2' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
{ "links": { "next": "/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts?after=<SCRIPT_NAME>" }}
删除脚本
要删除脚本,可以发送以下请求:空的响应表示删除成功。curl --request DELETE 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'