public async Task <BuiltWithModel> GetAsync(string hostUrl) { try { if (string.IsNullOrEmpty(hostUrl)) { throw new ArgumentNullException($"param {hostUrl} was null."); } var client = _client.CreateClient(); string apiKey = await _config.ReadConfiguration(TellMeMoreLogger.BuiltWithApiKey); var res = await client.GetAsync($"{BaseUrl}{apiKey}&LOOKUP={hostUrl}"); if (res.IsSuccessStatusCode) { var json = JsonConvert.DeserializeObject <BuiltWithModel>(await res?.Content?.ReadAsStringAsync()); return(json); } else if (res.StatusCode == System.Net.HttpStatusCode.TooManyRequests) { throw new urlScanIoException("We're quite busy at the moment, please try again in a minute or so."); } else { throw new HttpRequestException($"Request to end-point {BaseUrl} failed. Code was: {res?.StatusCode}"); } } catch (Exception ex) { throw ex; } }
public DnsDumpsterService(IHttpClientFactory httpClientFactory, ITellMeMoreLogger configuration) { _client = httpClientFactory; _config = configuration; BaseUrl = _config.ReadConfiguration(TellMeMoreLogger.DnsDumpsterBaseUrl)?.Result; }
/// <summary> /// Submit scan job to urlscan.io /// </summary> /// <param name="hostName"></param> /// <returns></returns> public async Task <urlScanIoNewScanResponse> PostAsync(string hostName) { try { var client = _httpClientFactory.CreateClient(HttpClientNames.urlScanIoHttpClient); string apiKey = await _config.ReadConfiguration(TellMeMoreLogger.UrlScanApiKey); client.DefaultRequestHeaders.Add("API-Key", apiKey); var reqObject = new urlScanIoRequestObject() { url = hostName }; var res = await client.PostAsync(BaseUrlScan, new StringContent( JsonConvert.SerializeObject(reqObject), Encoding.UTF8, "application/json") ); if (res.IsSuccessStatusCode) { var json = JsonConvert.DeserializeObject <urlScanIoNewScanResponse>(await res?.Content?.ReadAsStringAsync()); return(json); } else if (res.StatusCode == System.Net.HttpStatusCode.TooManyRequests) { throw new urlScanIoException("We're quite busy at the moment, please try again in a minute or so."); } else { throw new HttpRequestException($"Failed request to {BaseUrlScan}. Failed to start scan for URL: {hostName}"); } } catch (Exception ex) { throw ex; } }