lib/shodanz/apis/utils.rb in shodanz-2.0.1 vs lib/shodanz/apis/utils.rb in shodanz-2.0.2

- old
+ new

@@ -59,55 +59,71 @@ facets.select { |key, _| key == :facets } end private - RATELIMIT = 'rate limit reached' - NOINFO = 'no information available' - NOQUERY = 'empty search query' + RATELIMIT = 'rate limit reached' + NOINFO = 'no information available' + NOQUERY = 'empty search query' + ACCESSDENIED = 'access denied' + INVALIDKEY = 'invalid API key' def handle_any_json_errors(json) - return json unless json.is_a?(Hash) && json.key?('error') - - raise Shodanz::Errors::RateLimited if json['error'].casecmp(RATELIMIT) >= 0 - raise Shodanz::Errors::NoInformation if json['error'].casecmp(NOINFO) >= 0 - raise Shodanz::Errors::NoQuery if json['error'].casecmp(NOQUERY) >= 0 - - json + if json.is_a?(Hash) && json.key?('error') + raise Shodanz::Errors::RateLimited if json['error'].casecmp(RATELIMIT) >= 0 + raise Shodanz::Errors::NoInformation if json['error'].casecmp(NOINFO) >= 0 + raise Shodanz::Errors::NoQuery if json['error'].casecmp(NOQUERY) >= 0 + raise Shodanz::Errors::AccessDenied if json['error'].casecmp(ACCESSDENIED) >= 0 + raise Shodanz::Errors::InvalidKey if json['error'].casecmp(INVALIDKEY) >= 0 + end + return json end - def getter(path, **params) + def getter(path, one_shot=false, **params) # param keys should all be strings params = params.transform_keys(&:to_s) # build up url string based on special params url = "#{@url}#{path}?key=#{@key}" # special params params.each do |param,value| - url += "&#{param}=#{value}" unless value.is_a?(String) && value.empty? + next if value.is_a?(String) && value.empty? + value = URI.encode_www_form_component("#{value}") + url += "&#{param}=#{value}" end + resp = @internet.get(url) # parse all lines in the response body as JSON json = JSON.parse(resp.body.join) + @internet.close if one_shot + handle_any_json_errors(json) + + return json ensure resp&.close + @internet = Async::HTTP::Internet.new if one_shot end - def poster(path, **params) + def poster(path, one_shot=false, **params) # param keys should all be strings params = params.transform_keys(&:to_s) # make POST request to server resp = @internet.post("#{@url}#{path}?key=#{@key}", params) # parse all lines in the response body as JSON json = JSON.parse(resp.body.join) + @internet.close if one_shot + handle_any_json_errors(json) + + return json ensure resp&.close + @internet = Async::HTTP::Internet.new if one_shot end def slurper(path, **params) # param keys should all be strings params = params.transform_keys(&:to_s) @@ -133,28 +149,28 @@ resp&.close end def async_get(path, **params) Async::Task.current.async do - getter(path, **params) + getter(path, false, **params) end end def sync_get(path, **params) Async do - getter(path, **params) + getter(path, true, **params) end.wait end def async_post(path, **params) Async::Task.current.async do - poster(path, **params) + poster(path, false, **params) end end def sync_post(path, **params) Async do - poster(path, **params) + poster(path, true, **params) end.wait end def async_slurp_stream(path, **params) Async::Task.current.async do