AWS Support 使用範例 AWS SDK for .NET - AWS SDK 程式碼範例

AWS 文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例

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

AWS Support 使用範例 AWS SDK for .NET

下列程式碼範例說明如何使用 AWS SDK for .NET 與來執行動作及實作常見案例 AWS Support。

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

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

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

開始使用

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

AWS SDK for .NET
注意

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

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 詳細資訊,請參閱 AWS SDK for .NET API 參考DescribeServices中的。

動作

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

AWS SDK for .NET
注意

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

/// <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; }

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

AWS SDK for .NET
注意

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

/// <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; }

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

AWS SDK for .NET
注意

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

/// <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 Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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 詳細資訊,請參閱 AWS SDK for .NET API 參考CreateCase中的。

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

AWS SDK for .NET
注意

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

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考DescribeAttachment中的。

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

AWS SDK for .NET
注意

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

/// <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 Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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 詳細資訊,請參閱 AWS SDK for .NET API 參考DescribeCases中的。

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

AWS SDK for .NET
注意

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

/// <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; }

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

AWS SDK for .NET
注意

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

/// <summary> /// Get the descriptions of AWS services. /// </summary> /// <param name="name">Optional language for services. /// Currently Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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 詳細資訊,請參閱 AWS SDK for .NET API 參考DescribeServices中的。

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

AWS SDK for .NET
注意

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

/// <summary> /// Get the descriptions of support severity levels. /// </summary> /// <param name="name">Optional language for severity levels. /// Currently Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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; }

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

AWS SDK for .NET
注意

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

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考ResolveCase中的。

案例

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

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

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

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

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

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

  • 解決案例。

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

AWS SDK for .NET
注意

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

在命令提示中執行互動式案例。

/// <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)); } }

案例用於 AWS 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 Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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 Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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 Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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 Chinese (“zh”), English ("en"), Japanese ("ja") and Korean (“ko”) 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; } } }