Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

<!-- livebook:{"autosave_interval_s":30,"persist_outputs":true} --> # ecto_box ```elixir # Mix.install([ # {:req, "~> 0.3.11"} # ]) ``` <!-- livebook:{"output":true} --> ``` nil ``` ## Usage example ```elixir defmodule GithubClient do import EctoBox import EctoBox.HttpResponseMapper, only: [extract_body: 3] defschema Owner do field(:id, :integer) field(:login, :string) end defschema Repository do field(:full_name, :string) field(:has_downloads, :boolean) field(:owner, Owner) end defschema Error do field(:message, :string) end @type error :: Error.t() | Exception.t() | atom @spec get_public_repo(binary()) :: {:ok, Repository.t()} | {:error, error()} def get_public_repo(name) do "https://api.github.com/repos/#{name}" |> Req.get() |> extract_body(Repository, Error) end end ``` <!-- livebook:{"output":true} --> ``` {:module, GithubClient, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:get_public_repo, 1}} ``` The request below returns `HTTP 200` - and should be converted into `Repository` schema: ```elixir GithubClient.get_public_repo("wojtekmach/req") ``` <!-- livebook:{"output":true} --> ``` {:ok, %GithubClient.Repository{ full_name: "wojtekmach/req", has_downloads: true, owner: %GithubClient.Owner{id: 76071, login: "wojtekmach"} }} ``` While this request returns `HTTP 400` and will be converted to `Error` schema: ```elixir GithubClient.get_public_repo("foo/bar") ``` <!-- livebook:{"output":true} --> ``` {:error, %GithubClient.Error{message: "Not Found"}} ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More