Skip to content

Commit

Permalink
update host uri and add project apikind
Browse files Browse the repository at this point in the history
  • Loading branch information
yeyuanh authored and boltomli committed May 12, 2020
1 parent 2ddde58 commit cb3edcc
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/API/API_V3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ public class API_V3
{
private static string TextToSpeechBasePath_V3_beta1 => "/api/texttospeech/v3.0-beta1/";

//Voice Project
private static string VoiceProject_Base => TextToSpeechBasePath_V3_beta1 + "projects";
public static string VoiceProject_Get => VoiceProject_Base;
public static string VoiceProject_Create => VoiceProject_Base;
public static string VoiceProject_DeleteById => VoiceProject_Base + "/{0}";

//Voice Datasets
private static string VoiceDataset_Base => TextToSpeechBasePath_V3_beta1 + "datasets";
public static string VoiceDatasets_Get => VoiceDataset_Base;
Expand Down
63 changes: 63 additions & 0 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/API/DTO/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace CustomVoice_API.API.DTO
{
class Project
{
[JsonConstructor]
private Project(
string self,
string projectKind,
string displayName,
string description,
IReadOnlyDictionary<string, string> properties,
CultureInfo locale,
DateTime createdDateTime)
{
this.Self = self;
this.Properties = properties;
this.DisplayName = displayName;
this.Description = description;
this.ProjectKind = projectKind;
this.Locale = locale.Name;
this.CreatedDateTime = createdDateTime;
}

public string Self { get; private set; }

public string ProjectKind { get; private set; }

public string DisplayName { get; private set; }

public string Description { get; private set; }

public IReadOnlyDictionary<string, string> Properties { get; private set; }

public string Locale { get; private set; }

public DateTime CreatedDateTime { get; private set; }

public static Project Create(
string self,
string projectKind,
string name,
string description,
IReadOnlyDictionary<string, string> properties,
CultureInfo locale,
DateTime createdDateTime,
OneApiState status)
{
return new Project(
self,
projectKind,
name,
description,
properties,
locale,
createdDateTime);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;

namespace CustomVoice_API.API.DTO
{
class ProjectDefinition
{
private ProjectDefinition(
string name,
string displayName,
string description,
string locale,
IReadOnlyDictionary<string, string> properties,
string projectKind)
{
this.Name = name;
this.DisplayName = displayName;
this.Description = description;
this.Locale = locale;
this.Properties = properties;
this.ProjectKind = projectKind;
}

public string Name { get; private set; }

public string DisplayName { get; private set; }

public string Description { get; private set; }

public string Locale { get; private set; }

public IReadOnlyDictionary<string, string> Properties { get; private set; }

public string ProjectKind { get; private set; }

public static ProjectDefinition Create(
string name,
string displayname,
string description,
string locale,
IReadOnlyDictionary<string, string> properties,
string projectKind)
{
return new ProjectDefinition(
name,
displayname,
description,
locale,
properties,
projectKind);
}
}
}
52 changes: 52 additions & 0 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/API/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using CustomVoice_API.API.DTO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;

namespace CustomVoice_API.API
{
class Project
{
public static IEnumerable<DTO.Project> Get(string subscriptionKey, string hostURI)
{
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceProject_Get);
return APIHelper.Get<IEnumerable<DTO.Project>>(subscriptionKey, url);
}

public static bool DeleteById(string subscriptionKey, string hostURI, string projectId)
{
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceProject_DeleteById, projectId);
var response = APIHelper.Delete(subscriptionKey, url);
if (response.StatusCode != HttpStatusCode.NoContent)
{
APIHelper.PrintErrorMessage(response);
return false;
}

return true;
}

public static bool Create(string subscriptionKey, string hostURI, string name, string description, string gender, string locale)
{
var properties = new Dictionary<string, string>();
properties.Add("Gender", gender.Substring(0, 1).ToUpper() + gender.Substring(1));

var projectDefinition = ProjectDefinition.Create(
name,
name,
description,
locale,
properties,
"TextToSpeech");
var response = APIHelper.Submit<ProjectDefinition>(subscriptionKey, hostURI + API_V3.VoiceProject_Create, projectDefinition);

if (response.StatusCode != HttpStatusCode.Accepted && response.StatusCode != HttpStatusCode.Created)
{
APIHelper.PrintErrorMessage(response);
return false;
}
return true;
}
}
}
29 changes: 24 additions & 5 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/APIArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class APIArguments
internal const string EndpointId = "endpointId";
internal const string BatchSynthesisId = "batchSynthesisId";
internal const string VoiceTestId = "voiceTestId";
internal const string HostUriValue = "https://Westus.customvoice.api.speech.microsoft.com/";

public static Dictionary<string, string> GetApiKindAndAction(string[] args)
{
Expand Down Expand Up @@ -49,7 +50,7 @@ public static Dictionary<string, string> GetApiKindAndAction(string[] args)

public static Dictionary<string, string> GetArguments(string[] args)
{
if(args.Length <= 0)
if (args.Length <= 0)
{
return null;
}
Expand Down Expand Up @@ -94,22 +95,40 @@ public static bool NoAction(Dictionary<string, string> ApiKindAndAction)

public static bool ParametersNoMatch(Dictionary<string, string> arguments, List<string> requiredParameters)
{
if(requiredParameters.Except(arguments.Keys).Count() > 0)
if (requiredParameters.Except(arguments.Keys).Count() > 0)
{
return true;
}

return false;
}

public static Dictionary<string, List<string>> GetParameters(APIKind apiKind, Action action )
public static Dictionary<string, List<string>> GetParameters(APIKind apiKind, Action action)
{
Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
List<string> RequiredParameters = null;
List<string> OptionalParameters = null;

switch ($"{apiKind}-{action}")
{
case nameof(APIKind.project) + "-" + nameof(Action.create):
{
RequiredParameters = new List<string>() { SubscriptionKey, HostUri, Name, Gender, Locale };
OptionalParameters = new List<string>() { Description };
break;
}
case nameof(APIKind.project) + "-" + nameof(Action.get):
{
RequiredParameters = new List<string>() { SubscriptionKey, HostUri };
OptionalParameters = new List<string>();
break;
}
case nameof(APIKind.project) + "-" + nameof(Action.delete):
{
RequiredParameters = new List<string>() { SubscriptionKey, HostUri, ProjectId };
OptionalParameters = new List<string>();
break;
}
case nameof(APIKind.dataset) + "-" + nameof(Action.uploaddataset):
{
RequiredParameters = new List<string>() { SubscriptionKey, HostUri, Name, ProjectId, Gender, Locale, WavePath, ScriptPath };
Expand Down Expand Up @@ -178,8 +197,8 @@ public static Dictionary<string, List<string>> GetParameters(APIKind apiKind, Ac
}
case nameof(APIKind.voicetest) + "-" + nameof(Action.create):
{
RequiredParameters = new List<string>() { SubscriptionKey, HostUri, ProjectId, ModelId, "script"};
OptionalParameters = new List<string>() { "isSSML"};
RequiredParameters = new List<string>() { SubscriptionKey, HostUri, ProjectId, ModelId, "script" };
OptionalParameters = new List<string>() { "isSSML" };
break;
}
case nameof(APIKind.voicetest) + "-" + nameof(Action.get):
Expand Down
70 changes: 70 additions & 0 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/APIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public static void ExecuteApi(APIKind apiKind, Action action, Dictionary<string,
{
switch (apiKind)
{
case APIKind.project:
ExecuteProjectApi(action, arguments);
break;
case APIKind.dataset:
ExecuteDatasetApi(action, arguments);
break;
Expand All @@ -41,6 +44,24 @@ public static void ExecuteApi(APIKind apiKind, Action action, Dictionary<string,
}
}

private static void ExecuteProjectApi(Action action, Dictionary<string, string> arguments)
{
switch (action)
{
case Action.get:
ProjectGet(arguments);
break;
case Action.delete:
ProjectDeleteById(arguments);
break;
case Action.create:
ProjectCreate(arguments);
break;
default:
break;
}
}

private static void ExecuteDatasetApi(Action action, Dictionary<string, string> arguments)
{
switch (action)
Expand Down Expand Up @@ -164,6 +185,55 @@ private static void ExecuteBatchSynthesisApi(Action action, Dictionary<string, s
}
}

private static void ProjectGet(Dictionary<string, string> arguments)
{
string subscriptionKey = arguments["subscriptionkey"];
string hostURI = arguments["hosturi"];

var result = Project.Get(subscriptionKey, hostURI);
DisplayResult<API.DTO.Project>(result);
}

private static void ProjectDeleteById(Dictionary<string, string> arguments)
{
string subscriptionKey = arguments["subscriptionkey"];
string hostURI = arguments["hosturi"];
string projectId = arguments["projectid"];

if (Project.DeleteById(subscriptionKey, hostURI, projectId))
{
Console.WriteLine("Delete project successfully");
}
else
{
Console.WriteLine("Delete project failed");
}
}

private static void ProjectCreate(Dictionary<string, string> arguments)
{
string subscriptionKey = arguments["subscriptionkey"];
string hostURI = arguments["hosturi"];
string name = arguments["name"];
string gender = arguments["gender"];
string locale = arguments["locale"];
string description = name;

if (arguments.Keys.ToList().Contains("description"))
{
description = arguments["description"];
}

if (Project.Create(subscriptionKey, hostURI, name, description, gender, locale))
{
Console.WriteLine("Create project successfully");
}
else
{
Console.WriteLine("Create project failed");
}
}

private static void DatasetGet(Dictionary<string, string> arguments)
{
string subscriptionKey = arguments["subscriptionkey"];
Expand Down
1 change: 1 addition & 0 deletions CustomVoice-API-Samples/CSharp/CustomVoice-API/APIKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum APIKind
{
project,
dataset,
model,
voicetest,
Expand Down
Loading

0 comments on commit cb3edcc

Please sign in to comment.