Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics
{
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Rest;

/// <summary>
/// Allows authentication to the API using a basic apiKey mechanism
/// </summary>
public class ApiKeyServiceClientCredentials : ServiceClientCredentials
{
private readonly string subscriptionKey;

/// <summary>
/// Creates a new instance of the ApiKeyServiceClientCredentails class
/// </summary>
/// <param name="subscriptionKey">The subscription key to authenticate and authorize as</param>
public ApiKeyServiceClientCredentials(string subscriptionKey)
{
if (string.IsNullOrWhiteSpace(subscriptionKey))
throw new ArgumentNullException("subscriptionKey");

this.subscriptionKey = subscriptionKey;
}

/// <summary>
/// Add the Basic Authentication Header to each outgoing request
/// </summary>
/// <param name="request">The outgoing request</param>
/// <param name="cancellationToken">A token to cancel the operation</param>
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
throw new ArgumentNullException("request");

request.Headers.Add("Ocp-Apim-Subscription-Key", this.subscriptionKey);

return Task.FromResult<object>(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using System.Net.Http;

namespace Language.Tests
{
public abstract class BaseTests
{
public static bool IsTestTenant = false;
private static string SubscriptionKey = "000";

protected ITextAnalyticsAPI GetClient(DelegatingHandler handler)
{
return new TextAnalyticsAPI(new ApiKeyServiceClientCredentials(SubscriptionKey), handlers: handler)
{
AzureRegion = AzureRegions.Westus
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetPathOfFileAbove('AzSdk.test.reference.props'))" />
<PropertyGroup>
<Description>Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Tests Class Library</Description>
<AssemblyName>Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Tests</AssemblyName>
<Version>1.0.0-preview</Version>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TextAnalytics\Microsoft.Azure.CognitiveServices.Language.TextAnalytics.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="SessionRecords\**\*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"Entries": [
{
"RequestUri": "/text/analytics/v2.0/languages",
"EncodedRequestUri": "L3RleHQvYW5hbHl0aWNzL3YyLjAvbGFuZ3VhZ2Vz",
"RequestMethod": "POST",
"RequestBody": "{\r\n \"documents\": [\r\n {\r\n \"id\": \"id\",\r\n \"text\": \"I love my team mates\"\r\n }\r\n ]\r\n}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"98"
],
"Ocp-Apim-Subscription-Key": [
""
],
"User-Agent": [
"FxVersion/4.6.25211.01",
"Microsoft.CognitiveServices.Language.TextAnalytics.TextAnalyticsAPI/1.0.0"
]
},
"ResponseBody": "{\r\n \"documents\": [\r\n {\r\n \"id\": \"id\",\r\n \"detectedLanguages\": [\r\n {\r\n \"name\": \"English\",\r\n \"iso6391Name\": \"en\",\r\n \"score\": 1.0\r\n }\r\n ]\r\n }\r\n ],\r\n \"errors\": []\r\n}",
"ResponseHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Wed, 30 Aug 2017 02:11:18 GMT"
],
"Transfer-Encoding": [
"chunked"
],
"x-ms-transaction-count": [
"1"
],
"x-aml-ta-request-id": [
"473e3a1f-cf5e-4230-85b5-68fe2518370f"
],
"X-Content-Type-Options": [
"nosniff"
],
"apim-request-id": [
"69e6aeb0-e601-42ca-9468-f0cfaa408708"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"Entries": [
{
"RequestUri": "/text/analytics/v2.0/entities",
"EncodedRequestUri": "L3RleHQvYW5hbHl0aWNzL3YyLjAvZW50aXRpZXM=",
"RequestMethod": "POST",
"RequestBody": "{\r\n \"documents\": [\r\n {\r\n \"language\": \"en\",\r\n \"id\": \"id\",\r\n \"text\": \"Microsoft released Windows 10\"\r\n }\r\n ]\r\n}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"123"
],
"Ocp-Apim-Subscription-Key": [
""
],
"User-Agent": [
"FxVersion/4.6.25211.01",
"Microsoft.CognitiveServices.Language.TextAnalytics.TextAnalyticsAPI/1.0.0"
]
},
"ResponseBody": "{ \"documents\": [{\"id\": \"1\", \"entities\": [ { \"name\": \"Windows 10\", \"matches\": [ { \"text\": \"Windows 10\", \"offset\": 19, \"length\": 10 } ], \"wikipediaLanguage\": \"en\", \"wikipediaId\": \"Windows 10\", \"wikipediaUrl\": \"https://en.wikipedia.org/wiki/Windows_10\", \"bingId\": \"5f9fbd03-49c4-39ef-cc95-de83ab897b94\" }, { \"name\": \"Microsoft\", \"matches\": [ { \"text\": \"Microsoft\", \"offset\": 0, \"length\": 9 } ], \"wikipediaLanguage\": \"en\", \"wikipediaId\": \"Microsoft\", \"wikipediaUrl\": \"https://en.wikipedia.org/wiki/Microsoft\", \"bingId\": \"a093e9b9-90f5-a3d5-c4b8-5855e1b01f85\" } ] }\r\n ],\r\n \"errors\": []\r\n}",
"ResponseHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Wed, 30 Aug 2017 02:11:17 GMT"
],
"Transfer-Encoding": [
"chunked"
],
"x-ms-transaction-count": [
"1"
],
"x-aml-ta-request-id": [
"460baaca-7f57-42c1-8cc7-8852957d05b9"
],
"X-Content-Type-Options": [
"nosniff"
],
"apim-request-id": [
"3f09b1c9-fe3b-46bb-badf-98108c599825"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"Entries": [
{
"RequestUri": "/text/analytics/v2.0/keyPhrases",
"EncodedRequestUri": "L3RleHQvYW5hbHl0aWNzL3YyLjAva2V5UGhyYXNlcw==",
"RequestMethod": "POST",
"RequestBody": "{\r\n \"documents\": [\r\n {\r\n \"language\": \"en\",\r\n \"id\": \"id\",\r\n \"text\": \"I love my team mates\"\r\n }\r\n ]\r\n}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"123"
],
"Ocp-Apim-Subscription-Key": [
""
],
"User-Agent": [
"FxVersion/4.6.25211.01",
"Microsoft.CognitiveServices.Language.TextAnalytics.TextAnalyticsAPI/1.0.0"
]
},
"ResponseBody": "{\r\n \"documents\": [\r\n {\r\n \"keyPhrases\": [\r\n \"team mates\"\r\n ],\r\n \"id\": \"id\"\r\n }\r\n ],\r\n \"errors\": []\r\n}",
"ResponseHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Wed, 30 Aug 2017 02:11:17 GMT"
],
"Transfer-Encoding": [
"chunked"
],
"x-ms-transaction-count": [
"1"
],
"x-aml-ta-request-id": [
"460baaca-7f57-42c1-8cc7-8852957d05b9"
],
"X-Content-Type-Options": [
"nosniff"
],
"apim-request-id": [
"3f09b1c9-fe3b-46bb-badf-98108c599825"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"Entries": [
{
"RequestUri": "/text/analytics/v2.0/sentiment",
"EncodedRequestUri": "L3RleHQvYW5hbHl0aWNzL3YyLjAvc2VudGltZW50",
"RequestMethod": "POST",
"RequestBody": "{\r\n \"documents\": [\r\n {\r\n \"language\": \"en\",\r\n \"id\": \"id\",\r\n \"text\": \"I love my team mates\"\r\n }\r\n ]\r\n}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"123"
],
"Ocp-Apim-Subscription-Key": [
""
],
"User-Agent": [
"FxVersion/4.6.25211.01",
"Microsoft.CognitiveServices.Language.TextAnalytics.TextAnalyticsAPI/1.0.0"
]
},
"ResponseBody": "{\r\n \"documents\": [\r\n {\r\n \"score\": 0.97380387783050537,\r\n \"id\": \"id\"\r\n }\r\n ],\r\n \"errors\": []\r\n}",
"ResponseHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Wed, 30 Aug 2017 02:11:17 GMT"
],
"Transfer-Encoding": [
"chunked"
],
"x-ms-transaction-count": [
"1"
],
"x-aml-ta-request-id": [
"06db056b-1bf0-4994-933a-f08eacf94053"
],
"X-Content-Type-Options": [
"nosniff"
],
"apim-request-id": [
"63c5b90f-3a35-48c3-bb85-d89218d1202e"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Language.Tests;

using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;

using System.Collections.Generic;
using System.Threading.Tasks;

using Xunit;

namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Tests
{
public class DetectLanguageTests : BaseTests
{
[Fact]
public async Task DetectLanguage()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "DetectLanguage");
ITextAnalyticsAPI client = GetClient(HttpMockServer.CreateInstance());
LanguageBatchResult result = await client.DetectLanguageAsync(
new BatchInput(
new List<Input>()
{
new Input("id","I love my team mates")
}));

Assert.Equal("English", result.Documents[0].DetectedLanguages[0].Name);
Assert.Equal("en", result.Documents[0].DetectedLanguages[0].Iso6391Name);
Assert.True(result.Documents[0].DetectedLanguages[0].Score > 0.7);
context.Stop();
}
}
}
}
Loading