<commit_before>class ApiCarto::API
  def self.search_qp(geojson)
    url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/")
    call(url, geojson)
  end

  def self.search_cadastre(geojson)
    url = [API_CARTO_URL, "cadastre", "geometrie"].join("/")
    call(url, geojson)
  end

  private

  def self.call(url, geojson)
    params = geojson.to_s
    RestClient.post(url, params, content_type: 'application/json')

  rescue RestClient::InternalServerError, RestClient::BadGateway, RestClient::GatewayTimeout, RestClient::ServiceUnavailable => e
    Rails.logger.error "[ApiCarto] Error on #{url}: #{e}"
    raise RestClient::ResourceNotFound
  end
end<commit_msg>[API Carto]: use typhoeus and correctly handle errors<commit_after>class ApiCarto::API
  def self.search_qp(geojson)
    url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/")
    call(url, geojson)
  end

  def self.search_cadastre(geojson)
    url = [API_CARTO_URL, "cadastre", "geometrie"].join("/")
    call(url, geojson)
  end

  private

  def self.call(url, geojson)
    response = Typhoeus.post(url, body: geojson.to_s, headers: { 'content-type' => 'application/json' })

    if response.success?
      response.body
    else
      message = response.code == 0 ? response.return_message : response.code.to_s
      Rails.logger.error "[ApiCarto] Error on #{url}: #{message}"
      raise RestClient::ResourceNotFound
    end
  end
end
{'fraction_non_alphanumeric': 0.13426573426573427, 'fraction_numerical': 0.0006993006993006993, 'mean_word_length': 4.184782608695652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1170917', 'n_tokens_mistral': 509, 'n_tokens_neox': 486, 'n_words': 103}