<commit_before>defmodule Exsoda.Reader do
  alias Exsoda.{Soql, Http}
  alias HTTPoison.{AsyncResponse, AsyncStatus, AsyncHeaders, AsyncChunk, AsyncEnd}
  alias NimbleCSV.RFC4180, as: CSV
  require Logger

  defmodule Query do
    defstruct fourfour: nil,
      opts: [],
      query: %{}
  end

  def query(fourfour, options \\ []) do
    %Query{
      fourfour: fourfour,
      opts: Http.options(options),
      query: %{}
    }
  end

  defp view_columns(decoded) do
    columns = decoded
    |> Map.get("columns", [])
    |> Enum.map(fn column -> {column["name"], column["dataTypeName"]} end)
    |> Enum.into(%{})

    {:ok, columns}
  end

  def get_views(fourfours, options \\ []) do
    state = query(nil, options)

    fourfours = fourfours
    |> Enum.map(fn ff -> "ids=#{Http.encode(ff)}" end)
    |> Enum.join("&")
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      "#{base}/views?method=getByIds&#{fourfours}"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def get_view(%Query{fourfour: fourfour} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      "#{base}/views/#{Http.encode(fourfour)}.json"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def get_unpublished_copy(%Query{fourfour: fourfour} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      "#{base}/views/#{Http.encode(fourfour)}.json?method=getPublicationGroup&stage=unpublished"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def get_published_copy(%Query{fourfour: fourfour} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      "#{base}/views/#{Http.encode(fourfour)}.json?method=getPublicationGroup&stage=published"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def replication(%Query{fourfour: fourfour} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      "#{base}/views/#{Http.encode(fourfour)}/replication.json"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def geocoding(%Query{fourfour: fourfour} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      "#{base}/geocoding/#{Http.encode(fourfour)}?method=pending"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  defp get_columns(query) do
    with {:ok, view} <- get_view(query) do<commit_msg>Use the `query` field of `Query` in all places `Query` is used<commit_after>defmodule Exsoda.Reader do
  alias Exsoda.{Soql, Http}
  alias HTTPoison.{AsyncResponse, AsyncStatus, AsyncHeaders, AsyncChunk, AsyncEnd}
  alias NimbleCSV.RFC4180, as: CSV
  require Logger

  defmodule Query do
    defstruct fourfour: nil,
      opts: [],
      query: %{}
  end

  def query(fourfour, options \\ []) do
    %Query{
      fourfour: fourfour,
      opts: Http.options(options),
      query: %{}
    }
  end

  # Add the given get parameters (which should be a map with string keys and values)
  # to the current query; existing parameters with the same keys will be overwritten.
  def with_get_parameters(%Query{query: base_params} = query, %{} = params) do
    %Query{query | query: Map.merge(base_params, params)}
  end

  defp view_columns(decoded) do
    columns = decoded
    |> Map.get("columns", [])
    |> Enum.map(fn column -> {column["name"], column["dataTypeName"]} end)
    |> Enum.into(%{})

    {:ok, columns}
  end

  def get_views(fourfours, options \\ []) do
    state = query(nil, options)

    fourfours = fourfours
    |> Enum.map(fn ff -> "ids=#{Http.encode(ff)}" end)
    |> Enum.join("&")
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      "#{base}/views?method=getByIds&#{fourfours}"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def get_view(%Query{fourfour: fourfour, query: query} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      query = URI.encode_query(query)
      "#{base}/views/#{Http.encode(fourfour)}.json?#{query}"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def get_unpublished_copy(%Query{fourfour: fourfour, query: query} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      query = URI.encode_query(Map.merge(query, %{"method" => "getPublicationGroup", "stage" => "unpublished"}))
      "#{base}/views/#{Http.encode(fourfour)}.json?#{query}"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def get_published_copy(%Query{fourfour: fourfour, query: query} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      query = URI.encode_query(Map.merge(query, %{"method" => "getPublicationGroup", "stage" => "published"}))
      "#{base}/views/#{Http.encode(fourfour)}.json?#{query}"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def replication(%Query{fourfour: fourfour, query: query} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      query = URI.encode_query(query)
      "#{base}/views/#{Http.encode(fourfour)}/replication.json?#{query}"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  def geocoding(%Query{fourfour: fourfour, query: query} = state) do
    with {:ok, base} <- Http.base_url(state),
         {:ok, options} <- Http.http_opts(state) do
      query = URI.encode_query(Map.merge(query, %{ "method" => "pending" }))
      "#{base}/geocoding/#{Http.encode(fourfour)}?#{query}"
      |> HTTPoison.get(Http.headers(state), options)
      |> Http.as_json
    end
  end

  defp get_columns(query) do
    with {:ok, view} <- get_view(query) do
{'fraction_non_alphanumeric': 0.16387693288697594, 'fraction_numerical': 0.001275306870715766, 'mean_word_length': 3.2449255751014885, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 46, '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': '1264115', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2168, 'n_words': 447}