Esempio n. 1
0
        public override void HandleGetRequest(HttpProcessor p)
        {
            // write request success headers (THis is required for every reuqest)
            p.WriteSuccess();

            // create  new response object
            var response = new Response();

            // verify server secret key
            if ((string) p.HttpHeaders["Secret-Key"] != Server.SecretKey)
            {
                // handle wrong secretkey error
                response.error = "Access denied";
            }
            else if (String.IsNullOrEmpty((string) p.HttpHeaders["Action"]))
            {
                // handle no action error
                response.error = "No action provided";
            }
            else if (!_getActions.Contains((string) p.HttpHeaders["Action"]))
            {
                // handle action definition error
                response.error = "Action not defined as a get actions";
            }
            else
            {
                // start valid reqiest --------------------------------------------------------------->>>>
                // store header action for easiest access
                var action = (string) p.HttpHeaders["Action"];
                // request if a single data for example and ID of a patient or an appoointment book
                var request = p.HttpUrl.Substring(1);

                // DO STUFF HERE!
                switch (action)
                {

                    case "getPatientData":
                        // DO STUFF HERE!
                        response.results = new {};
                        // define success
                        response.success = true;
                        // define error if any
                        response.error = "";
                        
                        break;

                    case "getInsuranceData":
                        // DO STUFF HERE!
                         response.results = new {};
                        // define success
                        response.success = true;
                        // define error if any
                        response.error = "";
                        
                        break;

                    case "getBooks":
                        // DO STUFF HERE!
                        response.results = new { };
                        // define success
                        response.success = true;
                        // define error if any
                        response.error = "";
                        
                        break;

                    default:
                        response.error = "Nothing to do";
                        break;

                }


                // write this stuff on display for de bugging
                if (Server.Debug)
                {
                    Server.WriteDisplay("secretKey: " + p.HttpHeaders["Secret-Key"]);
                    Server.WriteDisplay("action: " + p.HttpHeaders["Action"]);
                    Server.WriteDisplay("request: " + request);
                }

                // end valid reqiest <<<<---------------------------------------------------------------
            }

            // JOSN convert response
            var jresponse = JsonConvert.SerializeObject(response);
            // send back response as json
            p.OutputStream.WriteLine(jresponse);
        }
