示例#1
0
        //Holds the data returned and result of the HTTP operation
        private RecordInfo GetRecordInfo(ResponseInfo responseInfo, bool IsSuccessStatus)
        {
            var recordInfo = new RecordInfo();

            int intStatusCode = -1;
            bool bResult = Int32.TryParse(responseInfo.Status, out intStatusCode);
            if (!bResult) { intStatusCode = -1; }

            //If success status is passed through function then store status, code number, message and value in properties of RecordInfo object
            //If status is not success then only store status, code number and description in the properties of RecordInfo object
            if (IsSuccessStatus)
            {

                recordInfo.Status = ActionStatus.Succeeded;
                recordInfo.CodeNumber = intStatusCode;
                recordInfo.Description = responseInfo.Message;
                recordInfo.ResultSet = (Object)responseInfo.Value;

            }

            else
            {
                recordInfo.CodeNumber = intStatusCode;
                recordInfo.Description = responseInfo.Message;
                recordInfo.Status = ActionStatus.Failed;
            }

            return recordInfo;
        }
示例#2
0
        public RecordInfo GetRecords()
        {
            //Create a RecordInfo object to store the information retrieved through a GET operation
            RecordInfo recordInfo = new RecordInfo();

            //Create a list of Object to hold the records retrieved from a GET
            List<Object> recordList = new List<Object>();

            var client = GetClient();

            try
            {
                string sTypeWithID = string.Empty;
                string sTypeWithLimit = string.Empty;

                //Create an instance of HTTP Client by calling custom GetClient function 

                // If ID is set, GET will retrieve one record matching with ID. If ID is not set, all the records will be retrieved.
                // Type indicates the type of object we are looking to retrieve. For example Customers, CustomerGroup, Products
                if (ID != 0)
                {
                    sTypeWithID = Type + "/" + ID;

                }
                else
                {
                    sTypeWithID = Type;
                }


                //Specify maximum number of records to GET
                if (RecordsGetLimit != 0)
                {
                    sTypeWithLimit = sTypeWithID + "?limit=" + RecordsGetLimit;

                }
                else
                {
                    sTypeWithLimit = sTypeWithID;
                }
                //GET the record(s) by using GetAsync method of HTTPClient and store the response in variable of HttpResponseMessage type
                HttpResponseMessage response = client.GetAsync(sTypeWithLimit).Result;


                //If GET is successful, store the records in  recordList object we already created, else throw the exception to show the error
                if (response.IsSuccessStatusCode)
                {

                    string receiveStream = response.Content.ReadAsStringAsync().Result;
                    recordList = (List<Object>)JsonConvert.DeserializeObject(receiveStream, typeof(List<Object>));

                }
                else
                {
                        //throws exception if operation was not successful
                        response.EnsureSuccessStatusCode();

                }

                //Store the records in resultset property and set the status to successful
                recordInfo.ResultSet = recordList;
                recordInfo.Status = ActionStatus.Succeeded;

            }
            catch (Exception ex)
            {
                recordInfo.Description = ex.Message;
                recordInfo.Status = ActionStatus.Failed;
            }



            return recordInfo;
        }
示例#3
0
        public RecordInfo DeleteRecord()
        {
             RecordInfo recordInfo = new RecordInfo();

             try
             {

                 string sTypeWithID = string.Empty;

                 //Create an instance of HTTP Client by calling custom GetClient function 
                 var client = GetClient();

                 // If ID is set, ID will be appended to URL, else update operation will fail.
                 // Type indicates the type of object we are looking to update. For example Customers, CustomerGroup, Products
                 if (ID != 0)
                 {
                     sTypeWithID = Type + "/" + ID;

                 }
                 else
                 {
                     sTypeWithID = Type;
                 }

                 //Call the DeleteAsync method to delete record through HTTP DELETE
                 HttpResponseMessage response = client.DeleteAsync(sTypeWithID).Result;

                 //Call ReadAsStringAsync to check the result code and if there is any error message. Store the information in ResponeInfo object.
                 string strResponseJson = response.Content.ReadAsStringAsync().Result;
                 ResponseInfo responeInfoObject = JsonConvert.DeserializeObject<ResponseInfo>(strResponseJson.Substring(1, strResponseJson.Length - 2));

                 recordInfo = GetRecordInfo(responeInfoObject, response.IsSuccessStatusCode);

             }

             catch (Exception ex)
             {

                 recordInfo.Description = ex.Message;
                 recordInfo.Status = ActionStatus.Failed;

             }

             return recordInfo;

        }
