public static async Task <HttpResponseMessage> GetAllSmartDetectors(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "smartDetector")] HttpRequestMessage req,
            TraceWriter log,
            CancellationToken cancellationToken)
        {
            using (IUnityContainer childContainer = Container.CreateChildContainer().WithTracer(log, true))
            {
                ITracer tracer           = childContainer.Resolve <ITracer>();
                var     smartDetectorApi = childContainer.Resolve <ISmartDetectorApi>();

                try
                {
                    ListSmartDetectorsResponse smartDetectors = await smartDetectorApi.GetSmartDetectorsAsync(cancellationToken);

                    // Create the response with StringContent to prevent Json from serializing to a string
                    var response = req.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(JsonConvert.SerializeObject(smartDetectors), Encoding.UTF8, "application/json");
                    return(response);
                }
                catch (SmartDetectorsManagementApiException e)
                {
                    tracer.TraceError($"Failed to get Smart Detectors due to managed exception: {e}");

                    return(req.CreateErrorResponse(e.StatusCode, "Failed to get Smart Detectors", e));
                }
                catch (Exception e)
                {
                    tracer.TraceError($"Failed to get Smart Detectors due to un-managed exception: {e}");

                    return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed to get Smart Detectors", e));
                }
            }
        }
Exemple #2
0
        public static async Task <HttpResponseMessage> GetAllSmartDetectors([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "smartDetector")] HttpRequestMessage req, TraceWriter log, CancellationToken cancellationToken)
        {
            using (IUnityContainer childContainer = Container.CreateChildContainer().WithTracer(log, true))
            {
                ITracer tracer           = childContainer.Resolve <ITracer>();
                var     smartDetectorApi = childContainer.Resolve <ISmartDetectorApi>();

                try
                {
                    ListSmartDetectorsResponse smartDetectors = await smartDetectorApi.GetAllSmartDetectorsAsync(cancellationToken);

                    return(req.CreateResponse(smartDetectors));
                }
                catch (SmartDetectorsManagementApiException e)
                {
                    tracer.TraceError($"Failed to get Smart Detectors due to managed exception: {e}");

                    return(req.CreateErrorResponse(e.StatusCode, "Failed to get Smart Detectors", e));
                }
                catch (Exception e)
                {
                    tracer.TraceError($"Failed to get Smart Detectors due to un-managed exception: {e}");

                    return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed to get Smart Detectors", e));
                }
            }
        }
        public async Task WhenGettingAllSmartDetectorsHappyFlow()
        {
            this.smartDetectorRepository.Setup(repository => repository.ReadAllSmartDetectorsManifestsAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => new List <SmartDetectorManifest>()
            {
                new SmartDetectorManifest("someId", "someName", "someDescription", Version.Parse("1.0"), "someAssemblyName", "someClassName", new List <ResourceType> {
                    ResourceType.ResourceGroup
                }, new List <int> {
                    60
                }, null, null)
            });

            ListSmartDetectorsResponse response = await this.smartDetectorsLogic.GetSmartDetectorsAsync(CancellationToken.None);

            Assert.AreEqual(1, response.SmartDetectors.Count);
            Assert.AreEqual("someId", response.SmartDetectors.First().Id);
            Assert.AreEqual("someName", response.SmartDetectors.First().Name);
        }