Esempio n. 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="inputData"></param>
        public override void HandlePostRequest(HttpProcessor p, StreamReader inputData)
        {
            if(Server.Debug) Server.WriteDisplay("Request Recived");
            var rawData = inputData.ReadToEnd();
            // write request success headers (THis is required for every reuqest)
            p.WriteSuccess();
            // create  new response object
            var response = new Response {success = true};
            // verify secret key
            if ((string) p.HttpHeaders["Secret-Key"] != Server.SecretKey)
            {
                // handle wrong secretkey error
                response.success = false;
                response.error = "Access denied";
                if (Server.Debug) Server.WriteDisplay(response.error);
            }
            else if (String.IsNullOrEmpty((string) p.HttpHeaders["Action"]))
            {
                // handle no action error
                response.success = false;
                response.error = "No Action Provided";
                if (Server.Debug) Server.WriteDisplay(response.error);
            }
            else if (!_setActions.Contains((string) p.HttpHeaders["Action"]))
            {
                // handle action definition error
                response.success = false;
                response.error = "Action Not Defined As a Set Actions";
                if (Server.Debug) Server.WriteDisplay(response.error);
            }
            else
            {
                // start valid reqiest --------------------------------------------------------------->>>>
                // store header action for easiest access
                var action = (string) p.HttpHeaders["Action"];
                dynamic data;
                // parse the data into an object
                if (Server.Debug) Server.WriteDisplay("UrlDecode, Decoding Data");
                var unscapedData = HttpUtility.UrlDecode(rawData);
                if (unscapedData.StartsWith("{"))
                {
                    data = JObject.Parse(unscapedData);
                }
                else
                {
                    data = JArray.Parse(unscapedData);
                }

                switch (action)
                {
                    case "setSync":
                        /////////////////////////////////////////
                        // Here is where the request is handle //
                        /////////////////////////////////////////
                        response.results = new JObject();
                        // appointments
                        var app = (JArray) data["appointments"];
                        if (app.Any())
                        {
                            response.results.appointments = new JObject();
                            response.results.appointments.successes = new JArray();
                            response.results.appointments.failures = new JArray();

                            if (Server.Debug) Server.WriteDisplay(String.Format("Working {0} Appointment(s)", app.Count()));
                            
                            for (var i = 0; i < app.Count(); i++)
                            {
                                if (WorkAppointment(app[i]))
                                {
                                    response.results.appointments.successes.Add(app[i]);
                                }
                                else
                                {
                                    response.results.appointments.failures.Add(app[i]);
                                }
                            }
                        }
                        // patients demographics
                        var pat = (JArray)data["patients"];
                        if (pat.Any())
                        {
                            response.results.patients = new JObject();
                            response.results.patients.successes = new JArray();
                            response.results.patients.failures = new JArray();

                            if (Server.Debug) Server.WriteDisplay(String.Format("Working {0} Patient(s)", pat.Count()));

                            for (var i = 0; i < pat.Count(); i++)
                            {
                                if (WorkPaitnet(pat[i]))
                                {
                                    response.results.patients.successes.Add(pat[i]);
                                }
                                else
                                {
                                    response.results.patients.failures.Add(pat[i]);
                                }
                            }
                        }
                        // insurance
                        var ins = (JArray) data["insurance"];
                        if (ins.Any())
                        {
                            response.results.insurance = new JObject();
                            response.results.insurance.successes = new JArray();
                            response.results.insurance.failures = new JArray();

                            if (Server.Debug) Server.WriteDisplay(String.Format("Working {0} Insurance(s)", ins.Count()));

                            for (var i = 0; i < ins.Count(); i++)
                            {
                                var isGood = WorkInsurence(ins[i]);
                                if (isGood.GetType() == typeof (DataTable))
                                {
                                    ins[i]["piorden"] = isGood.Rows[0]["piorden"].ToString();
                                    response.results.insurance.successes.Add(ins[i]);
                                }
                                else if(isGood)
                                {
                                    response.results.insurance.successes.Add(ins[i]);
                                }
                                else
                                {
                                    response.results.insurance.failures.Add(ins[i]);
                                }
                            }
                        }
                        // documents
                        var doc = (JArray)data["documents"];
                        if (doc.Any())
                        {
                            response.results.documents = new JObject();
                            response.results.documents.successes = new JArray();
                            response.results.documents.failures = new JArray();

                            if (Server.Debug) Server.WriteDisplay(String.Format("Working {0} Document(s)", doc.Count()));

                            for (var i = 0; i < doc.Count(); i++)
                            {
                                if (WorkDocuments(doc[i]))
                                {
                                    doc[i]["document"] = null; // do not return the document back
                                    response.results.documents.successes.Add(doc[i]);
                                }
                                else
                                {
                                    doc[i]["document"] = null; // do not return the document back
                                    response.results.documents.failures.Add(doc[i]);
                                }
                            }
                        }
                        // signatures are handle very similar to documents
                        var sig = (JArray)data["signatures"];
                        if (sig.Any())
                        {
                            response.results.signatures = new JObject();
                            response.results.signatures.successes = new JArray();
                            response.results.signatures.failures = new JArray();

                            if (Server.Debug) Server.WriteDisplay(String.Format("Working {0} Signatures(s)", sig.Count()));

                            for (var i = 0; i < sig.Count(); i++)
                            {
                                if (WorkSignatures(sig[i]))
                                {
                                    sig[i]["pdf"] = null; // do not return the pdf back
                                    response.results.signatures.successes.Add(sig[i]);
                                }
                                else
                                {
                                    sig[i]["pdf"] = null; // do not return the pdf back
                                    response.results.signatures.failures.Add(sig[i]);
                                }
                            }
                        }

                        // clinical are handle very similar to documents
                        var cli = (JArray)data["clinical"];
                        if (cli.Any())
                        {
                            response.results.clinical = new JObject();
                            response.results.clinical.successes = new JArray();
                            response.results.clinical.failures = new JArray();

                            if (Server.Debug) Server.WriteDisplay(String.Format("Working {0} Clinical(s)", cli.Count()));

                            for (var i = 0; i < cli.Count(); i++)
                            {
                                if (WorkClinical(cli[i]))
                                {
                                    cli[i]["pdf"] = null; // do not return the pdf back
                                    response.results.clinical.successes.Add(cli[i]);
                                }
                                else
                                {
                                    cli[i]["pdf"] = null; // do not return the pdf back
                                    response.results.clinical.failures.Add(cli[i]);
                                }
                            }
                        }

                        break;
                    default:
                        // define default case
                        response.success = false;
                        response.error = "Nothing to do";
                        break;
                }

                // write this stuff on display for de bugging
                if (Server.Debug) Server.WriteDisplay("Raw String Data: " + Uri.UnescapeDataString(rawData));
                

                // end valid reqiest <<<<---------------------------------------------------------------
            }
            #endregion

            // JOSN convert
            var jresponse = JsonConvert.SerializeObject(response);
            // Send back response
            p.OutputStream.WriteLine(jresponse);
        }