Last active
January 27, 2021 16:02
-
-
Save zhongwencool/6cd44df1acd699fc9c7159882ef3b597 to your computer and use it in GitHub Desktop.
maxwell vs httpoison vs httppotion examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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