AWS Support 使用 SDK for Kotlin 的範例 - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK GitHub

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS Support 使用 SDK for Kotlin 的範例

下列程式碼範例示範如何使用 for Kotlin 搭配 來執行動作和實作常見案例 AWS SDK AWS Support。

基本知識是程式碼範例,示範如何在 服務內執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

開始使用

下列程式碼範例示範如何開始使用 AWS Support。

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html In addition, you must have the AWS Business Support Plan to use the AWS Support Java API. For more information, see: https://aws.amazon.com/premiumsupport/plans/ This Kotlin example performs the following task: 1. Gets and displays available services. */ suspend fun main() { displaySomeServices() } // Return a List that contains a Service name and Category name. suspend fun displaySomeServices() { val servicesRequest = DescribeServicesRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeServices(servicesRequest) println("Get the first 10 services") var index = 1 response.services?.forEach { service -> if (index == 11) { return@forEach } println("The Service name is: " + service.name) // Get the categories for this service. service.categories?.forEach { cat -> println("The category name is ${cat.name}") index++ } } } }
  • 如需API詳細資訊,請參閱DescribeServices中的 AWS SDK for Kotlin API參考

基本概念

以下程式碼範例顯示做法:

  • 取得並顯示案例可用的服務和嚴重性層級。

  • 根據選取的服務、類別和嚴重性層級建立支援案例。

  • 取得並顯示當天開啟的案例清單。

  • 將附件集和通訊新增至新案例。

  • 描述案例的新附件和通訊。

  • 解決案例。

  • 取得並顯示當天已解決的案例清單。

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html In addition, you must have the AWS Business Support Plan to use the AWS Support Java API. For more information, see: https://aws.amazon.com/premiumsupport/plans/ This Kotlin example performs the following tasks: 1. Gets and displays available services. 2. Gets and displays severity levels. 3. Creates a support case by using the selected service, category, and severity level. 4. Gets a list of open cases for the current day. 5. Creates an attachment set with a generated file. 6. Adds a communication with the attachment to the support case. 7. Lists the communications of the support case. 8. Describes the attachment set included with the communication. 9. Resolves the support case. 10. Gets a list of resolved cases for the current day. */ suspend fun main(args: Array<String>) { val usage = """ Usage: <fileAttachment> Where: fileAttachment - The file can be a simple saved .txt file to use as an email attachment. """ if (args.size != 1) { println(usage) exitProcess(0) } val fileAttachment = args[0] println("***** Welcome to the AWS Support case example scenario.") println("***** Step 1. Get and display available services.") val sevCatList = displayServices() println("***** Step 2. Get and display Support severity levels.") val sevLevel = displaySevLevels() println("***** Step 3. Create a support case using the selected service, category, and severity level.") val caseIdVal = createSupportCase(sevCatList, sevLevel) if (caseIdVal != null) { println("Support case $caseIdVal was successfully created!") } else { println("A support case was not successfully created!") exitProcess(1) } println("***** Step 4. Get open support cases.") getOpenCase() println("***** Step 5. Create an attachment set with a generated file to add to the case.") val attachmentSetId = addAttachment(fileAttachment) println("The Attachment Set id value is $attachmentSetId") println("***** Step 6. Add communication with the attachment to the support case.") addAttachSupportCase(caseIdVal, attachmentSetId) println("***** Step 7. List the communications of the support case.") val attachId = listCommunications(caseIdVal) println("The Attachment id value is $attachId") println("***** Step 8. Describe the attachment set included with the communication.") describeAttachment(attachId) println("***** Step 9. Resolve the support case.") resolveSupportCase(caseIdVal) println("***** Step 10. Get a list of resolved cases for the current day.") getResolvedCase() println("***** This Scenario has successfully completed") } suspend fun getResolvedCase() { // Specify the start and end time. val now = Instant.now() LocalDate.now() val yesterday = now.minus(1, ChronoUnit.DAYS) val describeCasesRequest = DescribeCasesRequest { maxResults = 30 afterTime = yesterday.toString() beforeTime = now.toString() includeResolvedCases = true } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCases(describeCasesRequest) response.cases?.forEach { sinCase -> println("The case status is ${sinCase.status}") println("The case Id is ${sinCase.caseId}") println("The case subject is ${sinCase.subject}") } } } suspend fun resolveSupportCase(caseIdVal: String) { val caseRequest = ResolveCaseRequest { caseId = caseIdVal } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.resolveCase(caseRequest) println("The status of case $caseIdVal is ${response.finalCaseStatus}") } } suspend fun describeAttachment(attachId: String?) { val attachmentRequest = DescribeAttachmentRequest { attachmentId = attachId } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeAttachment(attachmentRequest) println("The name of the file is ${response.attachment?.fileName}") } } suspend fun listCommunications(caseIdVal: String?): String? { val communicationsRequest = DescribeCommunicationsRequest { caseId = caseIdVal maxResults = 10 } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCommunications(communicationsRequest) response.communications?.forEach { comm -> println("the body is: " + comm.body) comm.attachmentSet?.forEach { detail -> return detail.attachmentId } } } return "" } suspend fun addAttachSupportCase( caseIdVal: String?, attachmentSetIdVal: String?, ) { val caseRequest = AddCommunicationToCaseRequest { caseId = caseIdVal attachmentSetId = attachmentSetIdVal communicationBody = "Please refer to attachment for details." } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addCommunicationToCase(caseRequest) if (response.result) { println("You have successfully added a communication to an AWS Support case") } else { println("There was an error adding the communication to an AWS Support case") } } } suspend fun addAttachment(fileAttachment: String): String? { val myFile = File(fileAttachment) val sourceBytes = (File(fileAttachment).readBytes()) val attachmentVal = Attachment { fileName = myFile.name data = sourceBytes } val setRequest = AddAttachmentsToSetRequest { attachments = listOf(attachmentVal) } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addAttachmentsToSet(setRequest) return response.attachmentSetId } } suspend fun getOpenCase() { // Specify the start and end time. val now = Instant.now() LocalDate.now() val yesterday = now.minus(1, ChronoUnit.DAYS) val describeCasesRequest = DescribeCasesRequest { maxResults = 20 afterTime = yesterday.toString() beforeTime = now.toString() } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCases(describeCasesRequest) response.cases?.forEach { sinCase -> println("The case status is ${sinCase.status}") println("The case Id is ${sinCase.caseId}") println("The case subject is ${sinCase.subject}") } } } suspend fun createSupportCase( sevCatListVal: List<String>, sevLevelVal: String, ): String? { val serCode = sevCatListVal[0] val caseCategory = sevCatListVal[1] val caseRequest = CreateCaseRequest { categoryCode = caseCategory.lowercase(Locale.getDefault()) serviceCode = serCode.lowercase(Locale.getDefault()) severityCode = sevLevelVal.lowercase(Locale.getDefault()) communicationBody = "Test issue with ${serCode.lowercase(Locale.getDefault())}" subject = "Test case, please ignore" language = "en" issueType = "technical" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.createCase(caseRequest) return response.caseId } } suspend fun displaySevLevels(): String { var levelName = "" val severityLevelsRequest = DescribeSeverityLevelsRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeSeverityLevels(severityLevelsRequest) response.severityLevels?.forEach { sevLevel -> println("The severity level name is: ${sevLevel.name}") if (sevLevel.name == "High") { levelName = sevLevel.name!! } } return levelName } } // Return a List that contains a Service name and Category name. suspend fun displayServices(): List<String> { var serviceCode = "" var catName = "" val sevCatList = mutableListOf<String>() val servicesRequest = DescribeServicesRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeServices(servicesRequest) println("Get the first 10 services") var index = 1 response.services?.forEach { service -> if (index == 11) { return@forEach } println("The Service name is ${service.name}") if (service.name == "Account") { serviceCode = service.code.toString() } // Get the categories for this service. service.categories?.forEach { cat -> println("The category name is ${cat.name}") if (cat.name == "Security") { catName = cat.name!! } } index++ } } // Push the two values to the list. serviceCode.let { sevCatList.add(it) } catName.let { sevCatList.add(it) } return sevCatList }

