Пример #1
0
    public static string CreateCase(DbCase dbcase)
    {
        try
        {
            var ucase = new UploadCase();
            ucase.age  = dbcase.age;
            ucase.body = dbcase.body;
            ucase.diagnostic_certainty_id = dbcase.diagnostic_certainty_id;
            ucase.presentation            = dbcase.presentation;
            ucase.suitable_for_quiz       = dbcase.suitable_for_quiz;
            ucase.system_id = dbcase.system_id;
            ucase.title     = dbcase.title;
            string postData = JsonConvert.SerializeObject(ucase);
            byte[] data     = System.Text.Encoding.UTF8.GetBytes(postData);

            var api  = ACFG.GetSiteApiDetails();
            var user = AOA.GetUserRefreshIfNecessary(dbcase.username, dbcase.case_id);


            string responseFromServer = null;
            try
            {
                WebRequest request = WebRequest.Create(api.cases_url);
                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();
                if (string.IsNullOrEmpty(responseFromServer))
                {
                    throw new Exception("Unable to get response from server when creating case");
                }
                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 case on Radiopedia server", "API", errorResponse, dbcase.case_id);
                    }
                return(null);
            }


            var respObj = JsonConvert.DeserializeObject <CaseResponse>(responseFromServer);
            dbcase.r_case_id = respObj.id;
            APetaPoco.SetConnectionString("cn1");
            var bm = APetaPoco.PpUpdate(dbcase);
            if (!bm.Success)
            {
                throw new Exception("Unable to update Case in database");
            }
            LOG.InsertEvent("Successfully created Case on Radiopaedia:\n" + respObj.id, "API", responseFromServer, dbcase.case_id);
            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, dbcase.case_id);
            return(null);
        }
    }
Пример #2
0
    public static string CreateCase(DbCase dbcase)
    {
        try
        {
            var ucase = new UploadCase();
            ucase.age = dbcase.age;
            ucase.body = dbcase.body;
            ucase.diagnostic_certainty_id = dbcase.diagnostic_certainty_id;
            ucase.presentation = dbcase.presentation;
            ucase.suitable_for_quiz = dbcase.suitable_for_quiz;
            ucase.system_id = dbcase.system_id;
            ucase.title = dbcase.title;
            string postData = JsonConvert.SerializeObject(ucase);
            byte[] data = System.Text.Encoding.UTF8.GetBytes(postData);

            var api = ACFG.GetSiteApiDetails();
            var user = AOA.GetUserRefreshIfNecessary(dbcase.username, dbcase.case_id);


            string responseFromServer = null;
            try
            {
                WebRequest request = WebRequest.Create(api.cases_url);
                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();
                if (string.IsNullOrEmpty(responseFromServer))
                    throw new Exception("Unable to get response from server when creating case");
                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 case on Radiopedia server", "API", errorResponse, dbcase.case_id);
                }
                return null;
            }


            var respObj = JsonConvert.DeserializeObject<CaseResponse>(responseFromServer);
            dbcase.r_case_id = respObj.id;
            APetaPoco.SetConnectionString("cn1");
            var bm = APetaPoco.PpUpdate(dbcase);
            if (!bm.Success)
                throw new Exception("Unable to update Case in database");
            LOG.InsertEvent("Successfully created Case on Radiopaedia:\n" + respObj.id, "API", responseFromServer, dbcase.case_id);
            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, dbcase.case_id);
            return null;
        }        
    }