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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE> [99s-extend] REST responses
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20REST%20responses&In-Reply-To=%3CCA%2BdV7cTrbQd-widnjH5CjsYum%3DASwax7VxtAUn_9BVONp2MMuA%40mail.gmail.com%3E">
<META NAME="robots" CONTENT="index,nofollow">
<style type="text/css">
pre {
white-space: pre-wrap; /* css-2.1, curent FF, Opera, Safari */
}
</style>
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<LINK REL="Previous" HREF="000386.html">
<LINK REL="Next" HREF="000389.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[99s-extend] REST responses</H1>
<B>Paulo F. Oliveira</B>
<A HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20REST%20responses&In-Reply-To=%3CCA%2BdV7cTrbQd-widnjH5CjsYum%3DASwax7VxtAUn_9BVONp2MMuA%40mail.gmail.com%3E"
TITLE="[99s-extend] REST responses">paulo.ferraz.oliveira at gmail.com
</A><BR>
<I>Tue May 20 23:41:15 CEST 2014</I>
<P><UL>
<LI>Previous message: <A HREF="000386.html">[99s-extend] REST responses
</A></li>
<LI>Next message: <A HREF="000389.html">[99s-extend] REST responses
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#388">[ date ]</a>
<a href="thread.html#388">[ thread ]</a>
<a href="subject.html#388">[ subject ]</a>
<a href="author.html#388">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>Hi, Loïc.
Thanks for having taken the time to reply. In some of my questions I think
I didn't explain myself correctly so I'll give it another go.
On 20 May 2014 19:46, Loïc Hoguin <<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A>> wrote:
><i> Hi,
</I>><i>
</I>><i>
</I>><i> On 05/20/2014 06:27 PM, Paulo F. Oliveira wrote:
</I>><i>
</I>>><i> Hello.
</I>>><i>
</I>>><i> First of all, thanks for the great work you've done with cowboy. I've
</I>>><i> been using it with a fait amount of success and I'm a fairly new Erlang
</I>>><i> developer. I'm mainly interested in the REST "interface" of the
</I>>><i> application and its way of doing RESTful things, and I like the way you
</I>>><i> did it (what with all the content_types_provided, service_available,
</I>>><i> etc. functions). I've tested the way the system reacted to the different
</I>>><i> Accept, Content-Type, etc. headers and always got very well-opinionated
</I>>><i> responses (406, 415, ...).
</I>>><i>
</I>>><i> A couple of questions remain though (I'm sorry if they've been asked
</I>>><i> already but I've searched the web for answers and read the available
</I>>><i> docs and couldn't find them):
</I>>><i>
</I>>><i> 1. is it expected that, if I use cowboy_req:reply/2 in a GET handler
</I>>><i> (coming from content_types_provided), the onresponse/4 hook be called
</I>>><i> twice? I guess one is due to the reply and the other one due to the
</I>>><i> workflow of the request, but is there a way to prevent the second
</I>>><i> execution?
</I>>><i>
</I>><i>
</I>><i> If you reply from a callback you must call {halt, Req, State} to stop
</I>><i> processing.
</I>
Got it!
><i> 2. if I want to JSON-parse ALL my requests should I a) use the
</I>>><i> onrequest/1 hook or b) do this on a per-request basis? Because I'd like
</I>>><i> to reply with a 400 ASAP but keep going if the JSON validates (I'm going
</I>>><i> to use JSON-schema for validating input); and, if possible, have the
</I>>><i> JSON-parsed body stored somewhere for future manipulation.
</I>>><i>
</I>><i>
</I>><i> It makes little sense to do it before the accept callback you define. Not
</I>><i> only because you will duplicate content-type checks and whatnot, but also
</I>><i> because you don't actually win anything from doing this. If you are using
</I>><i> JSON, then JSON processing will take infinitely more resources than the
</I>><i> REST code.
</I>
OK, I'll probably stick with a "helper" function that'll do this for me and
reply in case there are validation errors.
I only found the flow diagrams for the requests today after I had sent this
message, and they helped a lot.
><i> 3. I haven't seen examples that made use of the State (from the function
</I>>><i> returns). When should I use this instead of the Request metadata? I'd
</I>>><i> like to be able to set a generic error state for a request (either in
</I>>><i> meta ou State) and that have a "standard" error response be created at a
</I>>><i> later time (in a unique function, for example - e.g. onresponse/4).
</I>>><i>
</I>><i>
</I>><i> State is for the functions within the current module. Look at
</I>><i> cowboy_static for an example.
</I>
State allows me to, well, keep state, for a request "travelling" through
functions, right, and I can change it whenever I want just before returning
from a function that is executed prior to another one (the only function
for which this doesn't seem to make since is the last one cowboy calls
before actually replying to the client)? At the same time, so does the
request meta, from what I understood from the manual. So what is the
difference between one and the other and when would you recommend one or
the other.
4. is there anything like a catch-all exception handler? I'd like to
>><i> catch exceptions that occur anywhere so I could log them and analyze
</I>>><i> them at a later moment.
</I>>><i>
</I>><i>
</I>><i> You can add your own error_logger handler, or use something like lager.
</I>><i> All errors end up sending a message to error_logger.
</I>
I'll do this, thanks.
><i> I'm probably abusing the onresponse/onrequest hooks already, so your
</I>>><i> answers should help me clarify this.
</I>>><i>
</I>><i>
</I>><i> Sounds like it!
</I>><i>
</I>><i> Thanks.
</I>>><i>
</I>>><i> - Paulo
</I>>><i>
</I>>><i>
</I>>><i> _______________________________________________
</I>>><i> Extend mailing list
</I>>><i> <A HREF="https://lists.ninenines.eu/listinfo/extend">Extend at lists.ninenines.eu</A>
</I>>><i> <A HREF="https://lists.ninenines.eu/listinfo/extend">https://lists.ninenines.eu/listinfo/extend</A>
</I>>><i>
</I>>><i>
</I>><i> --
</I>><i> Loïc Hoguin
</I>><i> <A HREF="http://ninenines.eu">http://ninenines.eu</A>
</I>><i>
</I>
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <<A HREF="http://lists.ninenines.eu/archives/extend/attachments/20140520/32454f85/attachment.html">http://lists.ninenines.eu/archives/extend/attachments/20140520/32454f85/attachment.html</A>>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="000386.html">[99s-extend] REST responses
</A></li>
<LI>Next message: <A HREF="000389.html">[99s-extend] REST responses
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#388">[ date ]</a>
<a href="thread.html#388">[ thread ]</a>
<a href="subject.html#388">[ subject ]</a>
<a href="author.html#388">[ author ]</a>
</LI>
</UL>
<hr>
<a href="https://lists.ninenines.eu/listinfo/extend">More information about the Extend
mailing list</a><br>
</body></html>
|