1 |
|
%% -*- erlang -*- |
2 |
|
|
3 |
|
-module(chrme_http_apic). |
4 |
|
-export([list_targets/2, version/2, new_target/3, close_target/3]). |
5 |
|
|
6 |
|
%% List available debugging targets |
7 |
|
-spec list_targets(Host :: klsn:binstr(), Port :: integer()) -> {ok, [map()]} | {error, term()}. |
8 |
|
list_targets(Host, Port) -> |
9 |
|
%% Host must be binary; Path is binary |
10 |
:-( |
Url = build_url(Host, Port, <<"/json">>), |
11 |
:-( |
case httpc:request(get, {Url, []}, [], [{body_format, binary}]) of |
12 |
|
{ok, {{_, 200, _}, _, Body}} -> |
13 |
:-( |
{ok, jsone:decode(Body)}; |
14 |
|
{ok, {{_, Code, _}, _, Body}} -> |
15 |
:-( |
{error, {http_error, Code, Body}}; |
16 |
|
{error, Reason} -> |
17 |
:-( |
{error, Reason} |
18 |
|
end. |
19 |
|
|
20 |
|
%% Query Chrome version info |
21 |
|
-spec version(Host :: klsn:binstr(), Port :: integer()) -> {ok, map()} | {error, term()}. |
22 |
|
version(Host, Port) -> |
23 |
:-( |
Url = build_url(Host, Port, <<"/json/version">>), |
24 |
:-( |
case httpc:request(get, {Url, []}, [], [{body_format, binary}]) of |
25 |
|
{ok, {{_, 200, _}, _, Body}} -> |
26 |
:-( |
{ok, jsone:decode(Body)}; |
27 |
|
{ok, {{_, Code, _}, _, Body}} -> |
28 |
:-( |
{error, {http_error, Code, Body}}; |
29 |
|
{error, Reason} -> |
30 |
:-( |
{error, Reason} |
31 |
|
end. |
32 |
|
|
33 |
|
%% Create a new debugging target |
34 |
|
-spec new_target(Host :: klsn:binstr(), Port :: integer(), Url0 :: klsn:binstr()) -> {ok, map()} | {error, term()}. |
35 |
|
new_target(Host, Port, Url0) -> |
36 |
|
%% Host and Url0 must be binaries |
37 |
:-( |
Url = cow_qs:urlencode(Url0), |
38 |
:-( |
PathBin = <<"/json/new?", Url/binary>>, %% build path as binary |
39 |
:-( |
FullUrl = build_url(Host, Port, PathBin), |
40 |
|
%% new_target requires HTTP PUT, not GET |
41 |
:-( |
case httpc:request(put, {FullUrl, [], "application/json", <<>>}, [], [{body_format, binary}]) of |
42 |
|
{ok, {{_, 200, _}, _, Body}} -> |
43 |
:-( |
{ok, jsone:decode(Body)}; |
44 |
|
{ok, {{_, Code, _}, _, Body}} -> |
45 |
:-( |
{error, {http_error, Code, Body}}; |
46 |
|
{error, Reason} -> |
47 |
:-( |
{error, Reason} |
48 |
|
end. |
49 |
|
|
50 |
|
%% Close an existing debugging target |
51 |
|
-spec close_target(Host :: klsn:binstr(), Port :: integer(), TargetId0 :: klsn:binstr()) -> {ok, true} | {error, term()}. |
52 |
|
close_target(Host, Port, TargetId0) -> |
53 |
|
%% Host and TargetId0 must be binaries |
54 |
:-( |
PathBin = <<"/json/close/", TargetId0/binary>>, |
55 |
:-( |
Url = build_url(Host, Port, PathBin), |
56 |
|
%% close_target requires HTTP PUT |
57 |
:-( |
case httpc:request(put, {Url, [], "application/json", <<>>}, [], [{body_format, binary}]) of |
58 |
|
{ok, {{_, 200, _}, _, _Body}} -> |
59 |
:-( |
{ok, true}; |
60 |
|
{ok, {{_, Code, _}, _, Body}} -> |
61 |
:-( |
{error, {http_error, Code, Body}}; |
62 |
|
{error, Reason} -> |
63 |
:-( |
{error, Reason} |
64 |
|
end. |
65 |
|
|
66 |
|
%% Build an HTTP URL; binary inputs |
67 |
|
-spec build_url(HostBin :: klsn:binstr(), Port :: integer(), PathBin :: klsn:binstr()) -> string(); |
68 |
|
(HostList :: string(), Port :: integer(), PathList :: string()) -> string(). |
69 |
|
build_url(HostBin, Port, PathBin) when is_binary(HostBin), is_integer(Port), is_binary(PathBin) -> |
70 |
:-( |
HostList = binary_to_list(HostBin), |
71 |
:-( |
PathList = binary_to_list(PathBin), |
72 |
:-( |
"http://" ++ HostList ++ ":" ++ integer_to_list(Port) ++ PathList. |