LoginSignup
3
0

More than 3 years have passed since last update.

Enjoy Elixir #006 HTTP GET!

Last updated at Posted at 2020-07-07
1 / 8

はじめに

  • KFIEという近畿大学産業理工学部の情報系コミュニティがあります
  • 最近は、毎週火曜日にLT会をやっているそうです
  • 私が学生だったのはもうずいぶん昔のことなのですが、参加させてもらっています
  • 毎週、5分間時間をもらって、Elixirいいよ! を伝えていきたいとおもいます
  • 今日は以下を学びます
    • HTTP GET!
  • A journey of a thousand miles begins with a single step.
  • この記事はElixirを触るのがはじめてという方に向けて書いています

もくじ

001 mix new, iex -S mix, mix format
|> 002 型
|> 003 Pattern matching
|> 004 Modules and functions
|> 005 Pipe operator and Enum module
|> 006 HTTP GET!
|> 007 Flow
|> 008 AtCoderを解いてみる
|> 999 Where to go next


準備

$ mix new hello_http
$ cd hello_http

依存関係の解消

  • hexでパッケージをさがします
  • 今回は以下を使います
    • httpoison
      • Yet Another HTTP client for Elixir powered by hackney
    • jason
      • A blazing fast JSON parser and generator in pure Elixir.
  • 上記のページの右のほうに書いてある記述を参考にdepsを書き換えます

スクリーンショット 2020-07-07 17.11.42.png

mix.exs
  defp deps do
    [
      {:httpoison, "~> 1.7"},
      {:jason, "~> 1.2"}
    ]
  end
$ mix deps.get

HTTP GET!

  • ここではQiitaAPIを利用してみましょう
  • 記事では結果だけ書いていますが、発表のときにはiexで少しずつ結果を確かめながら作っていくさまをお見せできたらとおもっています
lib/hello_http.ex
defmodule HelloHttp do
  @query URI.encode_query(%{"query" => "tag:Elixir"})

  def run do
    "https://qiita.com/api/v2/items?#{@query}"
    |> HTTPoison.get()
    |> handle_response()
    |> Jason.decode!()
  end

  defp handle_response({:ok, %HTTPoison.Response{body: body, status_code: 200}}) do
    body
  end

  defp handle_response(_) do
    raise "error"
  end
end

実行

$ iex -S mix

iex> HelloHttp.run

GETはわかったけどその後、それをどうしたらいいの?


Wrapping Up

  • 今日のポイントは、hexの探し方と、mix deps.getです
  • サンプルコードのtagにお好みの言語を設定してHTTP GETをお楽しみください
  • 次回は、Flow
    • 来週を待ちきれない方は、リソースやコミュニティの情報を Where to go next にまとめていますのでダイブしてください!
  • Enjoy!!!
3
0
0

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
3
0