// [WebInvoke(Method = "POST", UriTemplate = "PostEvent", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] public async Task <HttpResponseMessage> GetRouteTimeAndCost(HttpRequestMessage request) { HttpResponseMessage response; int supplierID = 0; //Ensure HTTPS //if (!(request.RequestUri.Scheme == Uri.UriSchemeHttps)) //{ // response = new HttpResponseMessage(HttpStatusCode.Forbidden) // { // ReasonPhrase = "HTTPS Required", // }; // return response; //} #region Validation IEnumerable <string> users; var getUserHeader = request.Headers.TryGetValues("username", out users); if (!getUserHeader) { response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "'username' header required", }; return(response); } if (users.First() == "oceanic") { supplierID = 1; } else if (users.First() == "telstar") { supplierID = 2; } string storedPassword = ""; try { storedPassword = _users.Get(users.First()); } catch (Exception) { response = new HttpResponseMessage(HttpStatusCode.InternalServerError); } if (storedPassword == "") { response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "Specified username not recognized", }; return(response); } IEnumerable <string> passes; var getPassHeader = request.Headers.TryGetValues("password", out passes); if (!getPassHeader) { response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "'password' header required", }; return(response); } if (passes.First() != storedPassword) { response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "Incorrect password", }; return(response); } #endregion var jstring = await request.Content.ReadAsStringAsync(); RouteRequest requestObject; try { requestObject = JsonConvert.DeserializeObject <RouteRequest>(jstring); } catch (Exception) { response = new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "Could not parse JSON", }; return(response); } //Look-up price and time via data from requestObject Tuple <double, double> priceTime; int transID; try { priceTime = _priceRepo.GetSG(requestObject.Source, requestObject.Target, DateTime.Parse(requestObject.Parcel.ShipmentDate), requestObject.Parcel.GoodsType, requestObject.Parcel.WeightInKg, requestObject.Parcel.LargestSizeInCm); transID = int.Parse(_orderRepo.CreateExternalOrder(supplierID.ToString(), priceTime.Item2, requestObject.Source, requestObject.Target, requestObject.Parcel.GoodsType, requestObject.Parcel.WeightInKg, requestObject.Parcel.LargestSizeInCm, priceTime.Item1, DateTime.Now.AddDays(1))); } catch (Exception e) { response = new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "Error retrieving price info", }; return(response); } //Fill answer RouteResponse answer = new RouteResponse() { TimeInHours = (int)Math.Ceiling(priceTime.Item1), CostInDollars = priceTime.Item2, TransactionID = transID, }; response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); var jsonstring = JsonConvert.SerializeObject(answer); response.Content = new StringContent(jsonstring); return(response); }