Skip to content

Instantly share code, notes, and snippets.

@zhongwencool
Last active January 27, 2021 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhongwencool/6cd44df1acd699fc9c7159882ef3b597 to your computer and use it in GitHub Desktop.
Save zhongwencool/6cd44df1acd699fc9c7159882ef3b597 to your computer and use it in GitHub Desktop.
maxwell vs httpoison vs httppotion examples
defmodule Httpoison.GitHub do
use HTTPoison.Base
def process_url(url) do
"https://api.github.com" <> url
end
def process_request_headers(headers) do
Dict.put headers, :"User-Agent", "github-httpoison"
end
def process_response_body(body) do
body
|> Poison.decode!
end
def get_public_repos(name) do
"users/" <> name
|>get().body["public_repos"]
end
end
iex>Httpoison.GitHub.start
iex> Httpoison.GitHub.get_public_repos("zhongwencool")
defmodule HttpPotion.GitHub do
use HTTPotion.Base
def process_url(url) do
"https://api.github.com/" <> url
end
def process_request_headers(headers) do
Dict.put headers, :"User-Agent", "github-httpotion"
end
def process_response_body(body) do
body |> IO.iodata_to_binary |> :jsx.decode
|> :orddict.from_list
end
def get_pubilc_repos(name) do
"users/" <> name
|> get().body["public_repos"]
end
end
iex> HttpPotion.Github.get_public_repos("zhongwencool")
defmodule Maxwell.Client do
use Maxwell.Builder, ~w(get post put)a
adapter Maxwell.Adapter.Ibrowse
# or adapter Maxwell.Adapter.Hackney
middleware Maxwell.Middleware.BaseUrl, "https://api.github.com"
middleware Maxwell.Middleware.Headers, %{'User-Agent': "github-maxwell"}
middleware Maxwell.Middleware.DecodeJson
def get_public_repos(name) do
url("users/" <> name)
|> get!
|> get_in([:body, "public_repos"])
end
end
iex> Maxwell.Github.get_public_repos("zhongwencool")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment