示例#1
0
        public void TestGet()
        {
            string baseUrl = "https://example.org/v1/";
            string trialID = "NCT02194738";

            string trialFilePath = TestFileTools.GetPathToTestFile(typeof(ClinicalTrialsAPIClientTests), Path.Combine(new string[] { "TrialExamples", trialID + ".json" }));

            HttpClient mockedClient = HttpClientMockHelper.GetClientMockForURLWithFileResponse(String.Format("{0}clinical-trial/{1}", baseUrl, trialID), trialFilePath);

            mockedClient.BaseAddress = new Uri(baseUrl);

            ClinicalTrialsAPIClient client = new ClinicalTrialsAPIClient(mockedClient);


            ClinicalTrial trial = client.Get(trialID);

            Assert.Equal(trialID, trial.NCTID);
        }
        /// <summary>
        /// Initializes the CTSManager and Parses the Query Params
        /// </summary>
        protected override void Init()
        {
            base.Init();

            ParsedReqUrlParams = new NciUrl(true, true, true);  //We need this to be lowercase and collapse duplicate params. (Or not use an NCI URL)
            ParsedReqUrlParams.SetUrl(this.Request.Url.Query);

            //////////////////////////////
            // Create an instance of a BasicCTSManager.
            ClinicalTrialsAPIClient apiClient = APIClientHelper.GetV1ClientInstance();

            CTSManager = new BasicCTSManager(apiClient);

            /////////////////////////////
            // Parse the Query to get the search params.
            try
            {
                // Get mapping file names from configuration
                TrialTermLookupConfig mappingConfig = new TrialTermLookupConfig();
                mappingConfig.MappingFiles.AddRange(Config.MappingFiles.Select(fp => HttpContext.Current.Server.MapPath(fp)));

                CTSSearchParamFactory factory = new CTSSearchParamFactory(new TrialTermLookupService(mappingConfig, apiClient), new ZipCodeGeoLookup());
                SearchParams = factory.Create(ParsedReqUrlParams);
            }
            catch (Exception ex)
            {
                log.Error("could not parse the CTS search parameters", ex);
                throw ex;
            }

            ///////////////////////////
            // Parse the page specific parameters
            if (IsInUrl(ParsedReqUrlParams, "pn"))
            {
                this._pageNum = ParsedReqUrlParams.CTSParamAsInt("pn", 1);
            }

            _itemsPerPage = Config.DefaultItemsPerPage;
            if (IsInUrl(ParsedReqUrlParams, "ni"))
            {
                this._itemsPerPage = ParsedReqUrlParams.CTSParamAsInt("ni", _itemsPerPage);
            }
        }
 /// <summary>
 /// Checks whether the given trial ID exists in the API.
 /// </summary>
 /// <param name="idString">NCT ID</param>
 /// <returns>True if trial is found in API.</returns>
 private bool IsValidTrial(string idString)
 {
     // If the ID is a valid NCTID, go to web service and see if trial exists
     try
     {
         ClinicalTrialsAPIClient client = APIClientHelper.GetV1ClientInstance();
         ClinicalTrial           trial  = client.Get(idString);
         if (trial != null)
         {
             return(true);
         }
     }
     catch (ArgumentNullException nx)
     {
         log.DebugFormat("Error retrieving trial - value cannot be null. idString = " + idString, nx);
         return(false);
     }
     catch (Exception ex)
     {
         log.Error("Error retrieving trial object from API", ex);
         return(false);
     }
     return(false);
 }
示例#4
0
        private void GeneratePrintCacheAndRedirect(HttpContext context, CTSPrintManager manager)
        {
            HttpRequest  request  = context.Request;
            HttpResponse response = context.Response;

            NciUrl parsedReqUrlParams = new NciUrl(true, true, true);  //We need this to be lowercase and collapse duplicate params. (Or not use an NCI URL)

            parsedReqUrlParams.SetUrl(request.Url.Query);


            ClinicalTrialsAPIClient apiClient = APIClientHelper.GetV1ClientInstance();

            CTSSearchParams searchParams = null;

            try
            {
                // Get mapping file names from configuration
                TrialTermLookupConfig mappingConfig = new TrialTermLookupConfig();
                mappingConfig.MappingFiles.AddRange(_config.MappingFiles.Select(fp => HttpContext.Current.Server.MapPath(fp)));

                CTSSearchParamFactory factory = new CTSSearchParamFactory(new TrialTermLookupService(mappingConfig, apiClient), new ZipCodeGeoLookup());
                searchParams = factory.Create(parsedReqUrlParams);
            }
            catch (Exception ex)
            {
                ErrorPageDisplayer.RaisePageByCode(this.GetType().ToString(), 400); //Anything here is just a bad request.
            }

            //Set our output to be JSON
            response.ContentType     = "application/json";
            response.ContentEncoding = Encoding.UTF8;

            //Try and get the request.
            Request req = null;

            try
            {
                req = GetRequestAndValidate(context);
            }
            catch (Exception)
            {
                ErrorPageDisplayer.RaisePageByCode(this.GetType().ToString(), 400); //Anything here is just a bad request.
                return;
            }

            // Store the cached print content
            Guid printCacheID = manager.StorePrintContent(req.TrialIDs, DateTime.Now, searchParams);

            //Add in debugging helper param to make debugging templates easier.
            //TODO: Refactor so get content and render is a single function.
            if (parsedReqUrlParams.QueryParameters.ContainsKey("debug") && parsedReqUrlParams.QueryParameters["debug"] == "true")
            {
                response.ContentType     = "text/HTML";
                response.ContentEncoding = Encoding.UTF8;
                // If there is no error, send the printID to the manager to retrieve the cached print content
                string printContent = manager.GetPrintContent(printCacheID);

                printContent = printContent.Replace("${generatePrintURL}", GetEmailUrl(printCacheID));

                response.Write(printContent);
                response.End();
            }
            else
            {
                // Format our return as JSON
                var resp = JsonConvert.SerializeObject(new
                {
                    printID = printCacheID
                });

                response.Write(resp);
                response.End();
            }
        }