View Source SurrealEx.Response (SurrealEx v0.2.0)
Link to this section Summary
Functions
all_status_ok?
reads all responses status searching that all will be "OK".
From raw Surreal DB response (that can gives us many responses) we can build the responses list [%SurrealEx.Response{...}] using build_all(raw_body)
.
SurrealEx.Response structure constructor cast the SurrealDB´S JSON responses.
Using to_dot_syntax
we will can work using dot syntax with result field.
Link to this section Functions
all_status_ok?
reads all responses status searching that all will be "OK".
example
Example:
iex> raw_body = "[{\"time\":\"77.182µs\",\"status\":\"OK\",\"detail\":\"\"}, {\"time\":\"77.182µs\",\"status\":\"OK\",\"detail\":\"\"}]"
iex> {:ok, response} = SurrealEx.Response.build_all({:ok,raw_body})
iex> SurrealEx.Response.all_status_ok?(response)
true
iex> raw_body = "[{\"time\":\"77.182µs\",\"status\":\"ERR\",\"detail\":\"Database record `team:valenciacf` already exists\"}, {\"time\":\"77.182µs\",\"status\":\"OK\",\"detail\":\"\"}]"
iex> {:ok, response} = SurrealEx.Response.build_all({:ok,raw_body})
iex> SurrealEx.Response.all_status_ok?(response)
false
From raw Surreal DB response (that can gives us many responses) we can build the responses list [%SurrealEx.Response{...}] using build_all(raw_body)
.
example
Example:
iex> raw_body = "[{\"time\":\"77.182µs\",\"status\":\"ERR\",\"detail\":\"Database record `team:valenciacf` already exists\"}]"
iex> {:ok, response} = SurrealEx.Response.build_all({:ok,raw_body})
iex> response
[
%SurrealEx.Response{
time: "77.182µs",
status: "ERR",
detail: "Database record `team:valenciacf` already exists",
result: []
}
]
SurrealEx.Response structure constructor cast the SurrealDB´S JSON responses.
example
Example:
iex> json_res = %{ "time" => "775.74µs",
...> "status" => "OK",
...> "detail" => "",
...> "result" => [%{
...> "id" => "person:bob",
...> "name" => "bob"
...> }]
...>}
iex> response = SurrealEx.Response.new(json_res)
iex> response.time
"775.74µs"
iex> response.status
"OK"
iex> response.detail
""
iex> response.result
%{"id" => "person:bob","name" => "bob"}
Using to_dot_syntax
we will can work using dot syntax with result field.
IMPORTANT: Remember that if you try access to field with dot and this field isnt exist will throws KeyError (See following example...)
example
Example:
iex> json_res = %{ "time" => "127.74µs",
...> "status" => "OK",
...> "detail" => "",
...> "result" => [
...> %{ "id" => "city:esvalencia", "name" => "Valencia", "population" => 791_413 },
...> %{ "id" => "city:esmadrid", "name" => "Madrid", "area" => 604.45 },
...> %{ "id" => "city:esbarcelona", "name" => "Barcelona" }
...> ]}
iex> response = SurrealEx.Response.new(json_res)
...> |> SurrealEx.Response.to_dot_syntax()
iex> [valencia, madrid, barcelona] = response.result
iex> valencia.population
791_413
iex> madrid.area
604.45
iex> barcelona.area
** (KeyError) key :area not found in: %{id: "city:esbarcelona", name: "Barcelona"}