Skip to content

Add simple http client #1123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/wasm-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ jobs:
mkdir build
cd build
cmake .. -G Ninja -DAVM_WARNINGS_ARE_ERRORS=ON
ninja AtomVM atomvmlib test_eavmlib test_alisp hello_world run_script call_cast html5_events wasm_webserver
# test_eavmlib does not work with wasm due to http + ssl test
ninja AtomVM atomvmlib test_alisp hello_world run_script call_cast html5_events wasm_webserver

- name: Upload AtomVM and test modules
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -122,7 +123,8 @@ jobs:
node src/AtomVM.js ../../../../build/examples/erlang/hello_world.beam ../../../../build/libs/eavmlib/src/eavmlib.avm
# Run tests that pass
node src/AtomVM.js ../../../../build/tests/libs/alisp/test_alisp.avm
node src/AtomVM.js ../../../../build/tests/libs/eavmlib/test_eavmlib.avm
# test_eavmlib does not work with wasm due to http + ssl test
# node src/AtomVM.js ../../../../build/tests/libs/eavmlib/test_eavmlib.avm

- name: "Rename and write sha256sum (node)"
if: startsWith(github.ref, 'refs/tags/')
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.6.3] - Unreleased

### Added

- Simple http client, that can be used for different use case such as downloading OTA updates

### Fixed

- Fix bug (with code compiled with OTP-21) with binary pattern matching: the fix introduced with
Expand Down
1 change: 1 addition & 0 deletions examples/erlang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ pack_runnable(code_lock code_lock estdlib eavmlib)
pack_runnable(mqtt_client mqtt_client estdlib eavmlib)
pack_runnable(network_console network_console estdlib eavmlib alisp)
pack_runnable(logging_example logging_example estdlib eavmlib)
pack_runnable(http_client http_client estdlib eavmlib)
82 changes: 82 additions & 0 deletions examples/erlang/http_client.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
%
% This file is part of AtomVM.
%
% Copyright 2024 Davide Bettio <[email protected]>
%
% 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.
%
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(http_client).
-export([start/0]).

-define(ACTIVE, false).

start() ->
ssl:start(),
ConnectResult = ahttp_client:connect(https, "atomvm.net", 443, [
{active, ?ACTIVE}, {verify, verify_none}, {parse_headers, [<<"Location">>]}
]),
case ConnectResult of
{ok, Conn} ->
case ahttp_client:request(Conn, <<"GET">>, <<"/">>, [], undefined) of
{ok, Conn2, _Ref} ->
loop(Conn2);
{error, _} = RequestError ->
io:format("Request failed: ~p~n", [RequestError]),
RequestError
end;
{error, _} = ConnectError ->
io:format("Request failed: ~p~n", [ConnectError]),
ConnectError
end.

-if(?ACTIVE).
loop(Conn) ->
receive
Message ->
case ahttp_client:stream(Conn, Message) of
{ok, _Conn, closed} ->
io:format("Connection closed.~n"),
ok;
{ok, UpdatedConn, Responses} ->
io:format("Got: ~p~n", [Responses]),
loop(UpdatedConn);
unknown ->
io:format("Unexpected message: ~p~n", [Message]),
error
end
end.
-else.
loop(Conn) ->
case ahttp_client:recv(Conn, 0) of
{ok, UpdatedConn, Responses} ->
io:format("Got: ~p~n", [Responses]),
case maybe_terminate(Responses, UpdatedConn) of
ok -> loop(UpdatedConn);
closed -> ok
end;
Other ->
io:format("Unexpected reply: ~p~n", [Other]),
error
end.
-endif.

maybe_terminate([], _Conn) ->
ok;
maybe_terminate([{done, _Ref}], Conn) ->
ahttp_client:close(Conn),
closed;
maybe_terminate([_H | T], Conn) ->
maybe_terminate(T, Conn).
1 change: 1 addition & 0 deletions libs/eavmlib/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ project(eavmlib)
include(BuildErlang)

set(ERLANG_MODULES
ahttp_client
atomvm
avm_pubsub
console
Expand Down
Loading
Loading