示例#4
0
        public RecordInfo UpdateRecord(Object record)
        {
            var recordInfo = new RecordInfo();
            
            try
            {
                string sTypeWithID = string.Empty;

                //Create an instance of HTTP Client by calling custom GetClient function 
                var client = GetClient();

                // If ID is set, ID will be appended to URL, else update operation will fail.
                // Type indicates the type of object we are looking to update. For example Customers, CustomerGroup, Products
                if (ID != 0)
                {
                    sTypeWithID = Type + "/" + ID;

                }
                else
                {
                    sTypeWithID = Type;
                }

                //Call the PutAsJsonAsync method to update record through HTTP PUT
                HttpResponseMessage response = client.PutAsJsonAsync(sTypeWithID, record).Result;

                //Call ReadAsStringAsync to check the result code and if there is any error message. Store the information in ResponeInfo object.
                string strResponseJson = response.Content.ReadAsStringAsync().Result;
                ResponseInfo responeInfoObject = JsonConvert.DeserializeObject<ResponseInfo>(strResponseJson.Substring(1, strResponseJson.Length - 2));

                //If update is successful, store the ID of newly updated record in 'resultset' property and mark the status as successful, 
                //else set the status to failed and provide the error code and description through CodeNumber and description property
                recordInfo = GetRecordInfo(responeInfoObject, response.IsSuccessStatusCode);
                
            }

            catch (Exception ex)
            {
                recordInfo.Description = ex.Message;
                recordInfo.Status = ActionStatus.Failed;

            }

            return recordInfo;

        }
示例#5
0
        public RecordInfo AddRecord(Object record)
        {
            
            RecordInfo recordInfo = new RecordInfo();

            try
            {

                //Create an instance of HTTP Client by calling custom GetClient function 
                var client = GetClient();

                //Call the PostAsJsonAsync method to add record through HTTP POST
                HttpResponseMessage response = client.PostAsJsonAsync(Type, record).Result;

                //Call ReadAsStringAsync to check the result code and if there is any error message. Store the information in ResponeInfo object.
                string strResponseJson = response.Content.ReadAsStringAsync().Result;
                ResponseInfo responeInfoObject = JsonConvert.DeserializeObject<ResponseInfo>(strResponseJson.Substring(1, strResponseJson.Length - 2));
                

                //If add is successful, store the ID of newly generated record in 'resultset' property and mark the status as successful, 
                //else set the status to failed and provide the error code and description through CodeNumber and description property
                recordInfo = GetRecordInfo(responeInfoObject, response.IsSuccessStatusCode);

            }

            catch (Exception ex)
            {
                recordInfo.Description = ex.Message;
                recordInfo.Status = ActionStatus.Failed;

            }

            return recordInfo;


        }
