예제 #1
0
        public static string RequestUri(ApiEndPointType endpoint, string host)
        {
            string queryString = endpoint.GetQueryStringParameters(host, ApiKey, OutputFormat);

            string requestUri = string.Empty;

            if (endpoint == ApiEndPointType.GeoIpLocation)
            {
                requestUri = $"{host}{queryString}";
            }
            else if (endpoint == ApiEndPointType.RdapLookup)
            {
                requestUri = $"{endpoint.GetEnumValue().GetValue()}{queryString}";
            }
            else if (endpoint == ApiEndPointType.DomainAvailability)
            {
                requestUri = $"{queryString}";
            }
            else
            {
                requestUri = $"{endpoint.GetEnumValue().GetValue().ToLowerInvariant()}/{queryString}";
            }

            return(requestUri);
        }
예제 #2
0
 public static string RapidApiBaseUri(ApiSettingsType setting, ApiEndPointType endpoint)
 {
     if (setting == ApiSettingsType.GeoLocationSettings)
     {
         return(string.Concat("https://", RapidHeaderHostParameter(setting), "/"));
     }
     else
     {
         return(string.Concat("https://", RapidHeaderHostParameter(setting), "/", endpoint.GetEnumValue().GetValue().ToLowerInvariant(), "/"));
     }
 }
예제 #3
0
        public async Task <ApiResponse> GetAsync(string host, ApiEndPointType endpoint)
        {
            ApiResponse apiResponse;

            try
            {
                client.BaseAddress = new Uri(ApiSettingHelper.BaseUri);
                HttpResponseMessage response = await client.GetAsync(ApiSettingHelper.RequestUri(endpoint, host));

                if (response.IsSuccessStatusCode)
                {
                    string resultContent = await response.Content.ReadAsStringAsync();

                    apiResponse = new ApiResponse()
                    {
                        StatusCode = response.StatusCode,
                        Result     = JObject.Parse(resultContent)
                    };
                }
                else
                {
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        apiResponse = new ApiResponse()
                        {
                            StatusCode   = HttpStatusCode.NotFound,
                            ErrorMessage = response.ReasonPhrase
                        };
                    }
                    else
                    {
                        apiResponse = new ApiResponse()
                        {
                            StatusCode   = HttpStatusCode.InternalServerError,
                            ErrorMessage = "Error performing lookup"
                        };

                        logger.LogWarning("Failed lookup for: {lookup}. StatusCode: {statusCode}; Reason: {reasonPhrase}", host, response.StatusCode.ToString(), response.ReasonPhrase);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Exception occurred calling external API for '{lookup}'", host);

                apiResponse = new ApiResponse()
                {
                    StatusCode   = HttpStatusCode.InternalServerError,
                    ErrorMessage = ex.Message,
                };
            }

            return(apiResponse);
        }
예제 #4
0
        public static string RapidApiRequestUri(ApiSettingsType setting, ApiEndPointType endpoint, string host)
        {
            string baseUri    = RapidApiBaseUri(setting, endpoint);
            string requestUri = string.Empty;

            if (setting == ApiSettingsType.GeoLocationSettings)
            {
                requestUri = string.Concat(baseUri, host, endpoint.GetQueryStringParameters(host, RapidApiToken(setting), RapidApiResponseFormat));
            }
            else
            {
                requestUri = string.Concat(baseUri, endpoint.GetQueryStringParameters(host, RapidApiToken(setting), RapidApiResponseFormat));
            }

            return(requestUri);
        }
예제 #5
0
        public static string GetQueryStringParameters(this ApiEndPointType apiEndPointType, string host, string apiKey, string output)
        {
            string value = string.Empty;

            if (apiEndPointType == ApiEndPointType.PortScanner || apiEndPointType == ApiEndPointType.ReverseIp ||
                apiEndPointType == ApiEndPointType.Ping)
            {
                value = string.Format("?host={0}&apikey={1}&output={2}", host, apiKey, output);
            }
            else if (apiEndPointType == ApiEndPointType.GeoIpLocation)
            {
                value = string.Format("?access_key={0}&fields=main", apiKey);
            }
            else if (apiEndPointType == ApiEndPointType.RdapLookup || apiEndPointType == ApiEndPointType.DomainAvailability)
            {
                value = string.Format("?apiKey={0}&domainName={1}&outputFormat={2}", apiKey, host, output);

                if (apiEndPointType == ApiEndPointType.DomainAvailability)
                {
                    value += $"&mode=DNS_AND_WHOIS";
                }
            }
            else if (apiEndPointType == ApiEndPointType.ReverseDnsLookup)
            {
                value = string.Format("?ip={0}&apikey={1}&output={2}", host, apiKey, output);
            }
            else if (apiEndPointType == ApiEndPointType.DnsLookup || apiEndPointType == ApiEndPointType.AbuseContactLookup ||
                     apiEndPointType == ApiEndPointType.DnsPropagation || apiEndPointType == ApiEndPointType.IpHistoryCheck)
            {
                value = string.Format("?domain={0}&apikey={1}&output={2}", host, apiKey, output);

                if (apiEndPointType == ApiEndPointType.DnsLookup)
                {
                    value += "&recordtype=any";
                }
            }
            else
            {
                value = string.Format("?ip={0}&apikey={1}&output={2}", host, apiKey, output);
            }

            return(value);
        }
예제 #6
0
        public static Enum GetEnumValue(this ApiEndPointType apiEndPointType)
        {
            Enum value = null;

            switch (apiEndPointType)
            {
            case ApiEndPointType.AbuseContactLookup:
                value = ApiEndPointType.AbuseContactLookup;

                break;

            case ApiEndPointType.DnsLookup:
                value = ApiEndPointType.DnsLookup;

                break;

            case ApiEndPointType.DnsPropagation:
                value = ApiEndPointType.DnsPropagation;

                break;

            case ApiEndPointType.DomainAvailability:
                value = ApiEndPointType.DomainAvailability;

                break;

            case ApiEndPointType.GeoIpLocation:
                value = ApiEndPointType.GeoIpLocation;

                break;

            case ApiEndPointType.IpHistoryCheck:
                value = ApiEndPointType.IpHistoryCheck;

                break;

            case ApiEndPointType.Ping:
                value = ApiEndPointType.Ping;

                break;

            case ApiEndPointType.PortScanner:
                value = ApiEndPointType.PortScanner;

                break;

            case ApiEndPointType.RdapLookup:
                value = ApiEndPointType.RdapLookup;

                break;

            case ApiEndPointType.ReverseDnsLookup:
                value = ApiEndPointType.ReverseDnsLookup;

                break;

            case ApiEndPointType.ReverseIp:
                value = ApiEndPointType.ReverseIp;

                break;

            case ApiEndPointType.VirusScan:
                value = ApiEndPointType.VirusScan;

                break;
            }

            return(value);
        }
예제 #7
0
 public static RestClient RapidApiHttpClient(ApiSettingsType setting, ApiEndPointType endpoint, string host)
 {
     return(new RestClient(RapidApiRequestUri(setting, endpoint, host)));
 }