LoginSignup
22
1

More than 1 year has passed since last update.

Reqを使ってみる (Elixir)

Last updated at Posted at 2022-01-13

昔より 主を内海の 野間なれば 報いを待てや 羽柴筑前

Advent Calendar 2022 13日目1の記事です。
I'm ready for 12/25,2022 :santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5:
I'm looking forward to 12/25,2022 :santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5:
私のAdvent Calendar 2022 一覧

はじめに

Elixirを楽しんでいますか:bangbang::bangbang::bangbang:

$\huge{Enjoy\ Elixir🚀🚀🚀}$

今回は、Reqを使ってみます。

Req is an HTTP client with a focus on ease of use and composability, built on top of Finch.

What is Req ?

ElixirでHTTP通信するHexと言えば何をおもいうかべますか?

私は、HTTPoisonです。

それで昨日、Nerves LivebookにデフォルトでHTTPoisonを入れてみない? というプルリクを出してみました。

そうしたら、Reqはどうだい? という返事をいただきました。

Reqって何だい? ということで調べてみました。

Req is an HTTP client with a focus on ease of use and composability, built on top of Finch.

Youngです!

をみると、初登場はJul 15, 2021です。

Jasonが依存ライブラリに入っていて、JSONをいい感じに扱ってくれます。

HTTP Get!

Mix.install [{:req, "~> 0.2.1"}]

"https://qiita.com/api/v2/items?query=tag:Elixir"
|> URI.encode()
|> Req.get!()
|> Map.get(:body)
|> Enum.map(& Map.take(&1, ["title", "url"]))

:tada::tada::tada:

@mnishiguchi さんの実装例が参考になりますのでご紹介しておきます。
HTTPoisonReqに置き換えられています:rocket::rocket::rocket:

  def fetch_current_weather!() do
-    %HTTPoison.Response{status_code: 200, body: body} = HTTPoison.get!(@weather_url)
-    [current_weather] = body |> Jason.decode!() |> Access.fetch!("current_condition")
+    %Req.Response{status: 200, body: body} = Req.get!(@weather_url)
+    [current_weather] = body |> Access.fetch!("current_condition")

HTTP POST!

LINEのAPIでの使用例を書いてみました。

Mix.install [{:req, "~> 0.2.1"}]

reply_token = "reply_token"
text = "text"
channel_access_token = "channel_access_token"

json =
  %{
    replyToken: reply_token,
    messages: [
      %{
        type: "text",
        text: text
      }
    ]
  }
  |> Jason.encode!()

url = "https://api.line.me/v2/bot/message/reply"

headers = [
  "Content-type": "application/json",
  Authorization: "Bearer #{channel_access_token}"
]

%Req.Response{status: 200} = Req.post!(url, json, headers: headers)

こんな感じです。
楽しんでください:rocket::rocket::rocket:


こういうときにHTTP POSTの例を書くのにいいやつ知りませんか〜
あー 思い出した。

がよさそうですね!!!

Mix.install [{:req, "~> 0.2.1"}]

json = %{ enjoy: "Elixir" } |> Jason.encode!()
%Req.Response{body: %{"json" => something}, status: 200}
  = Req.post!("https://httpbin.org/post", json)

something["enjoy"] == "Elixir" # true

また、こんな使い方もできます!

iex> Req.post!("https://httpbin.org/post", {:json, %{ enjoy: %{Elixir: "awesome"} }})

コメント欄をご覧ください。

@mnishiguchi さんのコメントから調べました。
コメントありがとうございます。

Wrapping up :lgtm::lgtm::lgtm::lgtm::lgtm:

Enjoy Elixir:bangbang::bangbang::bangbang:

$\huge{I\ like\ Req\ 👍}$

2022年に流行る技術予想 ーー それは、Nerves Livebookです:rocket::rocket::rocket:


I organize autoracex.
And I belong to NervesJP.
I hope someday you'll join us.

We Are The Alchemists, my friends!

22
1
3

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
22
1