使用 Amazon SDK for .NET 的 Amazon Web Services Support 示例 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用 Amazon SDK for .NET 的 Amazon Web Services Support 示例

以下代码示例演示了如何将Amazon SDK for .NET 与 Amazon Web Services Support 结合使用,以执行操作和实现常见场景。

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

场景是指显示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

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

开始使用

以下代码示例演示了如何开始使用 Amazon Web Services Support。

Amazon SDK for .NET
注意

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

using Amazon.AWSSupport; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; public static class HelloSupport { static async Task Main(string[] args) { // Use the AWS .NET Core Setup package to set up dependency injection for the AWS Support service. // Use your AWS profile name, or leave it blank to use the default profile. // You must have one of the following AWS Support plans: Business, Enterprise On-Ramp, or Enterprise. Otherwise, an exception will be thrown. using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonAWSSupport>() ).Build(); // Now the client is available for injection. var supportClient = host.Services.GetRequiredService<IAmazonAWSSupport>(); // You can use await and any of the async methods to get a response. var response = await supportClient.DescribeServicesAsync(); Console.WriteLine($"\tHello AWS Support! There are {response.Services.Count} services available."); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DescribeServices中的。

操作

以下代码示例演示了如何将带附件的 Amazon Web Services Support 通信添加到支持案例。

Amazon SDK for .NET
注意

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

/// <summary> /// Add communication to a case, including optional attachment set ID and CC email addresses. /// </summary> /// <param name="caseId">Id for the support case.</param> /// <param name="body">Body text of the communication.</param> /// <param name="attachmentSetId">Optional Id for an attachment set.</param> /// <param name="ccEmailAddresses">Optional list of CC email addresses.</param> /// <returns>True if successful.</returns> public async Task<bool> AddCommunicationToCase(string caseId, string body, string? attachmentSetId = null, List<string>? ccEmailAddresses = null) { var response = await _amazonSupport.AddCommunicationToCaseAsync( new AddCommunicationToCaseRequest() { CaseId = caseId, CommunicationBody = body, AttachmentSetId = attachmentSetId, CcEmailAddresses = ccEmailAddresses }); return response.Result; }

以下代码示例演示了如何将 Amazon Web Services Support 附件添加到附件集。

Amazon SDK for .NET
注意

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

/// <summary> /// Add an attachment to a set, or create a new attachment set if one does not exist. /// </summary> /// <param name="data">The data for the attachment.</param> /// <param name="fileName">The file name for the attachment.</param> /// <param name="attachmentSetId">Optional setId for the attachment. Creates a new attachment set if empty.</param> /// <returns>The setId of the attachment.</returns> public async Task<string> AddAttachmentToSet(MemoryStream data, string fileName, string? attachmentSetId = null) { var response = await _amazonSupport.AddAttachmentsToSetAsync( new AddAttachmentsToSetRequest { AttachmentSetId = attachmentSetId, Attachments = new List<Attachment> { new Attachment { Data = data, FileName = fileName } } }); return response.AttachmentSetId; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考AddAttachmentsToSet中的。

以下代码示例演示了如何创建新的 Amazon Web Services Support 案例。

Amazon SDK for .NET
注意

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

/// <summary> /// Create a new support case. /// </summary> /// <param name="serviceCode">Service code for the new case.</param> /// <param name="categoryCode">Category for the new case.</param> /// <param name="severityCode">Severity code for the new case.</param> /// <param name="subject">Subject of the new case.</param> /// <param name="body">Body text of the new case.</param> /// <param name="language">Optional language support for your case. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <param name="attachmentSetId">Optional Id for an attachment set for the new case.</param> /// <param name="issueType">Optional issue type for the new case. Options are "customer-service" or "technical".</param> /// <returns>The caseId of the new support case.</returns> public async Task<string> CreateCase(string serviceCode, string categoryCode, string severityCode, string subject, string body, string language = "en", string? attachmentSetId = null, string issueType = "customer-service") { var response = await _amazonSupport.CreateCaseAsync( new CreateCaseRequest() { ServiceCode = serviceCode, CategoryCode = categoryCode, SeverityCode = severityCode, Subject = subject, Language = language, AttachmentSetId = attachmentSetId, IssueType = issueType, CommunicationBody = body }); return response.CaseId; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考CreateCase中的。

以下代码示例演示了如何描述 Amazon Web Services Support 案例的附件。

Amazon SDK for .NET
注意

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

/// <summary> /// Get description of a specific attachment. /// </summary> /// <param name="attachmentId">Id of the attachment, usually fetched by describing the communications of a case.</param> /// <returns>The attachment object.</returns> public async Task<Attachment> DescribeAttachment(string attachmentId) { var response = await _amazonSupport.DescribeAttachmentAsync( new DescribeAttachmentRequest() { AttachmentId = attachmentId }); return response.Attachment; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DescribeAttachment中的。

以下代码示例演示了如何描述 Amazon Web Services Support 案例。

Amazon SDK for .NET
注意

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

/// <summary> /// Get case details for a list of case ids, optionally with date filters. /// </summary> /// <param name="caseIds">The list of case IDs.</param> /// <param name="displayId">Optional display ID.</param> /// <param name="includeCommunication">True to include communication. Defaults to true.</param> /// <param name="includeResolvedCases">True to include resolved cases. Defaults to false.</param> /// <param name="afterTime">The optional start date for a filtered search.</param> /// <param name="beforeTime">The optional end date for a filtered search.</param> /// <param name="language">Optional language support for your case. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <returns>A list of CaseDetails.</returns> public async Task<List<CaseDetails>> DescribeCases(List<string> caseIds, string? displayId = null, bool includeCommunication = true, bool includeResolvedCases = false, DateTime? afterTime = null, DateTime? beforeTime = null, string language = "en") { var results = new List<CaseDetails>(); var paginateCases = _amazonSupport.Paginators.DescribeCases( new DescribeCasesRequest() { CaseIdList = caseIds, DisplayId = displayId, IncludeCommunications = includeCommunication, IncludeResolvedCases = includeResolvedCases, AfterTime = afterTime?.ToString("s"), BeforeTime = beforeTime?.ToString("s"), Language = language }); // Get the entire list using the paginator. await foreach (var cases in paginateCases.Cases) { results.Add(cases); } return results; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DescribeCases中的。

以下代码示例演示了如何描述案例的 Amazon Web Services Support 通信。

Amazon SDK for .NET
注意

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

/// <summary> /// Describe the communications for a case, optionally with a date filter. /// </summary> /// <param name="caseId">The ID of the support case.</param> /// <param name="afterTime">The optional start date for a filtered search.</param> /// <param name="beforeTime">The optional end date for a filtered search.</param> /// <returns>The list of communications for the case.</returns> public async Task<List<Communication>> DescribeCommunications(string caseId, DateTime? afterTime = null, DateTime? beforeTime = null) { var results = new List<Communication>(); var paginateCommunications = _amazonSupport.Paginators.DescribeCommunications( new DescribeCommunicationsRequest() { CaseId = caseId, AfterTime = afterTime?.ToString("s"), BeforeTime = beforeTime?.ToString("s") }); // Get the entire list using the paginator. await foreach (var communications in paginateCommunications.Communications) { results.Add(communications); } return results; }

以下代码示例演示如何描述 Amazon 服务列表。

Amazon SDK for .NET
注意

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

/// <summary> /// Get the descriptions of AWS services. /// </summary> /// <param name="name">Optional language for services. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <returns>The list of AWS service descriptions.</returns> public async Task<List<Service>> DescribeServices(string language = "en") { var response = await _amazonSupport.DescribeServicesAsync( new DescribeServicesRequest() { Language = language }); return response.Services; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DescribeServices中的。

以下代码示例演示了如何描述 Amazon Web Services Support 严重性级别。

Amazon SDK for .NET
注意

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

/// <summary> /// Get the descriptions of support severity levels. /// </summary> /// <param name="name">Optional language for severity levels. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <returns>The list of support severity levels.</returns> public async Task<List<SeverityLevel>> DescribeSeverityLevels(string language = "en") { var response = await _amazonSupport.DescribeSeverityLevelsAsync( new DescribeSeverityLevelsRequest() { Language = language }); return response.SeverityLevels; }

以下代码示例演示了如何解析 Amazon Web Services Support 案例。

Amazon SDK for .NET
注意

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

/// <summary> /// Resolve a support case by caseId. /// </summary> /// <param name="caseId">Id for the support case.</param> /// <returns>The final status of the case after resolving.</returns> public async Task<string> ResolveCase(string caseId) { var response = await _amazonSupport.ResolveCaseAsync( new ResolveCaseRequest() { CaseId = caseId }); return response.FinalCaseStatus; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ResolveCase中的。

场景

以下代码示例演示了操作流程:

  • 获取并显示案例的可用服务和严重级别。

  • 使用选定的服务、类别和严重性级别创建支持案例。

  • 获取并显示当天打开案例的列表。

  • 向新案例添加附件集和通信。

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

  • 解析案例。

  • 获取并显示当天未解决的案例列表。

Amazon SDK for .NET
注意

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

在命令提示符中运行交互式场景。

/// <summary> /// Hello AWS Support example. /// </summary> public static class SupportCaseScenario { /* Before running this .NET code example, set up your development environment, including your credentials. To use the AWS Support API, you must have one of the following AWS Support plans: Business, Enterprise On-Ramp, or Enterprise. This .NET example performs the following tasks: 1. Get and display services. Select a service from the list. 2. Select a category from the selected service. 3. Get and display severity levels and select a severity level from the list. 4. Create a support case using the selected service, category, and severity level. 5. Get and display a list of open support cases for the current day. 6. Create an attachment set with a sample text file to add to the case. 7. Add a communication with the attachment to the support case. 8. List the communications of the support case. 9. Describe the attachment set. 10. Resolve the support case. 11. Get a list of resolved cases for the current day. */ private static SupportWrapper _supportWrapper = null!; static async Task Main(string[] args) { // Set up dependency injection for the AWS Support service. // Use your AWS profile name, or leave it blank to use the default profile. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonAWSSupport>(new AWSOptions() { Profile = "default" }) .AddTransient<SupportWrapper>() ) .Build(); var logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger(typeof(SupportCaseScenario)); _supportWrapper = host.Services.GetRequiredService<SupportWrapper>(); Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the AWS Support case example scenario."); Console.WriteLine(new string('-', 80)); try { var apiSupported = await _supportWrapper.VerifySubscription(); if (!apiSupported) { logger.LogError("You must have a Business, Enterprise On-Ramp, or Enterprise Support " + "plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these examples."); return; } var service = await DisplayAndSelectServices(); var category = DisplayAndSelectCategories(service); var severityLevel = await DisplayAndSelectSeverity(); var caseId = await CreateSupportCase(service, category, severityLevel); await DescribeTodayOpenCases(); var attachmentSetId = await CreateAttachmentSet(); await AddCommunicationToCase(attachmentSetId, caseId); var attachmentId = await ListCommunicationsForCase(caseId); await DescribeCaseAttachment(attachmentId); await ResolveCase(caseId); await DescribeTodayResolvedCases(); Console.WriteLine(new string('-', 80)); Console.WriteLine("AWS Support case example scenario complete."); Console.WriteLine(new string('-', 80)); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); } } /// <summary> /// List some available services from AWS Support, and select a service for the example. /// </summary> /// <returns>The selected service.</returns> private static async Task<Service> DisplayAndSelectServices() { Console.WriteLine(new string('-', 80)); var services = await _supportWrapper.DescribeServices(); Console.WriteLine($"AWS Support client returned {services.Count} services."); Console.WriteLine($"1. Displaying first 10 services:"); for (int i = 0; i < 10 && i < services.Count; i++) { Console.WriteLine($"\t{i + 1}. {services[i].Name}"); } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > services.Count) { Console.WriteLine( "Select an example support service by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } Console.WriteLine(new string('-', 80)); return services[choiceNumber - 1]; } /// <summary> /// List the available categories for a service and select a category for the example. /// </summary> /// <param name="service">Service to use for displaying categories.</param> /// <returns>The selected category.</returns> private static Category DisplayAndSelectCategories(Service service) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"2. Available support categories for Service \"{service.Name}\":"); for (int i = 0; i < service.Categories.Count; i++) { Console.WriteLine($"\t{i + 1}. {service.Categories[i].Name}"); } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > service.Categories.Count) { Console.WriteLine( "Select an example support category by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } Console.WriteLine(new string('-', 80)); return service.Categories[choiceNumber - 1]; } /// <summary> /// List available severity levels from AWS Support, and select a level for the example. /// </summary> /// <returns>The selected severity level.</returns> private static async Task<SeverityLevel> DisplayAndSelectSeverity() { Console.WriteLine(new string('-', 80)); var severityLevels = await _supportWrapper.DescribeSeverityLevels(); Console.WriteLine($"3. Get and display available severity levels:"); for (int i = 0; i < 10 && i < severityLevels.Count; i++) { Console.WriteLine($"\t{i + 1}. {severityLevels[i].Name}"); } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > severityLevels.Count) { Console.WriteLine( "Select an example severity level by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } Console.WriteLine(new string('-', 80)); return severityLevels[choiceNumber - 1]; } /// <summary> /// Create an example support case. /// </summary> /// <param name="service">Service to use for the new case.</param> /// <param name="category">Category to use for the new case.</param> /// <param name="severity">Severity to use for the new case.</param> /// <returns>The caseId of the new support case.</returns> private static async Task<string> CreateSupportCase(Service service, Category category, SeverityLevel severity) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"4. Create an example support case" + $" with the following settings:" + $" \n\tService: {service.Name}, Category: {category.Name} " + $"and Severity Level: {severity.Name}."); var caseId = await _supportWrapper.CreateCase(service.Code, category.Code, severity.Code, "Example case for testing, ignore.", "This is my example support case."); Console.WriteLine($"\tNew case created with ID {caseId}"); Console.WriteLine(new string('-', 80)); return caseId; } /// <summary> /// List open cases for the current day. /// </summary> /// <returns>Async task.</returns> private static async Task DescribeTodayOpenCases() { Console.WriteLine($"5. List the open support cases for the current day."); // Describe the cases. If it is empty, try again and allow time for the new case to appear. List<CaseDetails> currentOpenCases = null!; while (currentOpenCases == null || currentOpenCases.Count == 0) { Thread.Sleep(1000); currentOpenCases = await _supportWrapper.DescribeCases( new List<string>(), null, false, false, DateTime.UtcNow.Date, DateTime.UtcNow); } foreach (var openCase in currentOpenCases) { Console.WriteLine($"\tCase: {openCase.CaseId} created {openCase.TimeCreated}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Create an attachment set for a support case. /// </summary> /// <returns>The attachment set id.</returns> private static async Task<string> CreateAttachmentSet() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"6. Create an attachment set for a support case."); var fileName = "example_attachment.txt"; // Create the file if it does not already exist. if (!File.Exists(fileName)) { await using StreamWriter sw = File.CreateText(fileName); await sw.WriteLineAsync( "This is a sample file for attachment to a support case."); } await using var ms = new MemoryStream(await File.ReadAllBytesAsync(fileName)); var attachmentSetId = await _supportWrapper.AddAttachmentToSet( ms, fileName); Console.WriteLine($"\tNew attachment set created with id: \n\t{attachmentSetId.Substring(0, 65)}..."); Console.WriteLine(new string('-', 80)); return attachmentSetId; } /// <summary> /// Add an attachment set and communication to a case. /// </summary> /// <param name="attachmentSetId">Id of the attachment set.</param> /// <param name="caseId">Id of the case to receive the attachment set.</param> /// <returns>Async task.</returns> private static async Task AddCommunicationToCase(string attachmentSetId, string caseId) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"7. Add attachment set and communication to {caseId}."); await _supportWrapper.AddCommunicationToCase( caseId, "This is an example communication added to a support case.", attachmentSetId); Console.WriteLine($"\tNew attachment set and communication added to {caseId}"); Console.WriteLine(new string('-', 80)); } /// <summary> /// List the communications for a case. /// </summary> /// <param name="caseId">Id of the case to describe.</param> /// <returns>An attachment id.</returns> private static async Task<string> ListCommunicationsForCase(string caseId) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"8. List communications for case {caseId}."); var communications = await _supportWrapper.DescribeCommunications(caseId); var attachmentId = ""; foreach (var communication in communications) { Console.WriteLine( $"\tCommunication created on: {communication.TimeCreated} has {communication.AttachmentSet.Count} attachments."); if (communication.AttachmentSet.Any()) { attachmentId = communication.AttachmentSet.First().AttachmentId; } } Console.WriteLine(new string('-', 80)); return attachmentId; } /// <summary> /// Describe an attachment by id. /// </summary> /// <param name="attachmentId">Id of the attachment to describe.</param> /// <returns>Async task.</returns> private static async Task DescribeCaseAttachment(string attachmentId) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"9. Describe the attachment set."); var attachment = await _supportWrapper.DescribeAttachment(attachmentId); var data = Encoding.ASCII.GetString(attachment.Data.ToArray()); Console.WriteLine($"\tAttachment includes {attachment.FileName} with data: \n\t{data}"); Console.WriteLine(new string('-', 80)); } /// <summary> /// Resolve the support case. /// </summary> /// <param name="caseId">Id of the case to resolve.</param> /// <returns>Async task.</returns> private static async Task ResolveCase(string caseId) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"10. Resolve case {caseId}."); var status = await _supportWrapper.ResolveCase(caseId); Console.WriteLine($"\tCase {caseId} has final status {status}"); Console.WriteLine(new string('-', 80)); } /// <summary> /// List resolved cases for the current day. /// </summary> /// <returns>Async Task.</returns> private static async Task DescribeTodayResolvedCases() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"11. List the resolved support cases for the current day."); var currentCases = await _supportWrapper.DescribeCases( new List<string>(), null, false, true, DateTime.UtcNow.Date, DateTime.UtcNow); foreach (var currentCase in currentCases) { if (currentCase.Status == "resolved") { Console.WriteLine( $"\tCase: {currentCase.CaseId}: status {currentCase.Status}"); } } Console.WriteLine(new string('-', 80)); } }

场景用于 Amazon Web Services Support 操作的包装程序方法。

/// <summary> /// Wrapper methods to use AWS Support for working with support cases. /// </summary> public class SupportWrapper { private readonly IAmazonAWSSupport _amazonSupport; public SupportWrapper(IAmazonAWSSupport amazonSupport) { _amazonSupport = amazonSupport; } /// <summary> /// Get the descriptions of AWS services. /// </summary> /// <param name="name">Optional language for services. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <returns>The list of AWS service descriptions.</returns> public async Task<List<Service>> DescribeServices(string language = "en") { var response = await _amazonSupport.DescribeServicesAsync( new DescribeServicesRequest() { Language = language }); return response.Services; } /// <summary> /// Get the descriptions of support severity levels. /// </summary> /// <param name="name">Optional language for severity levels. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <returns>The list of support severity levels.</returns> public async Task<List<SeverityLevel>> DescribeSeverityLevels(string language = "en") { var response = await _amazonSupport.DescribeSeverityLevelsAsync( new DescribeSeverityLevelsRequest() { Language = language }); return response.SeverityLevels; } /// <summary> /// Create a new support case. /// </summary> /// <param name="serviceCode">Service code for the new case.</param> /// <param name="categoryCode">Category for the new case.</param> /// <param name="severityCode">Severity code for the new case.</param> /// <param name="subject">Subject of the new case.</param> /// <param name="body">Body text of the new case.</param> /// <param name="language">Optional language support for your case. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <param name="attachmentSetId">Optional Id for an attachment set for the new case.</param> /// <param name="issueType">Optional issue type for the new case. Options are "customer-service" or "technical".</param> /// <returns>The caseId of the new support case.</returns> public async Task<string> CreateCase(string serviceCode, string categoryCode, string severityCode, string subject, string body, string language = "en", string? attachmentSetId = null, string issueType = "customer-service") { var response = await _amazonSupport.CreateCaseAsync( new CreateCaseRequest() { ServiceCode = serviceCode, CategoryCode = categoryCode, SeverityCode = severityCode, Subject = subject, Language = language, AttachmentSetId = attachmentSetId, IssueType = issueType, CommunicationBody = body }); return response.CaseId; } /// <summary> /// Add an attachment to a set, or create a new attachment set if one does not exist. /// </summary> /// <param name="data">The data for the attachment.</param> /// <param name="fileName">The file name for the attachment.</param> /// <param name="attachmentSetId">Optional setId for the attachment. Creates a new attachment set if empty.</param> /// <returns>The setId of the attachment.</returns> public async Task<string> AddAttachmentToSet(MemoryStream data, string fileName, string? attachmentSetId = null) { var response = await _amazonSupport.AddAttachmentsToSetAsync( new AddAttachmentsToSetRequest { AttachmentSetId = attachmentSetId, Attachments = new List<Attachment> { new Attachment { Data = data, FileName = fileName } } }); return response.AttachmentSetId; } /// <summary> /// Get description of a specific attachment. /// </summary> /// <param name="attachmentId">Id of the attachment, usually fetched by describing the communications of a case.</param> /// <returns>The attachment object.</returns> public async Task<Attachment> DescribeAttachment(string attachmentId) { var response = await _amazonSupport.DescribeAttachmentAsync( new DescribeAttachmentRequest() { AttachmentId = attachmentId }); return response.Attachment; } /// <summary> /// Add communication to a case, including optional attachment set ID and CC email addresses. /// </summary> /// <param name="caseId">Id for the support case.</param> /// <param name="body">Body text of the communication.</param> /// <param name="attachmentSetId">Optional Id for an attachment set.</param> /// <param name="ccEmailAddresses">Optional list of CC email addresses.</param> /// <returns>True if successful.</returns> public async Task<bool> AddCommunicationToCase(string caseId, string body, string? attachmentSetId = null, List<string>? ccEmailAddresses = null) { var response = await _amazonSupport.AddCommunicationToCaseAsync( new AddCommunicationToCaseRequest() { CaseId = caseId, CommunicationBody = body, AttachmentSetId = attachmentSetId, CcEmailAddresses = ccEmailAddresses }); return response.Result; } /// <summary> /// Describe the communications for a case, optionally with a date filter. /// </summary> /// <param name="caseId">The ID of the support case.</param> /// <param name="afterTime">The optional start date for a filtered search.</param> /// <param name="beforeTime">The optional end date for a filtered search.</param> /// <returns>The list of communications for the case.</returns> public async Task<List<Communication>> DescribeCommunications(string caseId, DateTime? afterTime = null, DateTime? beforeTime = null) { var results = new List<Communication>(); var paginateCommunications = _amazonSupport.Paginators.DescribeCommunications( new DescribeCommunicationsRequest() { CaseId = caseId, AfterTime = afterTime?.ToString("s"), BeforeTime = beforeTime?.ToString("s") }); // Get the entire list using the paginator. await foreach (var communications in paginateCommunications.Communications) { results.Add(communications); } return results; } /// <summary> /// Get case details for a list of case ids, optionally with date filters. /// </summary> /// <param name="caseIds">The list of case IDs.</param> /// <param name="displayId">Optional display ID.</param> /// <param name="includeCommunication">True to include communication. Defaults to true.</param> /// <param name="includeResolvedCases">True to include resolved cases. Defaults to false.</param> /// <param name="afterTime">The optional start date for a filtered search.</param> /// <param name="beforeTime">The optional end date for a filtered search.</param> /// <param name="language">Optional language support for your case. /// Currently "en" (English) and "ja" (Japanese) are supported.</param> /// <returns>A list of CaseDetails.</returns> public async Task<List<CaseDetails>> DescribeCases(List<string> caseIds, string? displayId = null, bool includeCommunication = true, bool includeResolvedCases = false, DateTime? afterTime = null, DateTime? beforeTime = null, string language = "en") { var results = new List<CaseDetails>(); var paginateCases = _amazonSupport.Paginators.DescribeCases( new DescribeCasesRequest() { CaseIdList = caseIds, DisplayId = displayId, IncludeCommunications = includeCommunication, IncludeResolvedCases = includeResolvedCases, AfterTime = afterTime?.ToString("s"), BeforeTime = beforeTime?.ToString("s"), Language = language }); // Get the entire list using the paginator. await foreach (var cases in paginateCases.Cases) { results.Add(cases); } return results; } /// <summary> /// Resolve a support case by caseId. /// </summary> /// <param name="caseId">Id for the support case.</param> /// <returns>The final status of the case after resolving.</returns> public async Task<string> ResolveCase(string caseId) { var response = await _amazonSupport.ResolveCaseAsync( new ResolveCaseRequest() { CaseId = caseId }); return response.FinalCaseStatus; } /// <summary> /// Verify the support level for AWS Support API access. /// </summary> /// <returns>True if the subscription level supports API access.</returns> public async Task<bool> VerifySubscription() { try { var response = await _amazonSupport.DescribeServicesAsync( new DescribeServicesRequest() { Language = "en" }); return response.HttpStatusCode == HttpStatusCode.OK; } catch (Amazon.AWSSupport.AmazonAWSSupportException ex) { if (ex.ErrorCode == "SubscriptionRequiredException") { return false; } else throw; } } }