Beispiel #1
0
        public void GetIpStackTest()
        {
            var ipStack = IpStack.FromJson(IpStackService.GetIpStack("178.235.146.51").Result);

            Assert.AreEqual("Gdynia", ipStack.City);
            Assert.AreEqual("Poland", ipStack.CountryName);
            Assert.IsTrue(54.483871459960938 == ipStack.Latitude);
            Assert.IsTrue(18.464729309082031 == ipStack.Longitude);
            Assert.AreEqual("178.235.146.51", ipStack.Ip);
        }
Beispiel #2
0
        public static ResultCode AddGeolocation(string ipOrUrl)
        {
            if (GeolocationDao.GetGeolocationByIpOrHost(ipOrUrl) != null)
            {
                return(ResultCode.RecordAlreadyExists);
            }
            string response = null;

            try
            {
                response = IpStackService.GetIpStack(ipOrUrl).Result;
            }
            catch (Exception e)
            {
                log.Error("Failed to get geolocation from IPStack", e);
                return(ResultCode.ServiceUnavailable);
            }
            if (response == null)
            {
                return(ResultCode.ServiceUnavailable);
            }
            IpStack ipStack = null;

            try
            {
                ipStack = IpStack.FromJson(response);
            }
            catch (Exception e)
            {
                log.Error("Failed to parse json from IPStack", e);
                return(ResultCode.UnexpectedError);
            }
            if (ipStack.Latitude == null || ipStack.Longitude == null)
            {
                return(ResultCode.UrlNotFound);
            }
            var geolocation = IpStackService.GetGeolocationFromIpStack(ipStack, ipOrUrl);

            try
            {
                if (GeolocationDao.Insert(geolocation))
                {
                    return(ResultCode.OK);
                }
            }
            catch (Exception)
            {
                return(ResultCode.DatabaseError);
            }
            return(ResultCode.UnexpectedError);
        }