Exemple #1
0
        public async Task <PredictResults> Predict(string endPointUrl, Stream imageStream)
        {
            var bearer = await _credential.GetAccessTokenForRequestAsync();

            imageStream.Position = 0;
            var requestBody = new
            {
                payload = new
                {
                    image = new
                    {
                        imageBytes = Utils.StreamToBase64(imageStream)
                    }
                }
            };

            using (var client = new HttpClient())
            {
                var contentStr = JsonConvert.SerializeObject(requestBody);
                var content    = new StringContent(contentStr, Encoding.UTF8, "application/json");
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearer);
                var result = await client.PostAsync(endPointUrl, content);

                if (!result.IsSuccessStatusCode)
                {
                    throw new Exception($"{result.StatusCode} - {result.ReasonPhrase}");
                }
                var strResult = await result.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <PredictResults>(strResult));
            }
        }
        public async Task AccessTokenCredential()
        {
            const string fakeAccessToken = "FakeAccessToken";
            // Create an access-token credential, and check the access token is correct.
            ICredential cred = GoogleCredential.FromAccessToken(fakeAccessToken);

            Assert.Equal(fakeAccessToken, await cred.GetAccessTokenForRequestAsync());
            // Check the auth header is added correctly to an HTTP request.
            var httpHandler = new FakeHandler();
            var httpClient  = new Http.ConfigurableHttpClient(new Http.ConfigurableMessageHandler(httpHandler));

            cred.Initialize(httpClient);
            await httpClient.GetAsync("http://localhost/TestRequest");

            Assert.Equal("Bearer", httpHandler.RequestHeaders.Authorization.Scheme);
            Assert.Equal(fakeAccessToken, httpHandler.RequestHeaders.Authorization.Parameter);
        }