/// <summary> /// Posts a Search Query to GLM to retrieve any applicable search Results. NOTE: Since GLM is a bit goofy and return a JSON collection that does NOT have a name, we have to return an array of items appearing in the payload as opposed to a rapper named list or object /// </summary> /// <param name="objSourceAddress"></param> /// <returns></returns> public List <Model.SearchLocationService.AddressLocationQuery.AddressSearchLocationV2> PostAddressSearchToGLM(Model.SearchLocationService.AddressLocationQuery.AddressLocationQueryRequest objSourceAddress) { // Instantiate a stopwatch to write runtimes to a log file Stopwatch objStopWatch = new Stopwatch(); objStopWatch.Start(); List <Model.SearchLocationService.AddressLocationQuery.AddressSearchLocationV2> lstArrayOfSearchResult = null; try { // Prep the Request object so it can be sent as the Content Body of a POST to GLM string strSourceAddressAsJsonContentString = Model.SerializationUtil.SerializeToJsonString(objSourceAddress); _strFinalRouteAndQueryString = _strRoute_SearchLocationService_AddressLocationQueryV2; // Initialize the objects used to POST the message to GLM HttpResponseMessage objHttpResponseMessage = null; using (_objHttpClient = new HttpClient()) { // Prep the http client object with the required http header info and endpoint PrepHttpClient(); // Since this is a POST, which has a concent body... create the http content body, and set the corresponding content-type header value HttpContent objHttpContentBody = new StringContent(strSourceAddressAsJsonContentString); objHttpContentBody.Headers.ContentType = new MediaTypeHeaderValue("application/json"); _objLogger.Debug(String.Concat("About to execute the post to GLM via REST. Post Url = [", _strBaseUrl, "]")); // Invoke the RESTful POST method objHttpResponseMessage = _objHttpClient.PostAsync(_strFinalRouteAndQueryString, objHttpContentBody).Result; _objLogger.Debug(String.Concat("Post Complete. Resulting Http Status Code = [", objHttpResponseMessage.StatusCode, "]")); _httpStatusCodeResult = objHttpResponseMessage.StatusCode; if (objHttpResponseMessage.IsSuccessStatusCode) { string strResult = objHttpResponseMessage.Content.ReadAsStringAsync().Result; lstArrayOfSearchResult = Model.SerializationUtil.DeserializeFromJson <List <Model.SearchLocationService.AddressLocationQuery.AddressSearchLocationV2> >(strResult); } else { string strErrorResult = objHttpResponseMessage.Content.ReadAsStringAsync().Result; string strErrorMessage = String.Format("The HTTP response code received from GLM indicated failure. The object returned to the caller will be null. Http Response Code = [{0}], Http Content Body Received from GLM = [{1}]", objHttpResponseMessage.StatusCode, strErrorResult); _objLogger.Warn(String.Format("The call to GLM failed. An error message has been added to the list on this object. Error Message Added = [{0}]", strErrorMessage)); throw new Exception(strErrorMessage); } // We should not get here if the status code evaluation above is good, but just in case anything slips through... we now throw an exception if this was not a success status code objHttpResponseMessage.EnsureSuccessStatusCode(); } } catch (Exception ex) { _objLogger.Warn(String.Concat("There was an issue while trying to POST the ADDRESS SEARCH to GLM. An exception will be thrown. Error Message = [", ex.Message, "]")); _lstErrorMessages.Add(ex.Message); lstArrayOfSearchResult = null; } // Stop the watch and log the call to the DB objStopWatch.Stop(); TimeSpan objTimeSpan = objStopWatch.Elapsed; LogAPICallToDB(objTimeSpan); // Return the result return(lstArrayOfSearchResult); }
public void Test_PostAddressSearch_GLMSample() { /// POST http://glmenv1/GLMSWebServices/SearchLocationService.svc/AddressLocationQuery/v2 HTTP/1.1 /// Authorization: svc.asset_management /// X - GLM - API - Authorization: 6775 /// Content - Type: application / json /// Host: glmenv1 /// Content - Length: 155 /// /// { /// "AddressLine1":"10475 Park Meadows Dr", /// "City":"Lone Tree", /// "StateCode":"CO", /// "CountryCode":"USA", /// "PostalCode":"", /// "CreateSiteIfNotFound": true /// } Model.SearchLocationService.AddressLocationQuery.AddressLocationQueryRequest objAdvancedLocationQueryV2Request = new Model.SearchLocationService.AddressLocationQuery.AddressLocationQueryRequest(); objAdvancedLocationQueryV2Request.CreateSiteIfNotFound = true; objAdvancedLocationQueryV2Request.AddressLine1 = "10475 Park Meadows Dr"; objAdvancedLocationQueryV2Request.City = "Lone Tree"; objAdvancedLocationQueryV2Request.StateCode = "CO"; objAdvancedLocationQueryV2Request.CountryCode = "USA"; RAL.GLMCallManager objGLMCallManager = new RAL.GLMCallManager(_strBaseUrl, _strAuthorizationHeaderUsername_Authorization, _strAuthorizationHeaderApplicationID_XGLMAPIAuthorization); List <Model.SearchLocationService.AddressLocationQuery.AddressSearchLocationV2> lstSearchResultV2 = objGLMCallManager.PostAddressSearchToGLM(objAdvancedLocationQueryV2Request); Assert.IsTrue(lstSearchResultV2 != null); Assert.IsTrue(objGLMCallManager.ErrorMessages.Count == 0); }