Exemple #1
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);
        }