예제 #1
0
        public HttpResponseMessage GetStates(string securityToken, string countryCode)
        {
            StatesResponse response = null;

            if (IsValid(securityToken))
            {
                ICommon commonSvc = new CommonService(this._dbContext);
                response = new StatesResponse {
                    Status = "OK"
                };
                response.States = commonSvc.FindStates(countryCode);

                if (null == response.States)
                {
                    response.Status       = "Error";
                    response.ErrorMessage = string.Format("States not found for country: {0}", countryCode);
                    CurrentLoggerProvider.Info(response.ErrorMessage);
                }
                else
                {
                    CurrentLoggerProvider.Info(string.Format("Retrieved States for Country: {0}", countryCode));
                }
            }
            else
            {
                response = new StatesResponse {
                    Status = "Error", ErrorCode = "ERR1001", ErrorMessage = "Invalid or expired token"
                };
                CurrentLoggerProvider.Info("Invalid Request");
            }

            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }
예제 #2
0
        public async Task <StatesResult> GetStates(string countryCode)
        {
            JObject @params = new JObject()
            {
                new JProperty("params", new JObject()
                {
                    new JProperty("country_iso", countryCode)
                })
            };

            StatesResponse response = await restClient.PostAsync <StatesResponse, JObject>(Constants.ApiURI.URL_MAIN + Constants.ApiURI.URI_GET_STATES, @params);

            return(response?.Result);
        }
예제 #3
0
        private void Initialize()
        {
            var            statesUrl      = ApiHost + StatesRoute;
            StatesResponse statesResponse = HttpGetAsync <StatesResponse>(statesUrl, LanguageParam).Result;

            States = new Dictionary <string, int>();
            if (statesResponse != null)
            {
                foreach (var state in statesResponse.states)
                {
                    States.Add(state.state_name, state.state_id);
                }
            }
            Districts = new Dictionary <int, Dictionary <string, int> >();
        }
        /// <summary>
        /// Map the unnamed JSON States objects to a list of State objects.
        /// </summary>
        /// <param name="statesResponse">The StatesResponse object that got deserialized.</param>
        /// <returns>The StatesResponse object with ConvertedStates value filled in if applicable.</returns>
        private StatesResponse MapObjectsToStates(StatesResponse statesResponse)
        {
            // Instantly return if there are no States.
            if (statesResponse.States == null)
            {
                return(statesResponse);
            }

            // Convert the states manually and add them to a list to slot into the response object.
            var convertedStates = new List <State>();

            foreach (var state in statesResponse.States)
            {
                // Remove leading [ and trailing ] and split on commas.
                var splitState = state.ToString().Trim('[', ']').Split(',');
                // Trim endings of all splits.
                for (int i = 0; i < splitState.Length; i++)
                {
                    // Remove all \r, \n and " occurences and trim excess whitespace.
                    splitState[i] = splitState[i].Replace("\r", "").Replace("\n", "").Replace('"', ' ').Trim();
                }

                var mappedState = new State()
                {
                    ICAO24        = splitState[0],
                    CallSign      = splitState[1],
                    OriginCountry = splitState[2],
                    // Skipped parsing 3 to 16 (except for 14) since they can be null.
                    Squawk = splitState[14]
                };
                if (!splitState[3].Equals("null"))
                {
                    mappedState.TimePosition = int.Parse(splitState[3]);
                }
                if (!splitState[4].Equals("null"))
                {
                    mappedState.LastContact = int.Parse(splitState[4]);
                }
                if (!splitState[5].Equals("null"))
                {
                    mappedState.Longitude = decimal.Parse(splitState[5]);
                }
                if (!splitState[6].Equals("null"))
                {
                    mappedState.Latitude = decimal.Parse(splitState[6]);
                }
                if (!splitState[7].Equals("null"))
                {
                    mappedState.BaroAltitude = decimal.Parse(splitState[7]);
                }
                if (!splitState[8].Equals("null"))
                {
                    mappedState.OnGround = bool.Parse(splitState[8]);
                }
                if (!splitState[9].Equals("null"))
                {
                    mappedState.Velocity = decimal.Parse(splitState[9]);
                }
                if (!splitState[10].Equals("null"))
                {
                    mappedState.TrueTrack = decimal.Parse(splitState[10]);
                }
                if (!splitState[11].Equals("null"))
                {
                    mappedState.VerticalRate = decimal.Parse(splitState[11]);
                }
                // Map the sensors int array from string to an actual int array.
                if (!splitState[12].Equals("null"))
                {
                    var sensorsStringArray = splitState[12].Split(',');
                    var sensors            = new List <int>();
                    foreach (var sensorString in sensorsStringArray)
                    {
                        sensors.Add(int.Parse(sensorString));
                    }
                    mappedState.Sensors = sensors.ToArray();
                }
                if (!splitState[13].Equals("null"))
                {
                    mappedState.GeoAltitude = decimal.Parse(splitState[13]);
                }
                if (!splitState[15].Equals("null"))
                {
                    mappedState.Spi = bool.Parse(splitState[15]);
                }
                if (!splitState[16].Equals("null"))
                {
                    mappedState.PositionSource = int.Parse(splitState[16]);
                }

                convertedStates.Add(mappedState);
            }

            statesResponse.ConvertedStates = convertedStates;

            return(statesResponse);
        }