Skip to content

Commit

Permalink
Merge pull request #34 from smartrent/CMWAPIS-174-202_json_response
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-kerle authored Jun 11, 2021
2 parents 33c7c1d + 4d7cc3d commit f0df1cc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ defmodule Solicit.Response do
|> json(%{details: details})
end

@spec accepted(Plug.Conn.t(), term(), term()) :: Plug.Conn.t()
def accepted(conn, details, fields \\ nil) do
conn
|> put_status(:accepted)
|> json(as_json(details, fields))
end

# 204
@doc """
Returns a successful no_content response. Normally used when deleting.
Expand Down
19 changes: 19 additions & 0 deletions md/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ This is used for an accepted response. Normally used to signify that the request

Returns `Plug.Conn.t()` with a status of `202` and the details passed in as the payload.

### Response.accepted/3

Usage:

This is used for an accepted response. Normally used to signify that the request was accepted, but might not have finished processing.

```elixir
Response.accepted(conn, %{license_plate: _, state: _}, fields)
```

Returns `Plug.Conn.t()` with a status of `202` and a payload of

```elixir
%{
"license_plate" => _,
"state" => _
}
```

### Response.no_content/1

Usage:
Expand Down
22 changes: 22 additions & 0 deletions test/response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ defmodule Solicit.ResponseTest do
end
end

describe "accepted/3" do
test "Should return 202 with all fields by default" do
response =
build_conn()
|> Response.accepted(%{license_plate: "Test", state: "VA"})
|> json_response(:accepted)

assert response == %{"license_plate" => "Test", "state" => "VA"}
end

test "Should return 202 with defined fields" do
response =
build_conn()
|> Response.accepted(%{license_plate: "Test", state: "VA"}, [
:license_plate
])
|> json_response(:accepted)

assert response == %{"license_plate" => "Test"}
end
end

describe "no_content" do
test "Should return 204" do
assert Response.no_content(build_conn()).status == 204
Expand Down

0 comments on commit f0df1cc

Please sign in to comment.