:- module(about_prolog_reader,
    [ get_json/1
    , get_json_atoms/1
    , describe/0
    ]
).

:- use_module(library(http/http_open),
    [ http_open/3
    ]
).

:- use_module(library(http/json),
    [ json_read_dict/2
    , json_read_dict/3
    ]
).

location_url('https://jsonplaceholder.typicode.com/users/').


get_json_atoms(Dict) :-
    location_url(URL),
    setup_call_cleanup(
        http_open(URL, StreamIn, [request_header('Accept'='application/json')]),
        json_read_dict(StreamIn, Dict, [tag(json), value_string_as(atom)]),
        close(StreamIn)
    ).

get_json(Dict) :-
    location_url(URL),
    setup_call_cleanup(
        http_open(URL, StreamIn, [request_header('Accept'='application/json')]),
        json_read_dict(StreamIn, Dict),
        close(StreamIn)
    ).

describe :-
    format("Users:~n"),
    get_json(D),
    describe(D).
describe([]).
describe([H|T]) :-
    format("    ~w.    ~w~n", [H.get(name), H.get(email)]),
    describe(T).
