private Task <IpResponse> ExecuteAllServiceAsync(string ipAddress, string url) { IpResponse response = new IpResponse(); //var dnsTask = _domainService.GetDnsDetails(ipAddress).Result; IpData geoTask; DnsData dnsTask; PingData pingTask; Timestamp timestamp = new Timestamp(); Parallel.Invoke( () => { geoTask = _domainService.GetGeoDetails(ipAddress, url).Result; response.GeoData = _ipMapper.Map(geoTask); timestamp.GeoTime = DateTime.Now; }, () => { dnsTask = _domainService.GetDnsDetails(ipAddress).Result; response.DnsInfo = _idnsMapper.Map(dnsTask); timestamp.DnsTime = DateTime.Now; }, () => { pingTask = _domainService.GetPingDetails(ipAddress).Result; response.PingInfo = _ipingMapper.Map(pingTask); timestamp.PingTime = DateTime.Now; } ); return(Task.FromResult <IpResponse>(response)); }
private static string TryGetDomainName(IpResponse value) { if (value == null) { return(null); } var domains = new List <string>(); var entities = new List <Entity>(); entities.AddRange(value.Entities); for (var i = 0; i < entities.Count; i++) { var entity = entities[i]; entities.AddRange(entity.Entities); ExtractDomain(domains, entity); } var common = new string(CommonDomain(domains)); if (domains.Contains(common)) { return(common); } return(domains.FirstOrDefault()); }
public GetIpDetailsResponse FetchIp(GetIpRequest ipRequest) { if (CheckIfExistInCache(ipRequest)) { return((GetIpDetailsResponse)cacheService.GetValue(ipRequest.Ip)); } if (CheckIfIpExistInDatabase(ipRequest)) { FetchIpFromDbIfExistAndCache(ipRequest); } else { //must refactor this to other methods IpResponse resultFromLib = (IpResponse)infoProvider.GetDetails(ipRequest.Ip); if (resultFromLib != null) { WriteIpInDbRequest writeInDbRequest = new WriteIpInDbRequest//to investigate why extension method dont work { Ip = ipRequest.Ip, City = resultFromLib.City, Country = resultFromLib.Country, Continent = resultFromLib.Continent, Latitude = resultFromLib.Latitude, Longitude = resultFromLib.Longitude }; dbService.WriteIpDetailsinDataBase(writeInDbRequest); cacheService.Add(ipRequest.Ip, ipRequest.Ip, DateTimeOffset.UtcNow.AddHours(1)); //must return the ip from library } } return(new GetIpDetailsResponse());//must fix this }
public static WriteIpInDbRequest ConvertRequest(this IpResponse ipResponse, string ip) { return(new WriteIpInDbRequest { Ip = ip, City = ipResponse.City, Country = ipResponse.Country, Continent = ipResponse.Continent, Latitude = ipResponse.Latitude, Longitude = ipResponse.Longitude }); }
private static string TryGetOrgName(IpResponse value) { if (value == null) { return(null); } string name = null; foreach (var entity in value.Entities) { if (entity.VCard != null && entity.VCard.TryGetValue("fn", out ContactCardProperty prop)) { name = prop.Value.FirstOrDefault(); if (!string.IsNullOrWhiteSpace(name)) { break; } } } return(name); }
public async Task <ActionResult <IpResponse> > Post([FromBody] DomainRequest domain) { List <ResultDetail> resultDetailList; IpResponse ipResponse = new IpResponse(); ValidationResult validationResult = _ipRequestValidator.Validate(domain); // Errors will never be null. if (validationResult.Errors.Any()) { resultDetailList = _errorMapper.Map(validationResult); ipResponse = new IpResponse { Result = new Result { ResultCode = "Warning", ResultDetailList = resultDetailList } }; return(StatusCode(400, ipResponse)); } try { var geoUrl = _configuration.GetSection("ServicesUrl").GetSection("GeoUrl").Value; switch (domain.services) { case ServiceData.Dns: var dnsData = await _domainService.GetDnsDetails(domain.IpAddress); ipResponse.DnsInfo = _idnsMapper.Map(dnsData); break; case ServiceData.Geo: var geoData = await _domainService.GetGeoDetails(domain.IpAddress, geoUrl); ipResponse.GeoData = _ipMapper.Map(geoData); break; case ServiceData.Ping: var data = await _domainService.GetPingDetails(domain.IpAddress); ipResponse.PingInfo = _ipingMapper.Map(data); break; case ServiceData.Rdap: var rdapData = await _domainService.GetRdapDetails(domain.IpAddress); ipResponse.RdapInfo = _irdapMapper.Map(rdapData); break; default: ipResponse = await ExecuteAllServiceAsync(domain.IpAddress, geoUrl); break; } } catch (Exception exception) { if (exception?.InnerException?.Data?.Values.Count > 0) { var exceptionData = (List <Error>)(exception.InnerException.Data["Error"]); resultDetailList = _errorMapper.Map(exceptionData); } else { ResultDetail resultDetail = new ResultDetail { Message = exception.Message }; resultDetailList = new List <ResultDetail> { resultDetail }; } ipResponse = SetErrorResponseForPost(resultDetailList); return(StatusCode(500, ipResponse)); } return(ipResponse); }