/// <summary>Generic method for execute the REST call</summary> /// <param name="func">The cal REST function </param> private IPOSResponse ExecuteRESTCall(System.Func <string, IPOSResponse> func) { Stopwatch sw = new Stopwatch(); sw.Start(); string bodyStr = Request.Body.ReadAsString(); string restUrl = Request.Url.ToString(); string requestIP = Request.UserHostAddress; if (requestIP == "::1") { requestIP = "localhost"; } int lastIndex = Request.Url.Path.LastIndexOf('/'); string methodName = lastIndex >= 0 ? Request.Url.Path.Substring(lastIndex + 1) : Request.Url.Path; string logRequestString = AddPrefixMessage( GetLogRequestString(restUrl, bodyStr), requestIP); // log request WriteLog(logRequestString, Request.Method, bodyStr, LogLevel.Debug); // call the function IPOSResponse response = func(bodyStr); sw.Stop(); string logResponseString = AddPrefixMessage( GetLogResponseString(restUrl, bodyStr, response.ResponseContent, sw.ElapsedMilliseconds), requestIP); // log response WriteLog(logResponseString, Request.Method, bodyStr, LogLevel.Debug); return(response); }
public RESTNancyModule() : base(ListenerConfig.Instance.POSRESTModuleBase) { Get["/status/{kiosk?}"] = parameters => { // try to get the kiosk parameter string kiosk = null; try { string kioskStr = parameters.kiosk; if (!string.IsNullOrWhiteSpace(kioskStr)) { kiosk = kioskStr; } } catch { } // defines the function for calling GetStatus method Func <string, IPOSResponse> func = (bodyStr) => { StatusPOSResponse statusPOSResponse = new StatusPOSResponse(); if (string.IsNullOrWhiteSpace(kiosk)) { // the kiosk parameter was not specified statusPOSResponse.SetPOSError(Errors.KioskNotSpecified); } else { try { // call the POS and get the status for the specified kiosk statusPOSResponse = GetStatus(kiosk); } catch (Exception ex) { statusPOSResponse = new StatusPOSResponse(); statusPOSResponse.SetPOSError(Errors.POSError, ex.Message); } } return(statusPOSResponse); }; // call GetStatus function IPOSResponse response = ExecuteRESTCall(func); if (response.HttpStatusCode == HttpStatusCode.OK) { return(new TextResponse(response.HttpStatusCode, response.ResponseContent)); } else { return(response.HttpStatusCode); } }; Post["/order"] = parameters => { // defines the function for calling OrderTransaction method Func <string, IPOSResponse> func = (bodyStr) => { OrderCreatePOSResponse posResponse = new OrderCreatePOSResponse(); Order order = posResponse.OrderCreateResponse.Order; OrderCreateRequest request = null; try { // deserialize request request = JsonConvert.DeserializeObject <OrderCreateRequest>(bodyStr); } catch (Exception ex) { posResponse.SetPOSError(Errors.ErrorDeserializeRequest, ex.Message); } if (!order.HasErrors) { // no deserialize errors => check some elements if (request.DOTOrder == null) { posResponse.SetPOSError(Errors.OrderMissing); } else if (string.IsNullOrWhiteSpace(request.DOTOrder.Kiosk)) { posResponse.SetPOSError(Errors.KioskNotSpecified); } else if (string.IsNullOrWhiteSpace(request.DOTOrder.RefInt)) { posResponse.SetPOSError(Errors.RefIntNotSpecified); } else if (request.DOTOrder.IsNewOrder && !request.DOTOrder.Items.Any()) { posResponse.SetPOSError(Errors.ItemListNotSpecified); } else if (request.DOTOrder.IsTenderOrder && ((request.DOTOrder.Tender == null) || (request.DOTOrder.Tender.TenderItems == null) || !request.DOTOrder.Tender.TenderItems.Any())) { posResponse.SetPOSError(Errors.TenderItemListNotSpecified); } else if (request.DOTOrder.IsExistingOrder && string.IsNullOrWhiteSpace(request.DOTOrder.OrderID)) { posResponse.SetPOSError(Errors.OrderIDNotSpecified); } } if (!order.HasErrors) { try { posResponse = OrderTransaction(request); } catch (Exception ex) { posResponse = new OrderCreatePOSResponse(); posResponse.SetPOSError(Errors.POSError, ex.Message); } } return(posResponse); }; // call OrderTransaction method IPOSResponse response = ExecuteRESTCall(func); if (response.HttpStatusCode == HttpStatusCode.Created) { return(new TextResponse(response.HttpStatusCode, response.ResponseContent)); } else { return(response.HttpStatusCode); } }; Get["/testdiag/{culturename?}"] = parameters => { // try to get the culture name string culturename = null; try { culturename = parameters.culturename; } catch { } // defines the function for calling TestDiag method Func <string, IPOSResponse> func = (bodyStr) => { TestDiagPOSResponse posResponse = new TestDiagPOSResponse(); if (string.IsNullOrWhiteSpace(culturename)) { posResponse.SetPOSError(Errors.CultureNameNotSpecified); } else { try { posResponse = TestDiag(culturename); } catch (Exception ex) { posResponse = new TestDiagPOSResponse(); posResponse.SetPOSError(Errors.POSError, ex.Message); } } return(posResponse); }; // call TestDiag method IPOSResponse response = ExecuteRESTCall(func); if (response.HttpStatusCode == HttpStatusCode.OK) { return(new TextResponse(response.HttpStatusCode, response.ResponseContent)); } else { return(response.HttpStatusCode); } }; }