Current section

Files

Jump to
smallex lib json.ex
Raw

lib/json.ex

defmodule Json do
@moduledoc """
JSON API call library (with parse support).
"""
@doc """
Get JSON API (header & map_function are optional)
## Examples
iex> Json.get( "https://api.github.com", "/rate_limit" )[ "rate" ][ "limit" ]
60
"""
def get( domain, path, header \\ [], map_function \\ &nop/1 ) do
domain <> path
|> HTTPoison.get!( header )
|> parse( map_function )
end
@doc """
Post JSON API (header & map_function are optional)
## Examples
iex> Json.post( "https://httpbin.org", "/post?param1=value1", "{ data1:value1 }" )[ "args" ]
%{"param1" => "value1"}
iex> Json.post( "https://httpbin.org", "/post?param1=value1", "{ data1:value1 }" )[ "data" ]
"{ data1:value1 }"
"""
def post( domain, path, body, header \\ [], map_function \\ &nop/1 ) do
domain <> path
|> HTTPoison.post!( body, header )
|> parse( map_function )
end
@doc """
Put JSON API (header & map_function are optional)
## Examples
iex> Json.put( "https://httpbin.org", "/put?param1=value1", "{ data1:value1 }" )[ "args" ]
%{"param1" => "value1"}
"""
def put( domain, path, body, header \\ [], map_function \\ &nop/1 ) do
domain <> path
|> HTTPoison.put!( body, header )
|> parse( map_function )
end
@doc """
Patch JSON API (header & map_function are optional)
## Examples
iex> Json.patch( "https://httpbin.org", "/patch?param1=value1", "{ data1:value1 }" )[ "args" ]
%{"param1" => "value1"}
"""
def patch( domain, path, body, header \\ [], map_function \\ &nop/1 ) do
domain <> path
|> HTTPoison.patch!( body, header )
|> parse( map_function )
end
@doc """
Delete JSON API (header & map_function are optional)
## Examples
iex> ( Json.delete( "https://httpbin.org", "/delete?param1=value1" ) |> Poison.decode! )[ "args" ]
%{"param1" => "value1"}
"""
def delete( domain, path, header \\ [] ) do
domain <> path
|> HTTPoison.delete!( header )
|> get_body
end
@doc """
Head JSON API (header & map_function are optional)
## Examples
iex> Json.head( "https://httpbin.org", "/", [] )
""
"""
def head( domain, path, header \\ [] ) do
domain <> path
|> HTTPoison.head!( header )
|> get_body
end
defp parse( response, map_function ) do
response
|> get_body
|> Poison.decode!
|> map_function.()
end
defp get_body( %{ status_code: 200, body: body } ), do: body
defp get_body( %{ status_code: 204, body: body } ), do: body
defp nop( map_list ), do: map_list
end