コード例 #1
0
        private async Task MigrateServiceConfigV1(ProfileStorageContainer container, string serviceName, Action <Profile, Domain.Profiles.CognitiveServiceConfig> setConfigProperty)
        {
            var faceApiConfig = await _localStorageService.GetItemAsync <Dictionary <string, CognitiveServiceConfig> >($"cs-config-profile-{serviceName}");

            // Migrate from old format where every service had it's own storage container.
            if (faceApiConfig != null)
            {
                foreach (var config in faceApiConfig)
                {
                    var profile = container.Profiles
                                  .FirstOrDefault(c => c.ProfileName == config.Key);

                    if (profile == null)
                    {
                        profile = new Profile(config.Key);
                        container.Profiles.Add(profile);
                    }

                    var newConfig = new CognitiveServiceConfig
                    {
                        BaseUrl     = config.Value.BaseUrl,
                        ServiceName = config.Value.ServiceName,
                        Token       = config.Value.Token
                    };

                    setConfigProperty(profile, newConfig);
                }
            }
        }
        public async Task ShouldUpdatePersonGroup()
        {
            using var httpTest = new HttpTest();
            httpTest.RespondWith("{}");

            var request = PersonGroupRequestGenerator.Update("default-group", "test name");
            var config  = new CognitiveServiceConfig("FaceApi", "http://cs-explorer.com", "test-token");
            await _handler.Handle(new ExecuteCognitiveServicesCommand(request, config), default);

            httpTest
            .ShouldHaveCalled("http://cs-explorer.com/face/v1.0/persongroups/default-group")
            .WithVerb(HttpMethod.Patch)
            .WithHeader("Ocp-Apim-Subscription-Key", "test-token")
            .Times(1);
        }
コード例 #3
0
        public async Task <string?> Send(HttpRequest request, CognitiveServiceConfig cognitiveServiceConfig, CancellationToken token = default)
        {
            var url = new Url(cognitiveServiceConfig.BaseUrl)
                      .AppendPathSegment(request.RelativePath)
                      .WithHeader(request.TokenHeaderName, cognitiveServiceConfig.Token);

            if (request.Queries?.Any() == true)
            {
                url = url.SetQueryParams(request.Queries);
            }

            Task <IFlurlResponse> responseTask = null !;
            string httpMethod = request.HttpMethod.ToUpperInvariant();

            switch (httpMethod)
            {
            case "GET":
                responseTask = url.GetAsync(token);
                break;

            case "POST":
            case "PUT":
            {
                HttpContent content;
                if (request.BinaryContent != null)
                {
                    var byteArrayContent = new ByteArrayContent(request.BinaryContent);
                    byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue(request.ContentType);
                    content = byteArrayContent;
                }
                else
                {
                    var stringContent = new StringContent(request.Body ?? string.Empty);
                    stringContent.Headers.ContentType = new MediaTypeHeaderValue(request.ContentType);
                    content = stringContent;
                }

                responseTask = httpMethod == "POST" ?
                               url.PostAsync(content, token) :
                               url.PutAsync(content, token);

                break;
            }

            case "DELETE":
                responseTask = url.DeleteAsync(token);
                break;

            case "PATCH":
            {
                var content = new StringContent(request.Body ?? string.Empty);
                content.Headers.ContentType = new MediaTypeHeaderValue(request.ContentType);
                responseTask = url.PatchAsync(content, token);
            }
            break;
            }

            using (var response = await responseTask)
            {
                return(await response.GetStringAsync());
            }
        }
 public ExecuteCognitiveServicesCommand(HttpRequest request, CognitiveServiceConfig cognitiveServiceConfig)
 {
     Request = request;
     CognitiveServiceConfig = cognitiveServiceConfig;
 }