Parse API return

Hello everyone,

I’m trying to parse this return to json in the phoenix.

%{"statuses_url" => "https://api.github.com/repos/sproutapp/pavlov/statuses/{sha}",
       "git_refs_url" => "https://api.github.com/repos/sproutapp/pavlov/git/refs{/sha}",
       "issue_comment_url" => "https://api.github.com/repos/sproutapp/pavlov/issues/comments{/number}",
       "watchers" => 128, "mirror_url" => nil,
       "languages_url" => "https://api.github.com/repos/sproutapp/pavlov/languages",
       "stargazers_count" => 128,
       "license" => %{"key" => "mit", "name" => "MIT License",
         "node_id" => "MDc6TGljZW5zZTEz", "spdx_id" => "MIT",
         "url" => "https://api.github.com/licenses/mit"}, "forks" => 6,
       "default_branch" => "master",
       "comments_url" => "https://api.github.com/repos/sproutapp/pavlov/comments{/number}",
       "commits_url" => "https://api.github.com/repos/sproutapp/pavlov/commits{/sha}",
       "id" => 27221358,
       "clone_url" => "https://github.com/sproutapp/pavlov.git",
       "homepage" => nil,
       "stargazers_url" => "https://api.github.com/repos/sproutapp/pavlov/stargazers",
       "events_url" => "https://api.github.com/repos/sproutapp/pavlov/events",
       ...},
     %{"statuses_url" => "https://api.github.com/repos/jonasschmidt/ex_json_schema/statuses/{sha}",
       "git_refs_url" => "https://api.github.com/repos/jonasschmidt/ex_json_schema/git/refs{/sha}",
       "issue_comment_url" => "https://api.github.com/repos/jonasschmidt/ex_json_schema/issues/comments{/number}",
       "watchers" => 127, "mirror_url" => nil,
       "languages_url" => "https://api.github.com/repos/jonasschmidt/ex_json_schema/languages",
       "stargazers_count" => 127,
       "license" => %{"key" => "mit", "name" => "MIT License",
         "node_id" => "MDc6TGljZW5zZTEz", "spdx_id" => "MIT",
         "url" => "https://api.github.com/licenses/mit"}, "forks" => 34,
       "default_branch" => "master",
       "comments_url" => "https://api.github.com/repos/jonasschmidt/ex_json_schema/comments{/number}",
       "commits_url" => "https://api.github.com/repos/jonasschmidt/ex_json_schema/commits{/sha}",
       "id" => 36134727,
       "clone_url" => "https://github.com/jonasschmidt/ex_json_schema.git",
       "homepage" => "",
       "stargazers_url" => "https://api.github.com/repos/jonasschmidt/ex_json_schema/stargazers",
       ...},

But without success, can anyone help?

I tried that way …

def index(conn, _params) do
    repos = Githubapp.search_repo("elixir-lang language:elixir") 
      repos
      |> Enum.map(&Enum.zip(repos, &1))
      |> Enum.map(&Enum.into(&1, %{}))
    render conn, "index.json", repos: repos
  end

defmodule GithubappWeb.PageView do
  use GithubappWeb, :view

  def render("index.json", %{repos: repos}) do
    %{data: render_many(repos, GithubappWeb.PageView, "repo.json")}
  end

  def render("repo.json", repos) do
    %{data: repos}
  end
end

Thank you all!

What errors/outputs are you getting?

[error] #PID<0.418.0> running GithubappWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: GET /api/github
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Enumerable not implemented for {200, %{"incomplete_results" => false, "items" => [%{"statuses_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/statuses/{sha}", "git_refs_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/git/refs{/sha}", "issue_comment_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/issues/comments{/number}", "watchers" => 2714, "mirror_url" => nil, "languages_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/languages", "stargazers_count" => 2714, "license" => nil, "forks" => 208, "default_branch" => "master", "comments_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/comments{/number}", "commits_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/commits{/sha}", "id" => 13060236, "clone_url" => "https://github.com/christopheradams/elixir_style_guide.git", "homepage" => nil, "stargazers_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/stargazers", "events_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/events", "blobs_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/git/blobs{/sha}", "forks_count" => 208, "score" => 75.93137, "pushed_at" => "2018-04-18T15:20:01Z", "git_url" => "git://github.com/christopheradams/elixir_style_guide.git", "hooks_url" => "https://api.github.com/repos/christopheradams/elixir_style_guide/hooks", "owner" => %{"avatar_url" => "https://avatars3.githubusercontent.com/u/60711?v=4", "events_url" => "https://api.github.com/users/christopheradams/events{/privacy}", "followers_url" => "https://api.github.com/users/christopheradams/followers", "following_url" => "https://api.github.com/users/christopheradams/following{/other_user}", "gists_url" => "https://api.github.com/users/christopheradams/gists{/gist_id}", "gravatar_id" => "", "html_url" => "https://github.com/christopheradams",

Thanks for the feedback and sorry for not getting the error @mbuhot

The error appears to be that you’re trying to enumerate over a tuple instead of something that’s enumerable.

Without knowing more, does Githubapp.search_repo return a tuple of status and response body? It appears that somewhere it returns that and it isn’t handled and that’s what’s tripping you up.

3 Likes

Thanks @mischov, your answer helped me to think better :smiley: