public void Search()
        {
            // Arrange
            ITravelLogicService travelLogicService = new TravelLogicService();
            // Call the service
            var searchResult = travelLogicService.Search(TravelLogicSearch.GetDemoObject());

            // Verify
            Assert.IsNotNull(searchResult);
        }
示例#2
0
        public object Search(TravelLogicSearch inputSearch)
        {
            object result    = null;
            string searchURI = ServiceAPI.Library.Infrastructure.Settings.TRAVELLOGIC_SEARCH_URI;

            try
            {
                // Create the HTTP client and set the API URL on it
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(endpoint);

                // Add an Accept header for JSON format.
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));


                // Get the token
                var token = Authenticate();

                if (token.IsAuthenticated())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
                }
                else
                {
                    throw new Exception(ServiceAPI.Library.Infrastructure.Settings.TRAVELLOGIC_AUTH_ERROR_MSG);
                }

                // The http body request
                var content = new StringContent(JsonConvert.SerializeObject(inputSearch), Encoding.UTF8, "application/json");

                // List data response
                HttpResponseMessage response = client.PostAsync(searchURI, content).Result;

                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body to get the list of tickets
                    var responseParsed = JsonConvert.DeserializeObject <TravelLogicSearchResult>(response.Content.ReadAsStringAsync().Result);
                    result = responseParsed.Result;
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(result);
        }
        public IHttpActionResult Search(TravelLogicSearch inputSearch)
        {
            // The Json object to be sent to the client
            object result = null;

            try
            {
                // Calls the Travel Logic search method
                result = _travelLogicService.Search(inputSearch);
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }

            return(Ok(result));
        }