Skip to content

Commit

Permalink
feat(Rest): add 'transform' option to obtain untransformed data from …
Browse files Browse the repository at this point in the history
…Discord (#5)

* feat(Rest): add 'raw' option to obtain untransformed data from Discord

* docs(Rest): add note about default value for raw option

* refactor(Rest): rename 'raw' to 'transform'
  • Loading branch information
SpaceEEC committed Sep 13, 2019
1 parent 921d6a7 commit b644f9f
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions lib/rest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@ defmodule Crux.Rest do
@moduledoc """
Main entry point for `Crux.Rest`.
For a more convenient way to consume this module you can `use` it in your own.
Possible `use` options are:
* `transform` - whether to transform the received JSON further into the documented structs.
Defaults to `true`.
### Example
```elixir
defmodule MyBot.Rest do
use Crux.Rest
# Define helper functions as needed
def gateway_bot_additional_info() do
with {:ok, data} <- gateway_bot() do
Map.put(data, "additional_info", MyBot.Additional.info())
end
end
end
```
This module fits under a supervision tree, see `start_link/1`'s' arguments for configuration.
The same applies to modules `use`-ing this module.
"""

alias Crux.Rest.{ApiError, Handler, Request, Util, Version}
Expand Down Expand Up @@ -1697,8 +1720,11 @@ defmodule Crux.Rest do
}
end

defmacro __using__(_ \\ []) do
@spec __using__() :: term()
defmacro __using__(opts \\ []) do
quote location: :keep do
transform = !!unquote(opts)[:transform]

@behaviour Crux.Rest

@name __MODULE__
Expand All @@ -1713,12 +1739,22 @@ defmodule Crux.Rest do
Crux.Rest.child_spec({@name, arg})
end

def request(request) do
Crux.Rest.request(@name, request)
end

def request!(request) do
Crux.Rest.request!(@name, request)
if transform do
def request(request) do
Crux.Rest.request(@name, request)
end

def request!(request) do
Crux.Rest.request!(@name, request)
end
else
def request(request) do
Crux.Rest.request(@name, %{request | transform: nil})
end

def request!(request) do
Crux.Rest.request!(@name, %{request | transform: nil})
end
end

@deprecated "Use request/1 instead"
Expand Down

0 comments on commit b644f9f

Please sign in to comment.