Current section

Files

Jump to
fireball src fireball.erl
Raw

src/fireball.erl

-module(fireball).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([decode_error/1, put_data/4, patch_data/4, get_data/3, get_doc/5, post_doc_from_string/7, post_doc_from_file/7, get_file/5, post_file/5, post_file_erl/2, post_file_external/7, doc_to_string/1, doc_to_list/1, doc_to_tuple/1, doc_to_json/1, post_doc/7, upload_data_to_json/1, json_to_upload_data/1]).
-export_type([collection/0, fireball_error/0, fireball_document/0, fireball_storage/0, upload_data/0, rtdb_error/0]).
-type collection() :: {collection, binary(), binary()}.
-type fireball_error() :: {fireball_error, binary()}.
-type fireball_document() :: {fireball_document, binary()}.
-type fireball_storage() :: {fireball_storage, binary()}.
-type upload_data() :: {upload_data,
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()}.
-type rtdb_error() :: {generic, binary()}.
-file("/export/gleam/fireball/src/fireball.gleam", 70).
-spec decode_error(binary()) -> {ok, rtdb_error()} |
{error, gleam@json:decode_error()}.
decode_error(Input) ->
gleam@json:decode(
Input,
gleam@dynamic:decode1(
fun(Field@0) -> {generic, Field@0} end,
gleam@dynamic:field(<<"error"/utf8>>, fun gleam@dynamic:string/1)
)
).
-file("/export/gleam/fireball/src/fireball.gleam", 78).
-spec put_data(binary(), binary(), binary(), binary()) -> {ok, binary()} |
{error, rtdb_error()}.
put_data(Apikey, Input, Dbpath, Dburl) ->
Url = <<<<<<Dburl/binary, Dbpath/binary>>/binary, ".json?auth="/utf8>>/binary,
Apikey/binary>>,
case gleam@http@request:to(Url) of
{ok, Base_req} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@1 = gleam@http@request:set_method(_pipe, put),
gleam@http@request:set_body(_pipe@1, Input)
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, erlang:element(4, Resp)};
{error, Err} ->
gleam@io:debug(Err),
{error, {generic, <<"error while sending request"/utf8>>}}
end;
{error, Err@1} ->
gleam@io:debug(Err@1),
{error, {generic, <<"error while creating request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 128).
-spec patch_data(binary(), binary(), binary(), binary()) -> {ok, binary()} |
{error, rtdb_error()}.
patch_data(Apikey, Input, Dbpath, Dburl) ->
Url = <<<<<<Dburl/binary, Dbpath/binary>>/binary, ".json?auth="/utf8>>/binary,
Apikey/binary>>,
case gleam@http@request:to(Url) of
{ok, Base_req} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@1 = gleam@http@request:set_method(_pipe, patch),
gleam@http@request:set_body(_pipe@1, Input)
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, erlang:element(4, Resp)};
{error, Err} ->
gleam@io:debug(Err),
{error, {generic, <<"error while sending request"/utf8>>}}
end;
{error, Err@1} ->
gleam@io:debug(Err@1),
{error, {generic, <<"error while creating request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 178).
-spec get_data(binary(), binary(), binary()) -> {ok, binary()} |
{error, rtdb_error()}.
get_data(Apikey, Dbpath, Dburl) ->
Url = <<<<<<Dburl/binary, Dbpath/binary>>/binary, ".json?auth="/utf8>>/binary,
Apikey/binary>>,
case gleam@http@request:to(Url) of
{ok, Base_req} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_method(_pipe, get)
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, erlang:element(4, Resp)};
{error, Err} ->
gleam@io:debug(Err),
{error, {generic, <<"error while sending request"/utf8>>}}
end;
{error, Err@1} ->
gleam@io:debug(Err@1),
{error, {generic, <<"error while creating request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 225).
-spec get_doc(binary(), binary(), binary(), binary(), binary()) -> {ok,
fireball_document()} |
{error, fireball_error()}.
get_doc(Apikey, Apiver, Database, Doc, Proj_id) ->
case gleam@http@request:to(
<<<<<<<<<<<<<<<<<<"https://firestore.googleapis.com/"/utf8,
Apiver/binary>>/binary,
"/projects/"/utf8>>/binary,
Proj_id/binary>>/binary,
"/databases/"/utf8>>/binary,
Database/binary>>/binary,
"/documents/"/utf8>>/binary,
Doc/binary>>/binary,
"?key="/utf8>>/binary,
Apikey/binary>>
) of
{ok, Base_req} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_method(_pipe, get)
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, {fireball_document, erlang:element(4, Resp)}};
{error, _} ->
{error,
{fireball_error, <<"error while sending request"/utf8>>}}
end;
{error, _} ->
{error, {fireball_error, <<"error while forming request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 336).
-spec post_doc_from_string(
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()
) -> {ok, fireball_document()} | {error, fireball_error()}.
post_doc_from_string(
Apikey,
Apiver,
Database,
Collection_path,
Doc_path,
Input_data,
Proj_id
) ->
Url = <<<<<<<<<<<<<<<<<<<<<<"https://firestore.googleapis.com/"/utf8,
Apiver/binary>>/binary,
"/projects/"/utf8>>/binary,
Proj_id/binary>>/binary,
"/databases/"/utf8>>/binary,
Database/binary>>/binary,
"/documents/"/utf8>>/binary,
Collection_path/binary>>/binary,
"?documentId="/utf8>>/binary,
Doc_path/binary>>/binary,
"&key="/utf8>>/binary,
Apikey/binary>>,
case gleam@http@request:to(Url) of
{ok, Base_req} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
gleam@http@request:set_body(_pipe@1, Input_data)
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, {fireball_document, erlang:element(4, Resp)}};
{error, _} ->
{error,
{fireball_error, <<"error while sending request"/utf8>>}}
end;
{error, _} ->
{error, {fireball_error, <<"error while creating request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 395).
-spec post_doc_from_file(
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()
) -> {ok, fireball_document()} | {error, fireball_error()}.
post_doc_from_file(
Apikey,
Apiver,
Database,
Collection_path,
Doc_path,
Input_doc,
Proj_id
) ->
Url = <<<<<<<<<<<<<<<<<<<<<<"https://firestore.googleapis.com/"/utf8,
Apiver/binary>>/binary,
"/projects/"/utf8>>/binary,
Proj_id/binary>>/binary,
"/databases/"/utf8>>/binary,
Database/binary>>/binary,
"/documents/"/utf8>>/binary,
Collection_path/binary>>/binary,
"?documentId="/utf8>>/binary,
Doc_path/binary>>/binary,
"&key="/utf8>>/binary,
Apikey/binary>>,
case gleam@http@request:to(Url) of
{ok, Base_req} ->
case simplifile:read(Input_doc) of
{ok, Json_data} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
gleam@http@request:set_body(_pipe@1, Json_data)
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, {fireball_document, erlang:element(4, Resp)}};
{error, _} ->
{error,
{fireball_error,
<<"error while sending request"/utf8>>}}
end;
{error, _} ->
{error, {fireball_error, <<"failed to read file"/utf8>>}}
end;
{error, _} ->
{error, {fireball_error, <<"error while creating request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 463).
-spec get_file(binary(), binary(), binary(), binary(), binary()) -> {ok,
fireball_storage()} |
{error, fireball_error()}.
get_file(Apiver, Obj_path, Proj_id, Token, Content_type) ->
Url = <<<<<<<<<<<<<<"https://firebasestorage.googleapis.com/"/utf8,
Apiver/binary>>/binary,
"/b/"/utf8>>/binary,
Proj_id/binary>>/binary,
".appspot.com/o/"/utf8>>/binary,
(gleam@string:replace(Obj_path, <<"/"/utf8>>, <<"%2F"/utf8>>))/binary>>/binary,
"?alt=media&token="/utf8>>/binary,
Token/binary>>,
case gleam@http@request:to(Url) of
{ok, Base_req} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
Content_type
),
gleam@http@request:set_method(_pipe, get)
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, {fireball_storage, erlang:element(4, Resp)}};
{error, _} ->
{error,
{fireball_error, <<"error while sending request"/utf8>>}}
end;
{error, _} ->
{error, {fireball_error, <<"failed to create request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 513).
-spec post_file(binary(), binary(), binary(), binary(), binary()) -> {ok,
binary()} |
{error, fireball_error()}.
post_file(Apiver, Apikey, Infile, Outfile, Proj_id) ->
case gleam@http@request:to(
<<<<<<<<<<"https://firestore.googleapis.com/"/utf8, Apiver/binary>>/binary,
"/b/"/utf8>>/binary,
Proj_id/binary>>/binary,
".appspot.com/o/"/utf8>>/binary,
(gleam@string:replace(Outfile, <<"/"/utf8>>, <<"%2F"/utf8>>))/binary>>
) of
{ok, Base_req} ->
case simplifile_erl:read_bits(Infile) of
{ok, Raw_file_data} ->
File_data = gleam_stdlib:bit_array_base64_encode(
Raw_file_data,
true
),
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"text/plain"/utf8>>
),
_pipe@1 = gleam@http@request:set_header(
_pipe,
<<"Authorization"/utf8>>,
Apikey
),
_pipe@2 = gleam@http@request:set_body(
_pipe@1,
File_data
),
gleam@http@request:set_method(_pipe@2, post)
end,
case gleam@httpc:send(Req) of
{ok, _} ->
{ok, <<"OK"/utf8>>};
{error, _} ->
{error,
{fireball_error,
<<"error while sending request"/utf8>>}}
end;
{error, _} ->
{error,
{fireball_error,
<<"Failed to read in file: "/utf8, Infile/binary>>}}
end;
{error, _} ->
{error, {fireball_error, <<"error while forming request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 577).
-spec post_file_erl(binary(), binary()) -> nil.
post_file_erl(Url, Infile) ->
fireball_ffi:post_file_erl(Url, Infile).
-file("/export/gleam/fireball/src/fireball.gleam", 580).
-spec post_file_external(
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()
) -> {ok, binary()} | {error, fireball_error()}.
post_file_external(Apiver, _, Infile, Outfile, Proj_id, External_script, Wd) ->
Url = <<<<<<<<<<"https://firebasestorage.googleapis.com/"/utf8,
Apiver/binary>>/binary,
"/b/"/utf8>>/binary,
Proj_id/binary>>/binary,
".appspot.com/o/"/utf8>>/binary,
(gleam@string:replace(Outfile, <<"/"/utf8>>, <<"%2F"/utf8>>))/binary>>,
case gleamyshell_ffi:execute(External_script, Wd, [Url, Infile]) of
{ok, {command_output, 0, Output}} ->
{ok, gleam@string:trim(Output)};
{ok, {command_output, Exit_code, Output@1}} ->
{error,
{fireball_error,
<<<<<<"ERROR:"/utf8,
(gleam@int:to_string(Exit_code))/binary>>/binary,
";"/utf8>>/binary,
Output@1/binary>>}};
{error, Reason} ->
{error, {fireball_error, <<"FATAL_ERROR:"/utf8, Reason/binary>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 619).
-spec doc_to_string(fireball_document()) -> binary().
doc_to_string(Doc) ->
erlang:element(2, Doc).
-file("/export/gleam/fireball/src/fireball.gleam", 624).
-spec doc_to_list(fireball_document()) -> list(binary()).
doc_to_list(Doc) ->
[erlang:element(2, Doc)].
-file("/export/gleam/fireball/src/fireball.gleam", 629).
-spec doc_to_tuple(fireball_document()) -> {binary()}.
doc_to_tuple(Doc) ->
{erlang:element(2, Doc)}.
-file("/export/gleam/fireball/src/fireball.gleam", 634).
-spec doc_to_json(fireball_document()) -> binary().
doc_to_json(Doc) ->
_pipe = gleam@json:object(
[{<<"name"/utf8>>, gleam@json:string(erlang:element(2, Doc))}]
),
gleam@json:to_string(_pipe).
-file("/export/gleam/fireball/src/fireball.gleam", 278).
-spec post_doc(
binary(),
binary(),
binary(),
binary(),
binary(),
fireball_document(),
binary()
) -> {ok, fireball_document()} | {error, fireball_error()}.
post_doc(
Apikey,
Apiver,
Database,
Collection_path,
Doc_path,
Input_doc,
Proj_id
) ->
Url = <<<<<<<<<<<<<<<<<<<<<<"https://firestore.googleapis.com/"/utf8,
Apiver/binary>>/binary,
"/projects/"/utf8>>/binary,
Proj_id/binary>>/binary,
"/databases/"/utf8>>/binary,
Database/binary>>/binary,
"/documents/"/utf8>>/binary,
Collection_path/binary>>/binary,
"?documentId="/utf8>>/binary,
Doc_path/binary>>/binary,
"&key="/utf8>>/binary,
Apikey/binary>>,
case gleam@http@request:to(Url) of
{ok, Base_req} ->
Req = begin
_pipe = gleam@http@request:prepend_header(
Base_req,
<<"Content-type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
gleam@http@request:set_body(_pipe@1, doc_to_json(Input_doc))
end,
case gleam@httpc:send(Req) of
{ok, Resp} ->
{ok, {fireball_document, erlang:element(4, Resp)}};
{error, _} ->
{error,
{fireball_error, <<"error while sending request"/utf8>>}}
end;
{error, _} ->
{error, {fireball_error, <<"error while creating request"/utf8>>}}
end.
-file("/export/gleam/fireball/src/fireball.gleam", 640).
-spec upload_data_to_json(upload_data()) -> binary().
upload_data_to_json(Upload_data) ->
_pipe = gleam@json:object(
[{<<"name"/utf8>>, gleam@json:string(erlang:element(2, Upload_data))},
{<<"bucket"/utf8>>,
gleam@json:string(erlang:element(3, Upload_data))},
{<<"generation"/utf8>>,
gleam@json:string(erlang:element(4, Upload_data))},
{<<"metageneration"/utf8>>,
gleam@json:string(erlang:element(5, Upload_data))},
{<<"contentType"/utf8>>,
gleam@json:string(erlang:element(6, Upload_data))},
{<<"timeCreated"/utf8>>,
gleam@json:string(erlang:element(7, Upload_data))},
{<<"updated"/utf8>>,
gleam@json:string(erlang:element(8, Upload_data))},
{<<"storageClass"/utf8>>,
gleam@json:string(erlang:element(9, Upload_data))},
{<<"size"/utf8>>,
gleam@json:string(erlang:element(10, Upload_data))},
{<<"md5Hash"/utf8>>,
gleam@json:string(erlang:element(11, Upload_data))},
{<<"contentEncoding"/utf8>>,
gleam@json:string(erlang:element(12, Upload_data))},
{<<"contentDisposition"/utf8>>,
gleam@json:string(erlang:element(13, Upload_data))},
{<<"crc32c"/utf8>>,
gleam@json:string(erlang:element(14, Upload_data))},
{<<"etag"/utf8>>,
gleam@json:string(erlang:element(15, Upload_data))},
{<<"downloadTokens"/utf8>>,
gleam@json:string(erlang:element(16, Upload_data))}]
),
gleam@json:to_string(_pipe).
-file("/export/gleam/fireball/src/fireball.gleam", 662).
-spec json_to_upload_data(gleam@dynamic:dynamic_()) -> {ok, upload_data()} |
{error, list(gleam@dynamic:decode_error())}.
json_to_upload_data(Input) ->
Decoder = (decode@zero:field(
<<"name"/utf8>>,
{decoder, fun decode@zero:decode_string/1},
fun(Name) ->
decode@zero:field(
<<"bucket"/utf8>>,
{decoder, fun decode@zero:decode_string/1},
fun(Bucket) ->
decode@zero:field(
<<"generation"/utf8>>,
{decoder, fun decode@zero:decode_string/1},
fun(Generation) ->
decode@zero:field(
<<"metageneration"/utf8>>,
{decoder, fun decode@zero:decode_string/1},
fun(Metageneration) ->
decode@zero:field(
<<"contentType"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(Content_type) ->
decode@zero:field(
<<"timeCreated"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(Time_created) ->
decode@zero:field(
<<"updated"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(Updated) ->
decode@zero:field(
<<"storageClass"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Storage_class
) ->
decode@zero:field(
<<"size"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Size
) ->
decode@zero:field(
<<"md5Hash"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Md5_hash
) ->
decode@zero:field(
<<"contentEncoding"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Content_encoding
) ->
decode@zero:field(
<<"contentDisposition"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Content_disposition
) ->
decode@zero:field(
<<"crc32c"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Crc32c
) ->
decode@zero:field(
<<"etag"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Etag
) ->
decode@zero:field(
<<"downloadTokens"/utf8>>,
{decoder,
fun decode@zero:decode_string/1},
fun(
Download_tokens
) ->
decode@zero:success(
{upload_data,
Name,
Bucket,
Generation,
Metageneration,
Content_type,
Time_created,
Updated,
Storage_class,
Size,
Md5_hash,
Content_encoding,
Content_disposition,
Crc32c,
Etag,
Download_tokens}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)),
decode@zero:run(Input, Decoder).