Exemplo n.º 1
0
    public static string CreateStudy(DbStudy dbstudy, string username, string caseId)
    {
        try
        {
            var ustudy = new StudyUpload();
            ustudy.modality = GetModality(dbstudy.modality);
            ustudy.caption  = dbstudy.caption;
            ustudy.findings = dbstudy.findings;
            ustudy.position = dbstudy.position;
            string postData = JsonConvert.SerializeObject(ustudy);
            byte[] data     = System.Text.Encoding.UTF8.GetBytes(postData);

            string responseFromServer = null;
            try
            {
                var        api     = ACFG.GetSiteApiDetails();
                var        user    = AOA.GetUserRefreshIfNecessary(username, caseId);
                WebRequest request = WebRequest.Create(api.cases_url + caseId + "/studies");
                request.Method      = "POST";
                request.ContentType = "application/json";
                request.Headers.Add("Authorization", "Bearer " + user.access_token);

                Stream stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();

                WebResponse            response   = request.GetResponse();
                var                    dataStream = response.GetResponseStream();
                System.IO.StreamReader reader     = new System.IO.StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (WebException ex)
            {
                using (var stream = ex.Response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        string errorResponse = reader.ReadToEnd();
                        LOG.InsertEvent("Unable to create new study on Radiopedia server", "API", errorResponse, dbstudy.case_id, dbstudy.study_uid);
                    }
                return(null);
            }
            catch (Exception ex)
            {
                string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name;
                LOG.Write(errorString);
                LOG.Write(ex.Message);
                LOG.InsertEvent(errorString, "API", ex.Message, dbstudy.case_id, dbstudy.study_uid);
                return(null);
            }

            var respObj = JsonConvert.DeserializeObject <StudyResponse>(responseFromServer);
            dbstudy.r_study_id = respObj.id;
            APetaPoco.SetConnectionString("cn1");
            var bm = APetaPoco.PpUpdate(dbstudy);
            if (!bm.Success)
            {
                throw new Exception("Unable to update Study in database");
            }
            LOG.InsertEvent("Successfully created new study:\n" + respObj.id, "API", responseFromServer, dbstudy.case_id, dbstudy.study_uid);
            return(respObj.id);
        }
        catch (Exception ex)
        {
            string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name;
            LOG.Write(errorString);
            LOG.Write(ex.Message);
            LOG.InsertEvent(errorString, "API", ex.Message, dbstudy.case_id, dbstudy.study_uid);
            return(null);
        }
        finally { GC.Collect(); }
    }
Exemplo n.º 2
0
    public static string CreateStudy(DbStudy dbstudy, string username, string caseId)
    {       
        try
        {
            var ustudy = new StudyUpload();
            ustudy.modality = GetModality(dbstudy.modality);
            ustudy.caption = dbstudy.caption;
            ustudy.findings = dbstudy.findings;
            ustudy.position = dbstudy.position;
            string postData = JsonConvert.SerializeObject(ustudy);
            byte[] data = System.Text.Encoding.UTF8.GetBytes(postData);

            string responseFromServer = null;
            try
            {
                var api = ACFG.GetSiteApiDetails();
                var user = AOA.GetUserRefreshIfNecessary(username, caseId);
                WebRequest request = WebRequest.Create(api.cases_url + caseId + "/studies");
                request.Method = "POST";
                request.ContentType = "application/json";
                request.Headers.Add("Authorization", "Bearer " + user.access_token);

                Stream stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();

                WebResponse response = request.GetResponse();
                var dataStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (WebException ex)
            {
                using (var stream = ex.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    string errorResponse = reader.ReadToEnd();
                    LOG.InsertEvent("Unable to create new study on Radiopedia server", "API", errorResponse, dbstudy.case_id, dbstudy.study_uid);
                }
                return null;
            }
            catch (Exception ex)
            {
                string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name;
                LOG.Write(errorString);
                LOG.Write(ex.Message);
                LOG.InsertEvent(errorString, "API", ex.Message, dbstudy.case_id, dbstudy.study_uid);
                return null;
            }

            var respObj = JsonConvert.DeserializeObject<StudyResponse>(responseFromServer);
            dbstudy.r_study_id = respObj.id;
            APetaPoco.SetConnectionString("cn1");
            var bm = APetaPoco.PpUpdate(dbstudy);
            if (!bm.Success)
                throw new Exception("Unable to update Study in database");
            LOG.InsertEvent("Successfully created new study:\n" + respObj.id, "API", responseFromServer, dbstudy.case_id, dbstudy.study_uid);
            return respObj.id;
        }
        catch (Exception ex)
        {
            string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name;
            LOG.Write(errorString);
            LOG.Write(ex.Message);
            LOG.InsertEvent(errorString, "API", ex.Message, dbstudy.case_id, dbstudy.study_uid);
            return null;
        }
        finally { GC.Collect(); }
    }