Packages
phoenix_gen_api
2.11.0
2.22.0
2.21.1
2.21.0
2.20.1
2.20.0
2.19.0
2.18.1
2.18.0
2.17.1
2.17.0
2.16.0
2.15.0
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.9.1
2.9.0
2.8.0
2.7.0
2.6.1
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.1
2.0.0
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.2.1
0.2.0
0.1.1
0.1.0
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
A library for fast develop APIs in Elixir cluster, using Phoenix Channels for transport data, auto pull api configs from service nodes.
Current section
Files
Jump to
Current section
Files
lib/phoenix_gen_api/structs/response.ex
defmodule PhoenixGenApi.Structs.Response do
@moduledoc """
Defines the structure of a response sent back to the client.
This module provides helper functions to create different types of responses,
such as synchronous, asynchronous, and error responses.
"""
@type t :: %__MODULE__{
request_id: String.t(),
result: any(),
success: boolean(),
error: String.t() | nil,
async: boolean(),
has_more: boolean(),
can_retry: boolean()
}
@derive Nestru.Encoder
defstruct [
:request_id,
:result,
success: false,
error: nil,
async: false,
has_more: false,
can_retry: false
]
@doc """
Creates a response for a successful synchronous request.
"""
def sync_response(request_id, result) do
%__MODULE__{request_id: request_id, result: result, success: true}
end
@doc """
Creates a response for an asynchronous request, indicating that the request
has been received and is being processed.
"""
def async_response(request_id) do
%__MODULE__{request_id: request_id, async: true, success: true}
end
@doc """
Creates a response for a streaming request.
"""
def stream_response(request_id, result, has_more \\ true) do
%__MODULE__{
request_id: request_id,
result: result,
async: true,
has_more: has_more,
success: true
}
end
@doc """
Creates a response to indicate the end of a stream.
"""
def stream_end_response(request_id) do
%__MODULE__{request_id: request_id, async: true, has_more: false, success: true}
end
@doc """
Creates a response for a failed request.
"""
def error_response(request_id, error, can_retry \\ false) do
%__MODULE__{request_id: request_id, error: error, success: false, can_retry: can_retry}
end
@doc """
Checks if the response represents an error.
"""
def is_error?(%__MODULE__{success: false}), do: true
def is_error?(%__MODULE__{}), do: false
@doc """
Create Request from params for convert data map from websocket api.
"""
def encode!(res = %__MODULE__{}) do
res
|> Nestru.encode!()
end
@doc """
Create Request from params for convert data map from websocket api.
Opts is not supported yet.
"""
def encode!(res = %__MODULE__{}, _opts) do
encode!(res)
end
def encode(res = %__MODULE__{}, _opts) do
try do
encode!(res)
rescue
_ -> {:error, "cannot convert to map type"}
end
end
# special impl for JSON library
defimpl JSON.Encoder, for: PhoenixGenApi.Structs.Response do
def encode(data, opts) do
data
|> PhoenixGenApi.Structs.Response.encode!(opts)
|> JSON.encode!()
end
end
end