用SDK于 Kotlin 的 Route 53 域名注册示例 - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

用SDK于 Kotlin 的 Route 53 域名注册示例

以下代码示例向您展示了如何使用 for Kotlin 和 Route 53 域名注册来执行操作和实现常见场景。 AWS SDK

基础知识是向您展示如何在服务中执行基本操作的代码示例。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

开始使用

以下代码示例显示如何开始使用 Route 53 域注册。

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 */ suspend fun main(args: Array<String>) { val usage = """ Usage: <domainType> Where: domainType - The domain type (for example, com). """ if (args.size != 1) { println(usage) exitProcess(0) } val domainType = args[0] println("Invokes ListPrices using a Paginated method.") listPricesPaginated(domainType) } suspend fun listPricesPaginated(domainType: String) { val pricesRequest = ListPricesRequest { maxItems = 10 tld = domainType } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .listPricesPaginated(pricesRequest) .transform { it.prices?.forEach { obj -> emit(obj) } } .collect { pr -> println("Registration: ${pr.registrationPrice} ${pr.registrationPrice?.currency}") println("Renewal: ${pr.renewalPrice?.price} ${pr.renewalPrice?.currency}") println("Transfer: ${pr.transferPrice?.price} ${pr.transferPrice?.currency}") println("Restoration: ${pr.restorationPrice?.price} ${pr.restorationPrice?.currency}") } } }
  • 有关API详细信息,请参阅ListPrices中的 Kotlin AWS SDK 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 This Kotlin code example performs the following operations: 1. List current domains. 2. List operations in the past year. 3. View billing for the account in the past year. 4. View prices for domain types. 5. Get domain suggestions. 6. Check domain availability. 7. Check domain transferability. 8. Request a domain registration. 9. Get operation details. 10. Optionally, get domain details. */ val DASHES: String = String(CharArray(80)).replace("\u0000", "-") suspend fun main(args: Array<String>) { val usage = """ Usage: <domainType> <phoneNumber> <email> <domainSuggestion> <firstName> <lastName> <city> Where: domainType - The domain type (for example, com). phoneNumber - The phone number to use (for example, +1.2065550100) email - The email address to use. domainSuggestion - The domain suggestion (for example, findmy.example). firstName - The first name to use to register a domain. lastName - The last name to use to register a domain. city - The city to use to register a domain. """ if (args.size != 7) { println(usage) exitProcess(1) } val domainType = args[0] val phoneNumber = args[1] val email = args[2] val domainSuggestion = args[3] val firstName = args[4] val lastName = args[5] val city = args[6] println(DASHES) println("Welcome to the Amazon Route 53 domains example scenario.") println(DASHES) println(DASHES) println("1. List current domains.") listDomains() println(DASHES) println(DASHES) println("2. List operations in the past year.") listOperations() println(DASHES) println(DASHES) println("3. View billing for the account in the past year.") listBillingRecords() println(DASHES) println(DASHES) println("4. View prices for domain types.") listAllPrices(domainType) println(DASHES) println(DASHES) println("5. Get domain suggestions.") listDomainSuggestions(domainSuggestion) println(DASHES) println(DASHES) println("6. Check domain availability.") checkDomainAvailability(domainSuggestion) println(DASHES) println(DASHES) println("7. Check domain transferability.") checkDomainTransferability(domainSuggestion) println(DASHES) println(DASHES) println("8. Request a domain registration.") val opId = requestDomainRegistration(domainSuggestion, phoneNumber, email, firstName, lastName, city) println(DASHES) println(DASHES) println("9. Get operation details.") getOperationalDetail(opId) println(DASHES) println(DASHES) println("10. Get domain details.") println("Note: You must have a registered domain to get details.") println("Otherwise an exception is thrown that states ") println("Domain xxxxxxx not found in xxxxxxx account.") getDomainDetails(domainSuggestion) println(DASHES) } suspend fun getDomainDetails(domainSuggestion: String?) { val detailRequest = GetDomainDetailRequest { domainName = domainSuggestion } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.getDomainDetail(detailRequest) println("The contact first name is ${response.registrantContact?.firstName}") println("The contact last name is ${response.registrantContact?.lastName}") println("The contact org name is ${response.registrantContact?.organizationName}") } } suspend fun getOperationalDetail(opId: String?) { val detailRequest = GetOperationDetailRequest { operationId = opId } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.getOperationDetail(detailRequest) println("Operation detail message is ${response.message}") } } suspend fun requestDomainRegistration( domainSuggestion: String?, phoneNumberVal: String?, emailVal: String?, firstNameVal: String?, lastNameVal: String?, cityVal: String?, ): String? { val contactDetail = ContactDetail { contactType = ContactType.Company state = "LA" countryCode = CountryCode.In email = emailVal firstName = firstNameVal lastName = lastNameVal city = cityVal phoneNumber = phoneNumberVal organizationName = "My Org" addressLine1 = "My Address" zipCode = "123 123" } val domainRequest = RegisterDomainRequest { adminContact = contactDetail registrantContact = contactDetail techContact = contactDetail domainName = domainSuggestion autoRenew = true durationInYears = 1 } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.registerDomain(domainRequest) println("Registration requested. Operation Id: ${response.operationId}") return response.operationId } } suspend fun checkDomainTransferability(domainSuggestion: String?) { val transferabilityRequest = CheckDomainTransferabilityRequest { domainName = domainSuggestion } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.checkDomainTransferability(transferabilityRequest) println("Transferability: ${response.transferability?.transferable}") } } suspend fun checkDomainAvailability(domainSuggestion: String) { val availabilityRequest = CheckDomainAvailabilityRequest { domainName = domainSuggestion } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.checkDomainAvailability(availabilityRequest) println("$domainSuggestion is ${response.availability}") } } suspend fun listDomainSuggestions(domainSuggestion: String?) { val suggestionsRequest = GetDomainSuggestionsRequest { domainName = domainSuggestion suggestionCount = 5 onlyAvailable = true } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.getDomainSuggestions(suggestionsRequest) response.suggestionsList?.forEach { suggestion -> println("Suggestion Name: ${suggestion.domainName}") println("Availability: ${suggestion.availability}") println(" ") } } } suspend fun listAllPrices(domainType: String?) { val pricesRequest = ListPricesRequest { tld = domainType } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .listPricesPaginated(pricesRequest) .transform { it.prices?.forEach { obj -> emit(obj) } } .collect { pr -> println("Registration: ${pr.registrationPrice} ${pr.registrationPrice?.currency}") println("Renewal: ${pr.renewalPrice?.price} ${pr.renewalPrice?.currency}") println("Transfer: ${pr.transferPrice?.price} ${pr.transferPrice?.currency}") println("Restoration: ${pr.restorationPrice?.price} ${pr.restorationPrice?.currency}") } } } suspend fun listBillingRecords() { val currentDate = Date() val localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() val zoneOffset = ZoneOffset.of("+01:00") val localDateTime2 = localDateTime.minusYears(1) val myStartTime = localDateTime2.toInstant(zoneOffset) val myEndTime = localDateTime.toInstant(zoneOffset) val timeStart: Instant? = myStartTime?.let { Instant(it) } val timeEnd: Instant? = myEndTime?.let { Instant(it) } val viewBillingRequest = ViewBillingRequest { start = timeStart end = timeEnd } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .viewBillingPaginated(viewBillingRequest) .transform { it.billingRecords?.forEach { obj -> emit(obj) } } .collect { billing -> println("Bill Date: ${billing.billDate}") println("Operation: ${billing.operation}") println("Price: ${billing.price}") } } } suspend fun listOperations() { val currentDate = Date() var localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() val zoneOffset = ZoneOffset.of("+01:00") localDateTime = localDateTime.minusYears(1) val myTime: java.time.Instant? = localDateTime.toInstant(zoneOffset) val time2: Instant? = myTime?.let { Instant(it) } val operationsRequest = ListOperationsRequest { submittedSince = time2 } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .listOperationsPaginated(operationsRequest) .transform { it.operations?.forEach { obj -> emit(obj) } } .collect { content -> println("Operation Id: ${content.operationId}") println("Status: ${content.status}") println("Date: ${content.submittedDate}") } } } suspend fun listDomains() { Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .listDomainsPaginated(ListDomainsRequest {}) .transform { it.domains?.forEach { obj -> emit(obj) } } .collect { content -> println("The domain name is ${content.domainName}") } } }