示例#6
0
        public static void CallFunctions(IRestAPIType singletonAPIObject, RestAPIActions siteRecords, ActionType action, RestAPIType type)
        {
            //RestAPIObject recordToProcess = null;
            //List<RestAPIObject> recordList = new List<RestAPIObject>();

            List <IRestAPIType> recordList = new List <IRestAPIType>();

            RestAPIClassFactory classFactory = new RestAPIClassFactory();



            #region Commented Code

            /*
             *          if (siteRecords.Type == GetRestAPIType(RestAPIType.Customer) )
             *          {
             *              recordToProcess = (Customer)singletonAPIObject;
             *          }
             *
             *          if (siteRecords.Type == GetRestAPIType(RestAPIType.CustomerGroup) )
             *          {
             *              recordToProcess = (CustomerGroup)singletonAPIObject;
             *          }
             *
             *
             *          if (siteRecords.Type == GetRestAPIType(RestAPIType.Order) )
             *          {
             *              recordToProcess = (Order)singletonAPIObject;
             *          }
             *
             *          if (siteRecords.Type == GetRestAPIType(RestAPIType.Product) )
             *          {
             *              recordToProcess = (Product)singletonAPIObject;
             *          }
             *
             *          if (siteRecords.Type == GetRestAPIType(RestAPIType.Manufacturer) )
             *          {
             *              recordToProcess = (Manufacturer)singletonAPIObject;
             *          }
             *
             *          if (siteRecords.Type == GetRestAPIType(RestAPIType.Distributor) )
             *          {
             *              recordToProcess = (Distributor)singletonAPIObject;
             *          }
             *
             *          if (siteRecords.Type ==  GetRestAPIType(RestAPIType.Category) )
             *          {
             *              recordToProcess = (Category)singletonAPIObject;
             *          }
             */

            #endregion

            string sIDPrefix = siteRecords.Type.Substring(0, siteRecords.Type.Length - 1) + "ID: ";


            if (action == ActionType.Get)
            {
                //GET
                RecordInfo recInf = siteRecords.GetRecords();


                if (recInf.Status == ActionStatus.Succeeded)
                {
                    Console.WriteLine("Record(s) retrieved successfully.");
                    try
                    {
                        List <Object> reclist = (List <Object>)recInf.ResultSet;

                        IRestAPIType restAPIRecord = classFactory.GetRestAPIClassType(type);

                        StringBuilder sb       = new StringBuilder();
                        var           date     = DateTime.Now.ToString("yyMMdd");
                        var           dateLong = DateTime.Now.ToString("yyyyMMdd");
                        var           time     = DateTime.Now.ToString("hhmm");
                        var           id       = "3142993930";
                        sb.AppendLine($"ISA*00*          *00*          *ZZ*{id}     *12*8003282935     *{date}*{time}*^*00403*000006383*0*P*>~");
                        sb.AppendLine($"GS*PO*{id}*8003282935*{dateLong}*{time}*6399*X*004030~");
                        int anotherCount = 1;
                        foreach (Object record in reclist)
                        {
                            string receiveStream = record.ToString();

                            //Use the type received from function

                            var order = (Order)JsonConvert.DeserializeObject(receiveStream, typeof(Order));
                            sb.AppendLine($"ST*850*000{anotherCount}~");
                            sb.AppendLine($"BEG*00*DS*{order.InvoiceNumber}**{dateLong}~");

                            var shipping = "CG";
                            if (order.ShipmentList[0].ShipmentMethodName.Contains("2nd"))
                            {
                                shipping = "SE";
                            }
                            else if (order.ShipmentList[0].ShipmentMethodName.Contains("Next"))
                            {
                                shipping = "ND";
                            }
                            sb.AppendLine($"TD5************{shipping}~");
                            var name = order.ShipmentList[0].ShipmentFirstName + " " + order.ShipmentList[0].ShipmentLastName;
                            sb.AppendLine($"N1*ST*{name}~");

                            var address = order.ShipmentList[0].ShipmentAddress;

                            if (!string.IsNullOrEmpty(order.ShipmentList[0].ShipmentAddress2))
                            {
                                address += "*" + order.ShipmentList[0].ShipmentAddress2;
                            }
                            sb.AppendLine($"N3*{address}~");
                            var city  = order.ShipmentList[0].ShipmentCity;
                            var state = order.ShipmentList[0].ShipmentState;
                            var zip   = order.ShipmentList[0].ShipmentZipCode;

                            sb.AppendLine($"N4*{city}*{state}*{zip}~");
                            int count = 1;
                            foreach (var item in order.OrderItemList)
                            {
                                sb.AppendLine($"PO1*{count}*{item.ItemQuantity}*EA*{item.ItemUnitCost}**VN*{item.ItemID}~");
                                sb.AppendLine($"PID*F****{item.ItemDescription}~");
                                count++;
                            }
                            sb.AppendLine($"CTT*{order.OrderItemList.Count}~");
                            int totalElements = 7 + order.OrderItemList.Count;
                            sb.AppendLine($"SE*{totalElements}*000{anotherCount}*~");

                            #region Commented Code

                            /*
                             * if (siteRecords.Type == GetRestAPIType(RestAPIType.Customer) )
                             * {
                             *  restAPIRecord = (Customer)JsonConvert.DeserializeObject(receiveStream, restAPIRecord.GetType());
                             *  //restAPIRecord = (IRestAPIType) JsonConvert.DeserializeObject(receiveStream, restAPIRecord.GetType());
                             *
                             * }
                             * else if (siteRecords.Type == GetRestAPIType(RestAPIType.CustomerGroup) )
                             * {
                             *  restAPIRecord = (CustomerGroup)JsonConvert.DeserializeObject(receiveStream, restAPIRecord.GetType());
                             *
                             * }
                             *
                             * else if (siteRecords.Type == GetRestAPIType(RestAPIType.Product) )
                             * {
                             *  restAPIRecord = (Product)JsonConvert.DeserializeObject(receiveStream, typeof(Product));
                             * }
                             * else if (siteRecords.Type == GetRestAPIType(RestAPIType.Order))
                             * {
                             *  restAPIRecord = (Order)JsonConvert.DeserializeObject(receiveStream, typeof(Order));
                             * }
                             * else if (siteRecords.Type == GetRestAPIType(RestAPIType.Manufacturer) )
                             * {
                             *  restAPIRecord = (Manufacturer)JsonConvert.DeserializeObject(receiveStream, typeof(Manufacturer));
                             * }
                             * else if (siteRecords.Type == GetRestAPIType(RestAPIType.Distributor) )
                             * {
                             *  restAPIRecord = (Distributor)JsonConvert.DeserializeObject(receiveStream, typeof(Distributor));
                             * }
                             * else if (siteRecords.Type == GetRestAPIType(RestAPIType.Category) )
                             * {
                             *  restAPIRecord = (Category)JsonConvert.DeserializeObject(receiveStream, typeof(Category));
                             * }
                             */
                            #endregion

                            //recordList.Add(restAPIRecord);
                            anotherCount++;
                        }
                        sb.AppendLine($"GE*{reclist.Count}*6383~");
                        sb.AppendLine($"IEA*1*000006383~");
                        var result = sb.ToString();
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    Console.WriteLine("Record(s) retrieval failed. Error description: " + recInf.Description);
                }
            }


            //IRestAPIType recordToProcess = classFactory.GetRestAPIClassType(type);


            if (action == ActionType.Add)
            {
                //POST
                //RecordInfo addInfo = siteRecords.AddRecord(recordToProcess);
                RecordInfo addInfo = siteRecords.AddRecord(singletonAPIObject);


                if (addInfo.Status == ActionStatus.Succeeded)
                {
                    Console.WriteLine("Record Added. " + sIDPrefix + addInfo.ResultSet);
                    Console.ReadKey();
                }

                else
                {
                    Console.WriteLine("Add failed.  Error Code: " + addInfo.CodeNumber + ", Description: " + addInfo.Description);
                    Console.ReadKey();
                }
            }


            if (action == ActionType.Update)
            {
                //PUT
                //RecordInfo updateInfo = siteRecords.UpdateRecord(recordToProcess);
                RecordInfo updateInfo = siteRecords.UpdateRecord(singletonAPIObject);


                if (updateInfo.Status == ActionStatus.Succeeded)
                {
                    Console.WriteLine("Record Updated. " + sIDPrefix + siteRecords.ID);
                    Console.ReadKey();
                }

                else
                {
                    Console.WriteLine("Update failed for " + sIDPrefix + siteRecords.ID + ". Error Code: " + updateInfo.CodeNumber + ", Description: " + updateInfo.Description);
                    Console.ReadKey();
                }
            }

            if (action == ActionType.Delete)
            {
                //DELETE
                RecordInfo deleteInfo = siteRecords.DeleteRecord();

                if (deleteInfo.Status == ActionStatus.Succeeded)
                {
                    Console.WriteLine("Record Deleted. " + sIDPrefix + "ID: " + siteRecords.ID);
                    Console.ReadKey();
                }

                else
                {
                    Console.WriteLine("Delete failed for " + sIDPrefix + siteRecords.ID + ". Error Code: " + deleteInfo.CodeNumber + ", Description: " + deleteInfo.Description);
                    Console.ReadKey();
                }
            }
        }
示例#7
0
        public RecordInfo GetRecords()
        {
            //Create a RecordInfo object to store the information retrieved through a GET operation
            RecordInfo recordInfo = new RecordInfo();

            //Create a list of Object to hold the records retrieved from a GET
            List <Object> recordList = new List <Object>();

            var client = GetClient();

            try
            {
                string sTypeWithID    = string.Empty;
                string sTypeWithLimit = string.Empty;

                //Create an instance of HTTP Client by calling custom GetClient function

                // If ID is set, GET will retrieve one record matching with ID. If ID is not set, all the records will be retrieved.
                // Type indicates the type of object we are looking to retrieve. For example Customers, CustomerGroup, Products
                if (ID != 0)
                {
                    sTypeWithID = Type + "/" + ID;
                }
                else
                {
                    sTypeWithID = Type;
                }


                //Specify maximum number of records to GET
                if (RecordsGetLimit != 0)
                {
                    sTypeWithLimit = sTypeWithID + "?limit=" + RecordsGetLimit;
                }
                else
                {
                    sTypeWithLimit = sTypeWithID;
                }
                //GET the record(s) by using GetAsync method of HTTPClient and store the response in variable of HttpResponseMessage type
                HttpResponseMessage response = client.GetAsync(sTypeWithLimit).Result;


                //If GET is successful, store the records in  recordList object we already created, else throw the exception to show the error
                if (response.IsSuccessStatusCode)
                {
                    string receiveStream = response.Content.ReadAsStringAsync().Result;
                    recordList = (List <Object>)JsonConvert.DeserializeObject(receiveStream, typeof(List <Object>));
                }
                else
                {
                    //throws exception if operation was not successful
                    response.EnsureSuccessStatusCode();
                }

                //Store the records in resultset property and set the status to successful
                recordInfo.ResultSet = recordList;
                recordInfo.Status    = ActionStatus.Succeeded;
            }
            catch (Exception ex)
            {
                recordInfo.Description = ex.Message;
                recordInfo.Status      = ActionStatus.Failed;
            }



            return(recordInfo);
        }