Skip to content

Commit

Permalink
feat(bang-methods): add bang methods to resource base module
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiercy827 committed Oct 8, 2018
1 parent 36c172a commit c4412e0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/lob/resource_base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,59 @@ defmodule Lob.ResourceBase do
def list(params \\ %{}, headers \\ %{}) do
Client.get_request("#{base_url()}?#{Util.build_query_string(params)}" , Util.build_headers(headers))
end

@spec list!(map, map) :: Client.response
def list!(params \\ %{}, headers \\ %{}) do
case list(params, headers) do
{:ok, body, headers} -> {body, headers}
error -> raise to_string(error)
end
end
end

if :retrieve in unquote(methods) do
@spec retrieve(String.t, map) :: Client.response
def retrieve(id, headers \\ %{}) do
Client.get_request(resource_url(id), Util.build_headers(headers))
end

@spec retrieve!(map, map) :: Client.response
def retrieve!(id \\ %{}, headers \\ %{}) do
case retrieve(id, headers) do
{:ok, body, headers} -> {body, headers}
error -> raise to_string(error)
end
end
end

if :create in unquote(methods) do
@spec create(map, map) :: Client.response
def create(data, headers \\ %{}) do
Client.post_request(base_url(), Util.build_body(data), Util.build_headers(headers))
end

@spec create!(map, map) :: Client.response
def create!(data \\ %{}, headers \\ %{}) do
case create(data, headers) do
{:ok, body, headers} -> {body, headers}
error -> raise to_string(error)
end
end
end

if :delete in unquote(methods) do
@spec delete(String.t, map) :: Client.response
def delete(id, headers \\ %{}) do
Client.delete_request(resource_url(id), Util.build_headers(headers))
end

@spec delete!(map, map) :: Client.response
def delete!(id \\ %{}, headers \\ %{}) do
case delete(id, headers) do
{:ok, body, headers} -> {body, headers}
error -> raise to_string(error)
end
end
end

@spec base_url :: String.t
Expand Down

0 comments on commit c4412e0

Please sign in to comment.