forked from for-GET/katt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
katt_callbacks_json.erl
114 lines (100 loc) · 3.85 KB
/
katt_callbacks_json.erl
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Copyright 2013 Klarna AB
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%% @copyright 2013 Klarna AB
%%%
%%% @doc Built-in JSON callback functions.
%%% @end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%_* Module declaration ===============================================
-module(katt_callbacks_json).
%%%_* Exports ==========================================================
%% API
-export([ recall_body/4
, parse/5
, validate_body/3
]).
%%%_* Includes =========================================================
-include("katt.hrl").
%%%_* API ==============================================================
%% @doc Recall all params inside json content.
%% @end
-spec recall_body( boolean()
, any()
, params()
, callbacks()
) -> any().
recall_body(true = _JustCheck, [Hdrs, _Bin], _Params, _Callbacks) ->
ContentType = proplists:get_value("Content-Type", Hdrs, ""),
is_json_content_type(ContentType);
recall_body(false = _JustCheck, [_Hdrs, Bin], [], _Callbacks) ->
Bin;
recall_body(false = _JustCheck, [Hdrs, Bin0], [{K0, V0} | Next], Callbacks) ->
K = ?RECALL_BEGIN_TAG ++ katt_util:to_list(K0) ++ ?RECALL_END_TAG,
REK = "\"" ++ katt_util:escape_regex(K) ++ "\"",
V = katt_util:maybe_json_string(V0),
REV = katt_util:escape_regex(V),
Bin1 = re:replace( Bin0
, REK
, REV
, [{return, binary}, global]),
Bin = katt_callbacks:recall(text, Bin1, [{K0, V0}], Callbacks),
recall_body(false, [Hdrs, Bin], Next, Callbacks).
%% @doc Parse the body of e.g. an HTTP response.
%% @end
-spec parse( boolean()
, headers()
, body()
, params()
, callbacks()
) -> any().
parse(true = _JustCheck, Hdrs, _Body, _Params, _Callbacks) ->
ContentType = proplists:get_value("Content-Type", Hdrs, ""),
is_json_content_type(ContentType);
parse(false = _JustCheck, _Hdrs, null, _Params, _Callbacks) ->
[];
parse(false = _JustCheck, Hdrs, Body, _Params, _Callbacks) ->
ContentType = proplists:get_value("Content-Type", Hdrs, ""),
case is_json_content_type(ContentType) of
true -> parse_json(Body);
false -> katt_util:from_utf8(Body)
end.
validate_body( true = _Justcheck
, #katt_response{headers=EHdrs}
, #katt_response{headers=AHdrs}
) ->
EContentType = proplists:get_value("Content-Type", EHdrs, ""),
AContentType = proplists:get_value("Content-Type", AHdrs, ""),
is_json_content_type(EContentType) andalso is_json_content_type(AContentType);
validate_body( false = _Justcheck
, #katt_response{parsed_body=E}
, #katt_response{parsed_body=A}
) ->
katt_util:compare_struct("/body", E, A, ?MATCH_ANY).
%%%_* Internal =========================================================
is_json_content_type(ContentType) ->
case string:str(ContentType, "json") of
0 -> false;
_ -> true
end.
parse_json(Bin) when is_binary(Bin), size(Bin) =:= 0 ->
[];
parse_json(Bin) ->
katt_util:to_proplist(mochijson3:decode(Bin)).
%%%_* Emacs ============================================================
%%% Local Variables:
%%% allout-layout: t
%%% erlang-indent-level: 2
%%% End: