From a81fc1b5cb20dc7dbc4167aac189167623026165 Mon Sep 17 00:00:00 2001 From: Joe He Date: Fri, 18 Oct 2019 17:44:24 +0800 Subject: [PATCH] Refine C# sample for batch API: (#143) Add GetSynthesisByID method. Add SSML sample text in java project. --- .../CSharp/CustomVoice-API/API/API_V3.cs | 2 +- .../CustomVoice-API/API/BatchSynthesis.cs | 40 +++++++++++++-- .../CSharp/CustomVoice-API/APIArguments.cs | 6 +++ .../CSharp/CustomVoice-API/APIHandler.cs | 20 ++++++-- .../CSharp/CustomVoice-API/Action.cs | 1 + .../CustomVoice-API/ArgumentsDescription.cs | 8 +++ .../Java/SSMLTextInputSample.txt | 50 +++++++++++++++++++ 7 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 CustomVoice-API-Samples/Java/SSMLTextInputSample.txt diff --git a/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/API_V3.cs b/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/API_V3.cs index 116f0dae..4aec45ef 100644 --- a/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/API_V3.cs +++ b/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/API_V3.cs @@ -47,7 +47,7 @@ public class API_V3 private static string VoiceSynthesis_Base => TextToSpeechBasePath_V3_beta1 + "voicesynthesis"; public static string VoiceSynthesis_Get => VoiceSynthesis_Base; public static string VoiceSynthesis_Create => VoiceSynthesis_Base; - public static string VoiceSynthesis_DeleteById => VoiceSynthesis_Base + "/{0}"; + public static string VoiceSynthesis_ById => VoiceSynthesis_Base + "/{0}"; public static string VoiceSynthesis_GetVoice => VoiceSynthesis_Base + "/voices"; } } diff --git a/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/BatchSynthesis.cs b/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/BatchSynthesis.cs index c35eeb4e..3d205d49 100644 --- a/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/BatchSynthesis.cs +++ b/CustomVoice-API-Samples/CSharp/CustomVoice-API/API/BatchSynthesis.cs @@ -4,19 +4,29 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Net; using System.Net.Http; +using System.Threading.Tasks; namespace CustomVoice_API.API { class BatchSynthesis { + private const string OneAPIOperationLocationHeaderKey = "Operation-Location"; + public static IEnumerable Get(string subscriptionKey, string hostURI) { string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_Get); return APIHelper.Get>(subscriptionKey, url); } + public static DTO.BatchSynthesis GetById(string subscriptionKey, string hostURI, string batchSynthesisId) + { + string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_ById, batchSynthesisId); + return APIHelper.Get(subscriptionKey, url); + } + public static IEnumerable Getvoices(string subscriptionKey, string hostURI) { string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_GetVoice); @@ -25,7 +35,7 @@ class BatchSynthesis public static bool DeleteById(string subscriptionKey, string hostURI, string batchSynthesisId) { - string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_DeleteById, batchSynthesisId); + string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_ById, batchSynthesisId); var response = APIHelper.Delete(subscriptionKey, url); if (response.StatusCode != HttpStatusCode.NoContent) { @@ -36,7 +46,7 @@ public static bool DeleteById(string subscriptionKey, string hostURI, string bat return true; } - public static bool Create(string subscriptionKey, string hostURI, string name, string description, + public static string Create(string subscriptionKey, string hostURI, string name, string description, string inputTextPath, string locale, IEnumerable models, string outputFormat, bool isConcatenateResult) { var properties = new Dictionary(); @@ -56,7 +66,7 @@ public static bool Create(string subscriptionKey, string hostURI, string name, s return Create(subscriptionKey, hostURI, batchSynthesisDefinition); } - private static bool Create(string subscriptionKey, string hostURI, BatchSynthesisDefinition batchSynthesisDefinition) + private static string Create(string subscriptionKey, string hostURI, BatchSynthesisDefinition batchSynthesisDefinition) { string scriptName = Path.GetFileName(batchSynthesisDefinition.InputTextPath); @@ -98,10 +108,30 @@ private static bool Create(string subscriptionKey, string hostURI, BatchSynthesi if (response.StatusCode != HttpStatusCode.Accepted) { APIHelper.PrintErrorMessage(response); - return false; + return null; } - return true; + + var returnUrl = GetLocationFromPostResponse(response); + if (returnUrl != null) + { + return new Guid(returnUrl.ToString().Split('/').LastOrDefault()).ToString(); + } + return null; } } + + private static Uri GetLocationFromPostResponse(HttpResponseMessage response) + { + IEnumerable headerValues; + if (response.Headers.TryGetValues(OneAPIOperationLocationHeaderKey, out headerValues)) + { + if (headerValues.Any()) + { + return new Uri(headerValues.First()); + } + } + + return response.Headers.Location; + } } } diff --git a/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIArguments.cs b/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIArguments.cs index fc746ae7..91f521ee 100644 --- a/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIArguments.cs +++ b/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIArguments.cs @@ -214,6 +214,12 @@ public static Dictionary> GetParameters(APIKind apiKind, Ac OptionalParameters = new List(); break; } + case nameof(APIKind.batchsynthesis) + "-" + nameof(Action.getbysynthesisid): + { + RequiredParameters = new List() { "subscriptionKey", "hostURI", "batchSynthesisId" }; + OptionalParameters = new List(); + break; + } case nameof(APIKind.endpoint) + "-" + nameof(Action.delete): { RequiredParameters = new List() { "subscriptionKey", "hostURI", "endpointId" }; diff --git a/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIHandler.cs b/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIHandler.cs index 69d43369..cdf7ae4e 100644 --- a/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIHandler.cs +++ b/CustomVoice-API-Samples/CSharp/CustomVoice-API/APIHandler.cs @@ -155,6 +155,9 @@ private static void ExecuteBatchSynthesisApi(Action action, Dictionary arguments) DisplayResult(result); } + private static void BatchSynthesisGetById(Dictionary arguments) + { + string subscriptionKey = arguments["subscriptionkey"]; + string hostURI = arguments["hosturi"]; + string batchSynthesisId = arguments["batchsynthesisid"]; + + var result = BatchSynthesis.GetById(subscriptionKey, hostURI, batchSynthesisId); + DisplaySingleResult(result, " "); + } + private static void BatchSynthesisGetvoices(Dictionary arguments) { string subscriptionKey = arguments["subscriptionkey"]; @@ -608,13 +621,14 @@ private static void BatchSynthesisCreate(Dictionary arguments) } var modelsList = new List(models.Split(';')).Select(x => new Guid(x)).ToList(); - if (BatchSynthesis.Create(subscriptionKey, hostURI, name, description, inputTextPath, locale, modelsList, outputFormat, isConcatenateResult)) + var synthesisId = BatchSynthesis.Create(subscriptionKey, hostURI, name, description, inputTextPath, locale, modelsList, outputFormat, isConcatenateResult); + if (string.IsNullOrEmpty(synthesisId)) { - Console.WriteLine("Create batch synthesis successfully"); + Console.WriteLine("Create batch synthesis failed"); } else { - Console.WriteLine("Create batch synthesis failed"); + Console.WriteLine($"Create batch synthesis successfully, ID : {synthesisId}"); } } diff --git a/CustomVoice-API-Samples/CSharp/CustomVoice-API/Action.cs b/CustomVoice-API-Samples/CSharp/CustomVoice-API/Action.cs index 2980ab33..194567f7 100644 --- a/CustomVoice-API-Samples/CSharp/CustomVoice-API/Action.cs +++ b/CustomVoice-API-Samples/CSharp/CustomVoice-API/Action.cs @@ -4,6 +4,7 @@ public enum Action { get, getbyprojectid, + getbysynthesisid, create, delete, uploaddataset, diff --git a/CustomVoice-API-Samples/CSharp/CustomVoice-API/ArgumentsDescription.cs b/CustomVoice-API-Samples/CSharp/CustomVoice-API/ArgumentsDescription.cs index 6f323c6f..d967fd79 100644 --- a/CustomVoice-API-Samples/CSharp/CustomVoice-API/ArgumentsDescription.cs +++ b/CustomVoice-API-Samples/CSharp/CustomVoice-API/ArgumentsDescription.cs @@ -144,6 +144,8 @@ public static void PrintAPIKindUsage(APIKind apiKind) Console.WriteLine("--action"); Console.WriteLine(" Get"); Console.WriteLine(" Gets a list of voice synthesis under the selected subscription."); + Console.WriteLine(" GetBySynthesisId"); + Console.WriteLine(" Gets a voice synthesis under the selected subscription with specific ID."); Console.WriteLine(" GetVoices"); Console.WriteLine(" Gets a list of supported voices for offline synthesis."); Console.WriteLine(" Create"); @@ -384,6 +386,12 @@ public static void PrintBatchSynthesisActionUsage(Action action, DictionaryThis is the waistline , and it's falling . +Alexis , meet Bill and Hillary , and the rest of America . +This is Jordan , Scottie Pippen and the ring dynasty . +The more I looked , the gloomier I got . +He saw Macbeth , the three witches , and the boiling cauldron . +But it is good to get together , cook together , eat together and enjoy . +The priorities , they say , are sofa , blanket , and clothing . +The metallic walls curve , twist , and turn . +The seagrass fiber is tough , durable and smooth . +The dissenters were Stevens , Souter , Ginsburg and Breyer . +They're also seeing Casey Martin , the golfer . +Lawrence Weschler , staff writer , The New Yorker . +And Ronaldo has his dedications to fulfill . +It's very troubling to me and it should be very troubling to all Americans . +He has been superb , and nothing changed early in the game . +The smell of gunsmoke and excrement drifted on the air . +The dissenters were Breyer , Stevens , Ginsburg and Souter . +These include fur hats , steel helmets , caps and other badges , and watches . +The background music and the special decor complete the Latin theme . +The resident female retorts in kind , piping and pointing . +I take one pillowcase , and stuff the other pillowcase and both sheets into it . +The same two crops , and blueberries , are threatened in Georgia . +The coastline boasts cliffs , caves , bays , and beaches . +The Bash began in baseball , and it went everywhere . +Less fit letters are not forwarded , and so die . +The hijackers had gained entry to the cockpit , and she did not know how . +They engage in all activities . +It's miserable , the cigarette business . +I think it is ridiculous , and I think it's unfair , and I think it's offensive . +People are born , live , and die on the sidewalks . +I'm enchanted by the tenants of Buddhism , like reincarnation and the development of the mind . +Like other visitors , he is a believer . +The biggest winners are the airlines . +He controls references to time , place , and location . +The casino is in a jurisdiction that allows it , and the money is in a jurisdiction that allows it . +But his weight lifting , bicycling and horseback riding have kept him taut and ready for all comers . +It's gross during the summer . +Sometimes the bodies are never identified . +It was Enron, WorldCom and Global Crossing . +The rotation is solid , defense is solid , the lineup's solid , the bullpen's solid . +That helped push crude oil prices higher . +The farm is in the Catskills , near Worcester , New York . +No , none whatsoever , and that shopkeeper was right . +She knows everything and everyone in the town , literally . +In response , a rock grew taller , pushing the children higher , and higher . +The industry has been cannibalizing itself . +The chiffon stretch , the chiffon silk , wool . +We thank you for your consideration , and look forward to hearing from you . +Like Philip Barry , Mike , the reporter , is an outsider . +The language in the settlement is unacceptable . \ No newline at end of file