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); }
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); }
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)); // SAM: Need to check status of response if (!response.IsSuccessStatusCode) { recordInfo.Status = ActionStatus.Failed; recordInfo.Description = response.ReasonPhrase + " : " + strResponseJson; Console.WriteLine(String.Format("Cannot create 3D Cart order: Error {0}\r\n", recordInfo.Description)); return(recordInfo); } //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; Console.WriteLine(String.Format("Cannot create 3D Cart order: Error {0}\r\n", recordInfo.Description)); } return(recordInfo); }
//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; }