動作

下列程式碼範例示範如何使用 AddAttachmentsToSet

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun addAttachment(fileAttachment: String): String? { val myFile = File(fileAttachment) val sourceBytes = (File(fileAttachment).readBytes()) val attachmentVal = Attachment { fileName = myFile.name data = sourceBytes } val setRequest = AddAttachmentsToSetRequest { attachments = listOf(attachmentVal) } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addAttachmentsToSet(setRequest) return response.attachmentSetId } }

下列程式碼範例示範如何使用 AddCommunicationToCase

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun addAttachSupportCase( caseIdVal: String?, attachmentSetIdVal: String?, ) { val caseRequest = AddCommunicationToCaseRequest { caseId = caseIdVal attachmentSetId = attachmentSetIdVal communicationBody = "Please refer to attachment for details." } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addCommunicationToCase(caseRequest) if (response.result) { println("You have successfully added a communication to an AWS Support case") } else { println("There was an error adding the communication to an AWS Support case") } } }

下列程式碼範例示範如何使用 CreateCase

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun createSupportCase( sevCatListVal: List<String>, sevLevelVal: String, ): String? { val serCode = sevCatListVal[0] val caseCategory = sevCatListVal[1] val caseRequest = CreateCaseRequest { categoryCode = caseCategory.lowercase(Locale.getDefault()) serviceCode = serCode.lowercase(Locale.getDefault()) severityCode = sevLevelVal.lowercase(Locale.getDefault()) communicationBody = "Test issue with ${serCode.lowercase(Locale.getDefault())}" subject = "Test case, please ignore" language = "en" issueType = "technical" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.createCase(caseRequest) return response.caseId } }
  • 如需API詳細資訊,請參閱CreateCase中的 AWS SDK for Kotlin API參考

