Skip to content

Commit b55edf0

Browse files
authored
[Custom Voice] Support to add additional request headers for long audio API. (#183)
1 parent fb91fb8 commit b55edf0

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

CustomVoice-API-Samples/CSharp/CustomVoice-API/API/APIHelper.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ public static T Get<T>(string subscriptionKey, string url)
3636
}
3737
}
3838

39+
public static T Get<T>(string subscriptionKey, string url, Dictionary<string, string> additionalRequestHeaders)
40+
{
41+
using (var client = new HttpClient())
42+
{
43+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
44+
foreach (var pair in additionalRequestHeaders)
45+
{
46+
client.DefaultRequestHeaders.Add(pair.Key, pair.Value);
47+
}
48+
49+
var response = client.GetAsync(url, CancellationToken.None).Result;
50+
51+
if (response.StatusCode != HttpStatusCode.OK)
52+
{
53+
PrintErrorMessage(response);
54+
return default(T);
55+
}
56+
57+
using (var responseStream = response.Content.ReadAsStreamAsync().Result)
58+
using (var streamReader = new StreamReader(responseStream))
59+
{
60+
string responseJson = streamReader.ReadToEnd();
61+
var items = JsonConvert.DeserializeObject<T>(responseJson);
62+
return items;
63+
}
64+
}
65+
}
66+
3967
public static IEnumerable<T> GetListPaged<T>(string subscriptionKey, string url)
4068
{
4169
using (var client = new HttpClient())
@@ -84,8 +112,9 @@ public static HttpResponseMessage Patch<T>(string subscriptionKey, string url, T
84112
{
85113
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
86114
var method = new HttpMethod("PATCH");
87-
using (var request = new HttpRequestMessage(method, url) {
88-
Content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")
115+
using (var request = new HttpRequestMessage(method, url)
116+
{
117+
Content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")
89118
})
90119
{
91120
return client.SendAsync(request).Result;

CustomVoice-API-Samples/CSharp/CustomVoice-API/API/BatchSynthesis.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public static DTO.BatchSynthesis GetById(string subscriptionKey, string hostURI,
5555
return APIHelper.Get<DTO.BatchSynthesis>(subscriptionKey, url);
5656
}
5757

58-
public static IEnumerable<DTO.Voice> Getvoices(string subscriptionKey, string hostURI)
58+
public static IEnumerable<DTO.Voice> Getvoices(string subscriptionKey, string hostURI, Dictionary<string, string> additionalRequestHeaders)
5959
{
6060
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_GetVoice);
61-
return APIHelper.Get<IEnumerable<DTO.Voice>>(subscriptionKey, url);
61+
return APIHelper.Get<IEnumerable<DTO.Voice>>(subscriptionKey, url, additionalRequestHeaders);
6262
}
6363

6464
public static bool DeleteById(string subscriptionKey, string hostURI, string batchSynthesisId)

CustomVoice-API-Samples/CSharp/CustomVoice-API/APIArguments.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class APIArguments
2222
internal const string BatchSynthesisId = "batchSynthesisId";
2323
internal const string VoiceTestId = "voiceTestId";
2424
internal const string HostUriValue = "https://<region>.customvoice.api.speech.microsoft.com/";
25+
internal const string AdditionalRequestHeaders = "additionalRequestHeaders";
2526

2627
public static Dictionary<string, string> GetApiKindAndAction(string[] args)
2728
{
@@ -270,7 +271,7 @@ public static Dictionary<string, List<string>> GetParameters(APIKind apiKind, Ac
270271
case nameof(APIKind.batchsynthesis) + "-" + nameof(Action.getvoices):
271272
{
272273
RequiredParameters = new List<string>() { SubscriptionKey, HostUri };
273-
OptionalParameters = new List<string>();
274+
OptionalParameters = new List<string>() { AdditionalRequestHeaders };
274275
break;
275276
}
276277
case nameof(APIKind.batchsynthesis) + "-" + nameof(Action.delete):

CustomVoice-API-Samples/CSharp/CustomVoice-API/APIHandler.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static void ExecuteBatchSynthesisApi(Action action, Dictionary<string, s
172172
BatchSynthesisGetById(arguments);
173173
break;
174174
case Action.getvoices:
175-
BatchSynthesisGetvoices(arguments);
175+
BatchSynthesisGetVoices(arguments);
176176
break;
177177
case Action.delete:
178178
BatchSynthesisDeleteById(arguments);
@@ -500,7 +500,7 @@ private static void VoiceTestGet(Dictionary<string, string> arguments)
500500
string subscriptionKey = arguments["subscriptionkey"];
501501
string hostURI = arguments["hosturi"];
502502
string modelId = arguments["modelid"];
503-
503+
504504
var result = VoiceTest.Get(subscriptionKey, hostURI, modelId);
505505
DisplayResult<API.DTO.VoiceTest>(result);
506506
}
@@ -676,7 +676,7 @@ private static void BatchSynthesisGet(Dictionary<string, string> arguments)
676676
{
677677
var skipParam = arguments["skip"];
678678
var ret = int.TryParse(skipParam, out skip);
679-
if(!ret)
679+
if (!ret)
680680
{
681681
Console.WriteLine("skip parameter should be an integer number.");
682682
}
@@ -705,12 +705,26 @@ private static void BatchSynthesisGetById(Dictionary<string, string> arguments)
705705
DisplaySingleResult(result, " ");
706706
}
707707

708-
private static void BatchSynthesisGetvoices(Dictionary<string, string> arguments)
708+
private static void BatchSynthesisGetVoices(Dictionary<string, string> arguments)
709709
{
710710
string subscriptionKey = arguments["subscriptionkey"];
711711
string hostURI = arguments["hosturi"];
712+
string additionalRequestHeadersStr = arguments["additionalrequestheaders"];
713+
Dictionary<string, string> additionalRequestHeaders = new Dictionary<string, string>();
714+
715+
if (!string.IsNullOrEmpty(additionalRequestHeadersStr))
716+
{
717+
foreach (string headerStr in additionalRequestHeadersStr.Split(';'))
718+
{
719+
var headerKeyValue = headerStr.Split(',');
720+
if (headerKeyValue.Length == 2)
721+
{
722+
additionalRequestHeaders.Add(headerKeyValue[0], headerKeyValue[1]);
723+
}
724+
}
725+
}
712726

713-
var result = BatchSynthesis.Getvoices(subscriptionKey, hostURI);
727+
var result = BatchSynthesis.Getvoices(subscriptionKey, hostURI, additionalRequestHeaders);
714728
DisplayResult<API.DTO.Voice>(result);
715729
}
716730

@@ -771,7 +785,7 @@ private static void BatchSynthesisCreate(Dictionary<string, string> arguments)
771785

772786
private static void DisplayResult<T>(IEnumerable<T> result)
773787
{
774-
if(result == null)
788+
if (result == null)
775789
{
776790
return;
777791
}
@@ -790,7 +804,7 @@ private static void DisplaySingleResult(object result, string indentation)
790804
string key;
791805
string value;
792806

793-
if(result == null)
807+
if (result == null)
794808
{
795809
return;
796810
}

0 commit comments

Comments
 (0)