コード例 #1
0
        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);
        }