 defmodule EspiDni.GoogleRealtimeClient do
 
   require Logger
-  import Ecto.Query, only: [from: 1, from: 2]
-  alias EspiDni.ViewCount
-  alias EspiDni.Repo
 
   @metrics "rt:pageviews"
   @dimensions "rt:pagePath"

    |> Enum.join(",")
  end

  defp parse_pageviews(response, team) do
    case Poison.decode!(response) do
      %{"columnHeaders" => headers, "rows" => rows} ->
        Enum.map(rows, fn(row) ->
          parse_viewcount(headers, row, team)
        end)
      _ -> []
    end
  end

  defp parse_viewcount(headers, row, team) do
    %{
      path: get_data(headers, row, @dimensions),
      count: get_data(headers, row, @metrics)

  def get_pageviews(team) do
    case HTTPoison.get(@url, [], params: request_params(team)) do
      {:ok, %HTTPoison.Response{status_code: 200, body: response}} ->
        parse_pageviews(response)
      {:ok, %HTTPoison.Response{status_code: 401, body: response}} ->
        Logger.error "Authentication error #{inspect response}"
        []
