Packages
krug
0.4.8
2.0.36
2.0.35
2.0.34
2.0.33
2.0.32
2.0.31
2.0.30
2.0.29
2.0.27
2.0.26
2.0.25
2.0.24
2.0.23
2.0.22
2.0.20
2.0.19
2.0.18
2.0.17
2.0.16
2.0.15
2.0.14
2.0.13
2.0.12
2.0.11
2.0.10
2.0.9
2.0.8
2.0.7
2.0.6
2.0.5
2.0.4
2.0.1
2.0.0
1.1.53
1.1.52
1.1.50
1.1.49
1.1.48
1.1.47
1.1.46
1.1.45
1.1.44
1.1.43
1.1.42
1.1.41
1.1.40
1.1.39
1.1.38
1.1.37
1.1.36
1.1.35
1.1.34
1.1.33
1.1.32
1.1.31
1.1.30
1.1.29
1.1.28
1.1.27
1.1.26
1.1.25
1.1.24
1.1.23
1.1.22
1.1.21
1.1.20
1.1.19
1.1.18
1.1.17
1.1.16
1.1.15
1.1.14
1.1.12
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.31
0.4.30
0.4.29
0.4.28
0.4.27
0.4.26
0.4.25
0.4.24
0.4.23
0.4.22
0.4.20
0.4.19
0.4.18
0.4.17
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
A Utilitary package functionalities modules for improve a secure performatic development.
Current section
Files
Jump to
Current section
Files
lib/krug/util/http.util.ex
defmodule Krug.HttpUtil do
@moduledoc """
Utilitary module to handle HTTPoison requests and respectives fail/responses.
Useful to access external services with CORS restrictions in browser,
or services that involves use of credentials that don't should be send/stored
in browser/UI.
"""
@doc """
Makes a ```GET``` request to a url.
In case of success return a response.body.
If fail return nil.
If fail and was received a debug parameter as ```true```, then return the fail reason.
## Examples
```elixir
iex > url = "www.google.com"
iex > Krug.HttpUtil.make_get_request(url)
"<!doctype html><html ..."
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_get_request(fake_url)
nil
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_get_request(fake_url,[],[],true)
%HTTPoison.Error{id: nil, reason: :nxdomain}
```
"""
def make_get_request(url,headers \\ [], options \\ [],debug \\ false) do
HTTPoison.get(url,headers,options) |> handle_response(debug)
end
@doc """
Makes a ```POST``` request to a url.
In case of success return a response.body.
If fail return nil.
If fail and was received a debug parameter as ```true```, then return the fail reason.
## Examples
```elixir
iex > url = "www.google.com"
iex > json_body = "{search: \"ping\"}"
iex > Krug.HttpUtil.make_post_request(url,json_body)
"<!doctype html><html ... Error 405 (Method Not Allowed) ..."
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_post_request(fake_url,json_body)
nil
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_post_request(fake_url,json_body,[],[],true)
%HTTPoison.Error{id: nil, reason: :nxdomain}
```
"""
def make_post_request(url,json_body,headers \\ [], options \\ [],debug \\ false) do
HTTPoison.post(url,json_body,headers,options) |> handle_response(debug)
end
@doc """
Makes a ```PUT``` request to a url.
In case of success return a response.body.
If fail return nil.
If fail and was received a debug parameter as ```true```, then return the fail reason.
## Examples
```elixir
iex > url = "www.google.com"
iex > json_body = "{search: \"ping\"}"
iex > Krug.HttpUtil.make_put_request(url,json_body)
"<!doctype html><html ... Error 405 (Method Not Allowed) ..."
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_put_request(fake_url,json_body)
nil
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_put_request(fake_url,json_body,[],[],true)
%HTTPoison.Error{id: nil, reason: :nxdomain}
```
"""
def make_put_request(url,json_body,headers \\ [], options \\ [],debug \\ false) do
HTTPoison.put(url,json_body,headers,options) |> handle_response(debug)
end
@doc """
Makes a ```PATCH``` request to a url.
In case of success return a response.body.
If fail return nil.
If fail and was received a debug parameter as ```true```, then return the fail reason.
## Examples
```elixir
iex > url = "www.google.com"
iex > json_body = "{search: \"ping\"}"
iex > Krug.HttpUtil.make_patch_request(url,json_body)
"<!doctype html><html ... Error 405 (Method Not Allowed) ..."
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_patch_request(fake_url,json_body)
nil
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_patch_request(fake_url,json_body,[],[],true)
%HTTPoison.Error{id: nil, reason: :nxdomain}
```
"""
def make_patch_request(url,json_body,headers \\ [], options \\ [],debug \\ false) do
HTTPoison.patch(url,json_body,headers,options) |> handle_response(debug)
end
@doc """
Makes a ```DELETE``` request to a url.
In case of success return a response.body.
If fail return nil.
If fail and was received a debug parameter as ```true```, then return the fail reason.
## Examples
```elixir
iex > url = "www.google.com"
iex > Krug.HttpUtil.make_delete_request(url)
"<!doctype html><html ... Error 405 (Method Not Allowed) ..."
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_delete_request(fake_url)
nil
```
```elixir
iex > fake_url = "www.fake_url.xxx"
iex > Krug.HttpUtil.make_delete_request(fake_url,[],[],true)
%HTTPoison.Error{id: nil, reason: :nxdomain}
```
"""
def make_delete_request(url,headers \\ [], options \\ [],debug \\ false) do
HTTPoison.delete(url,headers,options) |> handle_response(debug)
end
defp handle_response({:ok,response},_debug) do
response.body
end
defp handle_response({:error,reason},debug) do
cond do
(debug) -> reason
true -> nil
end
end
end