Skip to content

Commit

Permalink
Refine C# sample for batch API: (#143)
Browse files Browse the repository at this point in the history
Add GetSynthesisByID method.

Add SSML sample text in java project.
  • Loading branch information
hepower authored and boltomli committed Oct 18, 2019
1 parent f34faac commit a81fc1b
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DTO.BatchSynthesis> Get(string subscriptionKey, string hostURI)
{
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_Get);
return APIHelper.Get<IEnumerable<DTO.BatchSynthesis>>(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<DTO.BatchSynthesis>(subscriptionKey, url);
}

public static IEnumerable<DTO.Voice> Getvoices(string subscriptionKey, string hostURI)
{
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_GetVoice);
Expand All @@ -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)
{
Expand All @@ -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<Guid> models, string outputFormat, bool isConcatenateResult)
{
var properties = new Dictionary<string, string>();
Expand All @@ -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);

Expand Down Expand Up @@ -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<string> headerValues;
if (response.Headers.TryGetValues(OneAPIOperationLocationHeaderKey, out headerValues))
{
if (headerValues.Any())
{
return new Uri(headerValues.First());
}
}

return response.Headers.Location;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ public static Dictionary<string, List<string>> GetParameters(APIKind apiKind, Ac
OptionalParameters = new List<string>();
break;
}
case nameof(APIKind.batchsynthesis) + "-" + nameof(Action.getbysynthesisid):
{
RequiredParameters = new List<string>() { "subscriptionKey", "hostURI", "batchSynthesisId" };
OptionalParameters = new List<string>();
break;
}
case nameof(APIKind.endpoint) + "-" + nameof(Action.delete):
{
RequiredParameters = new List<string>() { "subscriptionKey", "hostURI", "endpointId" };
Expand Down
20 changes: 17 additions & 3 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/APIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ private static void ExecuteBatchSynthesisApi(Action action, Dictionary<string, s
case Action.get:
BatchSynthesisGet(arguments);
break;
case Action.getbysynthesisid:
BatchSynthesisGetById(arguments);
break;
case Action.getvoices:
BatchSynthesisGetvoices(arguments);
break;
Expand Down Expand Up @@ -555,6 +558,16 @@ private static void BatchSynthesisGet(Dictionary<string, string> arguments)
DisplayResult<API.DTO.BatchSynthesis>(result);
}

private static void BatchSynthesisGetById(Dictionary<string, string> 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<string, string> arguments)
{
string subscriptionKey = arguments["subscriptionkey"];
Expand Down Expand Up @@ -608,13 +621,14 @@ private static void BatchSynthesisCreate(Dictionary<string, string> arguments)
}

var modelsList = new List<string>(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}");
}
}

Expand Down
1 change: 1 addition & 0 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum Action
{
get,
getbyprojectid,
getbysynthesisid,
create,
delete,
uploaddataset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -384,6 +386,12 @@ public static void PrintBatchSynthesisActionUsage(Action action, Dictionary<stri
sampleCommand = "CustomVoice-API batchsynthesis get subscriptionKey [YourSubscriptionKey] hostURI https://Westus.cris.ai/";
PrintActionUsageBase(actionString, description, sampleCommand, parameters);
break;
case Action.getbysynthesisid:
actionString = "batchsynthesis getbysynthesisid";
description = "Gets a voice synthesis under the selected subscription by specific ID.";
sampleCommand = "CustomVoice-API batchsynthesis getbysynthesisid subscriptionKey [YourSubscriptionKey] hostURI https://Westus.cris.ai/ batchSynthesisId [BatchSynthesisId]";
PrintActionUsageBase(actionString, description, sampleCommand, parameters);
break;
case Action.getvoices:
actionString = "batchsynthesis getvoice";
description = "Gets a list of supported voices for offline synthesis.";
Expand Down
Loading

0 comments on commit a81fc1b

Please sign in to comment.