操作

以下代码示例显示了如何使用CheckDomainAvailability

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun checkDomainAvailability(domainSuggestion: String) { val availabilityRequest = CheckDomainAvailabilityRequest { domainName = domainSuggestion } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.checkDomainAvailability(availabilityRequest) println("$domainSuggestion is ${response.availability}") } }

以下代码示例显示了如何使用CheckDomainTransferability

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun checkDomainTransferability(domainSuggestion: String?) { val transferabilityRequest = CheckDomainTransferabilityRequest { domainName = domainSuggestion } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.checkDomainTransferability(transferabilityRequest) println("Transferability: ${response.transferability?.transferable}") } }

以下代码示例显示了如何使用GetDomainDetail

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun getDomainDetails(domainSuggestion: String?) { val detailRequest = GetDomainDetailRequest { domainName = domainSuggestion } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.getDomainDetail(detailRequest) println("The contact first name is ${response.registrantContact?.firstName}") println("The contact last name is ${response.registrantContact?.lastName}") println("The contact org name is ${response.registrantContact?.organizationName}") } }
  • 有关API详细信息,请参阅GetDomainDetail中的 Kotlin AWS SDK API 参考

以下代码示例显示了如何使用GetDomainSuggestions

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun listDomainSuggestions(domainSuggestion: String?) { val suggestionsRequest = GetDomainSuggestionsRequest { domainName = domainSuggestion suggestionCount = 5 onlyAvailable = true } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.getDomainSuggestions(suggestionsRequest) response.suggestionsList?.forEach { suggestion -> println("Suggestion Name: ${suggestion.domainName}") println("Availability: ${suggestion.availability}") println(" ") } } }