下列程式碼範例示範如何使用 DescribeAttachment

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun describeAttachment(attachId: String?) { val attachmentRequest = DescribeAttachmentRequest { attachmentId = attachId } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeAttachment(attachmentRequest) println("The name of the file is ${response.attachment?.fileName}") } }
  • 如需API詳細資訊,請參閱DescribeAttachment中的 AWS SDK for Kotlin API參考

下列程式碼範例示範如何使用 DescribeCases

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun getOpenCase() { // Specify the start and end time. val now = Instant.now() LocalDate.now() val yesterday = now.minus(1, ChronoUnit.DAYS) val describeCasesRequest = DescribeCasesRequest { maxResults = 20 afterTime = yesterday.toString() beforeTime = now.toString() } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCases(describeCasesRequest) response.cases?.forEach { sinCase -> println("The case status is ${sinCase.status}") println("The case Id is ${sinCase.caseId}") println("The case subject is ${sinCase.subject}") } } }
  • 如需API詳細資訊,請參閱DescribeCases中的 AWS SDK for Kotlin API參考

下列程式碼範例示範如何使用 DescribeCommunications

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun listCommunications(caseIdVal: String?): String? { val communicationsRequest = DescribeCommunicationsRequest { caseId = caseIdVal maxResults = 10 } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCommunications(communicationsRequest) response.communications?.forEach { comm -> println("the body is: " + comm.body) comm.attachmentSet?.forEach { detail -> return detail.attachmentId } } } return "" }

下列程式碼範例示範如何使用 DescribeServices

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Return a List that contains a Service name and Category name. suspend fun displayServices(): List<String> { var serviceCode = "" var catName = "" val sevCatList = mutableListOf<String>() val servicesRequest = DescribeServicesRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeServices(servicesRequest) println("Get the first 10 services") var index = 1 response.services?.forEach { service -> if (index == 11) { return@forEach } println("The Service name is ${service.name}") if (service.name == "Account") { serviceCode = service.code.toString() } // Get the categories for this service. service.categories?.forEach { cat -> println("The category name is ${cat.name}") if (cat.name == "Security") { catName = cat.name!! } } index++ } } // Push the two values to the list. serviceCode.let { sevCatList.add(it) } catName.let { sevCatList.add(it) } return sevCatList }
  • 如需API詳細資訊,請參閱DescribeServices中的 AWS SDK for Kotlin API參考

下列程式碼範例示範如何使用 DescribeSeverityLevels

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun displaySevLevels(): String { var levelName = "" val severityLevelsRequest = DescribeSeverityLevelsRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeSeverityLevels(severityLevelsRequest) response.severityLevels?.forEach { sevLevel -> println("The severity level name is: ${sevLevel.name}") if (sevLevel.name == "High") { levelName = sevLevel.name!! } } return levelName } }

下列程式碼範例示範如何使用 ResolveCase

SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun resolveSupportCase(caseIdVal: String) { val caseRequest = ResolveCaseRequest { caseId = caseIdVal } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.resolveCase(caseRequest) println("The status of case $caseIdVal is ${response.finalCaseStatus}") } }
  • 如需API詳細資訊,請參閱ResolveCase中的 AWS SDK for Kotlin API參考