-
Notifications
You must be signed in to change notification settings - Fork 64
/
responses.ex
115 lines (97 loc) · 3.8 KB
/
responses.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
defmodule RigApi.V3.Responses do
@moduledoc "Controller for submitting (backend) responses to asynchronous (frontend) requests."
require Logger
use RigApi, :controller
use PhoenixSwagger
alias RigInboundGateway.ApiProxy.ResponseFromParser
@prefix "/v3"
action_fallback(RigApi.Fallback)
swagger_path :create do
post(@prefix <> "/responses")
summary("Submit a message, to be sent to correlated reverse proxy request.")
description("Allows you to submit a message to RIG using a simple, \
synchronous call. Message will be sent to correlated reverse proxy request.")
parameters do
messageBody(
:body,
Schema.ref(:CloudEvent),
"CloudEvent",
required: true
)
end
response(202, "Accepted - message sent to correlated reverse proxy request")
response(400, "Bad Request: Failed to parse request body :parse-error")
end
@doc """
Accepts message to be sent to correlated HTTP process.
Note that body has to contain following field `"rig": { "correlation": "_id_" }`.
"""
def create(%{req_headers: req_headers} = conn, message) do
case ResponseFromParser.parse(req_headers, message) do
{deserialized_pid, response_code, response, extra_headers} ->
Logger.debug(fn ->
"HTTP response via internal HTTP to #{inspect(deserialized_pid)}: #{inspect(message)}"
end)
send(deserialized_pid, {:response_received, response, response_code, extra_headers})
send_resp(conn, :accepted, "message sent to correlated reverse proxy request")
err ->
Logger.warn(fn -> "Parse error #{inspect(err)} for #{inspect(message)}" end)
conn
|> put_status(:bad_request)
|> text("Failed to parse request body: #{inspect(err)}")
end
end
def swagger_definitions do
%{
Response:
swagger_schema do
title("CloudEvent")
description("The CloudEvent that will be sent to correlated reverse proxy request.")
properties do
id(
:string,
"ID of the event. The semantics of this string are explicitly undefined to ease \
the implementation of producers. Enables deduplication.",
required: true,
example: "A database commit ID"
)
specversion(
:string,
"The version of the CloudEvents specification which the event uses. This \
enables the interpretation of the context. Compliant event producers \
MUST use a value of 0.2 when referring to this version of the \
specification.",
required: true,
example: "0.2"
)
source(
:string,
"This describes the event producer. Often this will include information such \
as the type of the event source, the organization publishing the event, the \
process that produced the event, and some unique identifiers. The exact syntax \
and semantics behind the data encoded in the URI is event producer defined.",
required: true,
example: "/cloudevents/spec/pull/123"
)
type(
:string,
"Type of occurrence which has happened. Often this attribute is used for \
routing, observability, policy enforcement, etc.",
required: true,
example: "com.example.object.delete.v3"
)
rig(
Schema.new do
properties do
correlation(:string, "Correlation ID",
required: true,
example: "g2dkAA1ub25vZGVAbm9ob3N0AAADxwAAAAAA"
)
end
end
)
end
end
}
end
end