以下代码示例显示了如何使用GetOperationDetail

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun getOperationalDetail(opId: String?) { val detailRequest = GetOperationDetailRequest { operationId = opId } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.getOperationDetail(detailRequest) println("Operation detail message is ${response.message}") } }

以下代码示例显示了如何使用ListDomains

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun listDomains() { Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .listDomainsPaginated(ListDomainsRequest {}) .transform { it.domains?.forEach { obj -> emit(obj) } } .collect { content -> println("The domain name is ${content.domainName}") } } }
  • 有关API详细信息,请参阅ListDomains中的 Kotlin AWS SDK API 参考

以下代码示例显示了如何使用ListOperations

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun listOperations() { val currentDate = Date() var localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() val zoneOffset = ZoneOffset.of("+01:00") localDateTime = localDateTime.minusYears(1) val myTime: java.time.Instant? = localDateTime.toInstant(zoneOffset) val time2: Instant? = myTime?.let { Instant(it) } val operationsRequest = ListOperationsRequest { submittedSince = time2 } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .listOperationsPaginated(operationsRequest) .transform { it.operations?.forEach { obj -> emit(obj) } } .collect { content -> println("Operation Id: ${content.operationId}") println("Status: ${content.status}") println("Date: ${content.submittedDate}") } } }
  • 有关API详细信息,请参阅ListOperations中的 Kotlin AWS SDK API 参考

以下代码示例显示了如何使用ListPrices

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun listAllPrices(domainType: String?) { val pricesRequest = ListPricesRequest { tld = domainType } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .listPricesPaginated(pricesRequest) .transform { it.prices?.forEach { obj -> emit(obj) } } .collect { pr -> println("Registration: ${pr.registrationPrice} ${pr.registrationPrice?.currency}") println("Renewal: ${pr.renewalPrice?.price} ${pr.renewalPrice?.currency}") println("Transfer: ${pr.transferPrice?.price} ${pr.transferPrice?.currency}") println("Restoration: ${pr.restorationPrice?.price} ${pr.restorationPrice?.currency}") } } }
  • 有关API详细信息,请参阅ListPrices中的 Kotlin AWS SDK API 参考

以下代码示例显示了如何使用RegisterDomain

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun requestDomainRegistration( domainSuggestion: String?, phoneNumberVal: String?, emailVal: String?, firstNameVal: String?, lastNameVal: String?, cityVal: String?, ): String? { val contactDetail = ContactDetail { contactType = ContactType.Company state = "LA" countryCode = CountryCode.In email = emailVal firstName = firstNameVal lastName = lastNameVal city = cityVal phoneNumber = phoneNumberVal organizationName = "My Org" addressLine1 = "My Address" zipCode = "123 123" } val domainRequest = RegisterDomainRequest { adminContact = contactDetail registrantContact = contactDetail techContact = contactDetail domainName = domainSuggestion autoRenew = true durationInYears = 1 } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> val response = route53DomainsClient.registerDomain(domainRequest) println("Registration requested. Operation Id: ${response.operationId}") return response.operationId } }
  • 有关API详细信息,请参阅RegisterDomain中的 Kotlin AWS SDK API 参考

以下代码示例显示了如何使用ViewBilling

SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun listBillingRecords() { val currentDate = Date() val localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() val zoneOffset = ZoneOffset.of("+01:00") val localDateTime2 = localDateTime.minusYears(1) val myStartTime = localDateTime2.toInstant(zoneOffset) val myEndTime = localDateTime.toInstant(zoneOffset) val timeStart: Instant? = myStartTime?.let { Instant(it) } val timeEnd: Instant? = myEndTime?.let { Instant(it) } val viewBillingRequest = ViewBillingRequest { start = timeStart end = timeEnd } Route53DomainsClient { region = "us-east-1" }.use { route53DomainsClient -> route53DomainsClient .viewBillingPaginated(viewBillingRequest) .transform { it.billingRecords?.forEach { obj -> emit(obj) } } .collect { billing -> println("Bill Date: ${billing.billDate}") println("Operation: ${billing.operation}") println("Price: ${billing.price}") } } }
  • 有关API详细信息,请参阅ViewBilling中的 Kotlin AWS SDK API 参考