LoginSignup
2
0

More than 3 years have passed since last update.

Elixirでレスポンスを加工する。

Last updated at Posted at 2020-10-08

poisonライブラリを使います。

  • 2020年10月9日12:00加筆
    • Jasonを使った方が良さそうです。
    • 詳細はこちらです。

前提
httpoisonを使っている。

やること

depsに追記


defp deps do
  [

      { :httpoison, "~> 0.8" },
      { :poison, "~> 1.5" }, #この行を追記します。

  ]
end

install

mix deps.get

moduleを編集

Poison.Parser.parse!を使います。


  def handle_response({ :ok, %{status_code: 200, body: body}}) do
    { :ok, Poison.Parser.parse!(body) }
  end

確認

加工されたレスポンスが返ってきます。


{:ok,
 [
   %{
     "active_lock_reason" => nil,
     "assignee" => nil,
     "assignees" => [],
     "author_association" => "NONE",
     "body" => "```\r\nwarning: you must require CommonsPub.Circles.Circle.Migration before invoking the macro CommonsPub.Circles.Circle.Migration.create_circle_table/1\r\n  lib/circle.ex:53: CommonsPub.Circles.Circle.Migration.migrate_circle/1\r\n```",
     "closed_at" => nil,
     "comments" => 2,
     "comments_url" => "https://api.github.com/repos/elixir-lang/elixir/issues/10401/comments",
     "created_at" => "2020-10-06T11:13:20Z",
     "events_url" => "https://api.github.com/repos/elixir-lang/elixir/issues/10401/events",
     "html_url" => "https://github.com/elixir-lang/elixir/issues/10401",
     "id" => 715574131,
     "labels" => [
       %{
         "color" => "e10c02",
         "default" => false,
         "description" => nil,
         "id" => 207974,
         "name" => "Kind:Bug",
         "node_id" => "MDU6TGFiZWwyMDc5NzQ=",
         "url" => "https://api.github.com/repos/elixir-lang/elixir/labels/Kind:Bug"
       },
       %{
         "color" => "bfdadc",
         "default" => false,
         "description" => nil,
         "id" => 778790,
         "name" => "Note:Discussion",
         "node_id" => "MDU6TGFiZWw3Nzg3OTA=",
         "url" => "https://api.github.com/repos/elixir-lang/elixir/labels/Note:Discussion"
       }
     ],
     "labels_url" => "https://api.github.com/repos/elixir-lang/elixir/issues/10401/labels{/name}",
     "locked" => false,
     "milestone" => nil,
     "node_id" => "MDU6SXNzdWU3MTU1NzQxMzE=",
     "number" => 10401,
     "performed_via_github_app" => nil,
     "repository_url" => "https://api.github.com/repos/elixir-lang/elixir",
     "state" => "open",
     "title" => "fully qualifying a macro in the current module produces a warning about needing to require it",
     "updated_at" => "2020-10-06T12:15:23Z",
     "url" => "https://api.github.com/repos/elixir-lang/elixir/issues/10401",
     "user" => %{
       "avatar_url" => "https://avatars1.githubusercontent.com/u/65081?v=4",
       "events_url" => "https://api.github.com/users/jjl/events{/privacy}",
       "followers_url" => "https://api.github.com/users/jjl/followers",
       "following_url" => "https://api.github.com/users/jjl/following{/other_user}",
       "gists_url" => "https://api.github.com/users/jjl/gists{/gist_id}",
       "gravatar_id" => "",
       "html_url" => "https://github.com/jjl",
       "id" => 65081,
       "login" => "jjl",
       "node_id" => "MDQ6VXNlcjY1MDgx",
       "organizations_url" => "https://api.github.com/users/jjl/orgs",
       "received_events_url" => "https://api.github.com/users/jjl/received_events",
       "repos_url" => "https://api.github.com/users/jjl/repos",
       "site_admin" => false,
       "starred_url" => "https://api.github.com/users/jjl/starred{/owner}{/repo}",
       "subscriptions_url" => "https://api.github.com/users/jjl/subscriptions",
       "type" => "User",
       "url" => "https://api.github.com/users/jjl"
     }
   },
   %{
     "active_lock_reason" => nil,
     "assignee" => nil,
     "assignees" => [],
     "author_association" => "MEMBER",
     "body" => "This is a proposal to address #10372.\r\n\r\nThe parser is a MFArgs which receives the current input, the parsing options, and the buffer. It returns either `{:ok, expr, buffer}` or `{:incomplete, buffer}`. Buffer always starts as an empty string. Here is the default implementation:\r\n\r\n```elixir\r\n@break_trigger \"#iex:break\\n\"\r\n\r\ndef parse(@break_trigger, _opts, \"\") do\r\n  {:incomplete, \"\"}\r\nend\r\n\r\ndef parse(@break_trigger, opts, _buffer) do\r\n  :elixir_errors.parse_error([line: opts[:line]], opts[:file], \"incomplete expression\", \"\")\r\nend\r\n\r\ndef parse(input, opts, buffer) do\r\n  input = buffer <> input\r\n  \r\n  case Code.string_to_quoted(input, opts) do\r\n    {:ok, forms} ->\r\n      {:ok, forms, \"\"}\r\n\r\n    {:error, {_, _, \"\"}} ->\r\n      {:incomplete, input}\r\n\r\n    {:error, {location, error, token}} ->\r\n      :elixir_errors.parse_error(location, opts[:file], error, token)\r\n  end\r\nend\r\n```\r\n\r\nThis code was extracted [from the evaluator](https://github.com/elixir-lang/elixir/blob/master/lib/iex/lib/iex/evaluator.ex#L204), which should be changed to call a configurable MFArgs. The options are `[file: \"iex\", line: iex_state.counter]`. If the parser raises, the buffer is reset to an empty string. \r\n\r\nThe parser shall be configurable via `IEx.configure`. A PR is welcome!",
     "closed_at" => nil,

加工前はこんな感じでした。


def handle_response({ :ok, %{status_code: 200, body: body})) do
  { :ok, body }
end

レスポンス結果



{:ok,
 "[{\"url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10401\",\"repository_url\":\"https://api.github.com/repos/elixir-lang/elixir\",\"labels_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10401/labels{/name}\",\"comments_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10401/comments\",\"events_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10401/events\",\"html_url\":\"https://github.com/elixir-lang/elixir/issues/10401\",\"id\":715574131,\"node_id\":\"MDU6SXNzdWU3MTU1NzQxMzE=\",\"number\":10401,\"title\":\"fully qualifying a macro in the current module produces a warning about needing to require it\",\"user\":{\"login\":\"jjl\",\"id\":65081,\"node_id\":\"MDQ6VXNlcjY1MDgx\",\"avatar_url\":\"https://avatars1.githubusercontent.com/u/65081defmodule Issues.GithubIssues do
?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/jjl\",\"html_url\":\"https://github.com/jjl\",\"followers_url\":\"https://api.github.com/users/jjl/followers\",\"following_url\":\"https://api.github.com/users/jjl/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/jjl/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/jjl/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/jjl/subscriptions\",\"organizations_url\":\"https://api.github.com/users/jjl/orgs\",\"repos_url\":\"https://api.github.com/users/jjl/repos\",\"events_url\":\"https://api.github.com/users/jjl/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/jjl/received_events\",\"type\":\"User\",\"site_admin\":false},\"labels\":[{\"id\":207974,\"node_id\":\"MDU6TGFiZWwyMDc5NzQ=\",\"url\":\"https://api.github.com/repos/elixir-lang/elixir/labels/Kind:Bug\",\"name\":\"Kind:Bug\",\"color\":\"e10c02\",\"default\":false,\"description\":null},{\"id\":778790,\"node_id\":\"MDU6TGFiZWw3Nzg3OTA=\",\"url\":\"https://api.github.com/repos/elixir-lang/elixir/labels/Note:Discussion\",\"name\":\"Note:Discussion\",\"color\":\"bfdadc\",\"default\":false,\"description\":null}],\"state\":\"open\",\"locked\":false,\"assignee\":null,\"assignees\":[],\"milestone\":null,\"comments\":2,\"created_at\":\"2020-10-06T11:13:20Z\",\"updated_at\":\"2020-10-06T12:15:23Z\",\"closed_at\":null,\"author_association\":\"NONE\",\"active_lock_reason\":null,\"body\":\"```\\r\\nwarning: you must require CommonsPub.Circles.Circle.Migration before invoking the macro CommonsPub.Circles.Circle.Migration.create_circle_table/1\\r\\n  lib/circle.ex:53: CommonsPub.Circles.Circle.Migration.migrate_circle/1\\r\\n```\",\"performed_via_github_app\":null},{\"url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10390\",\"repository_url\":\"https://api.github.com/repos/elixir-lang/elixir\",\"labels_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10390/labels{/name}\",\"comments_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10390/comments\",\"events_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/10390/events\",\"html_url\":\"https://github.com/elixir-lang/elixir/issues/10390\",\"id\":714055869,\"node_id\":\"MDU6SXNzdWU3MTQwNTU4Njk=\",\"number\":10390,\"title\":\"Make IEx parser customizable\",\"user\":{\"login\":\"josevalim\",\"id\":9582,\"node_id\":\"MDQ6VXNlcjk1ODI=\",\"avatar_url\":\"https://avatars0.githubusercontent.com/u/9582?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/josevalim\",\"html_url\":\"https://github.com/josevalim\",\"followers_url\":\"https://api.github.com/users/josevalim/followers\",\"following_url\":\"https://api.github.com/users/josevalim/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/josevalim/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/josevalim/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/josevalim/subscriptions\",\"organizations_url\":\"https://api.github.com/users/josevalim/orgs\",\"repos_url\":\"https://api.github.com/users/josevalim/repos\",\"events_url\":\"https://api.github.com/users/josevalim/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/josevalim/received_events\",\"type\":\"User\",\"site_admin\":false},\"labels\":[{\"id\":16893337,\"node_id\":\"MDU6TGFiZWwxNjg5MzMzNw==\",\"url\":\"https://api.github.com/repos/elixir-lang/elixir/labels/App:IEx\",\"name\":\"App:IEx\",\"color\":\"CCCCCC\",\"default\":false,\"description\":null},{\"id\":207975,\"no" <> ...}

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0