Last active
December 22, 2016 19:15
-
-
Save darelf/0b2555ff849150803f6a503304be23ee to your computer and use it in GitHub Desktop.
Consume DataCite API data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(consumer). | |
%% API | |
-export([execute_query/0]). | |
parse_item(Data) -> | |
ID = maps:get(<<"id">>, Data), | |
Item = maps:get(<<"attributes">>, Data), | |
DOI = maps:get(<<"doi">>, Item), | |
Title = maps:get(<<"title">>, Item), | |
Published = maps:get(<<"published">>, Item), | |
Updated = maps:get(<<"updated">>, Item), | |
AuthorList = maps:get(<<"author">>, Item), | |
Author = maps:get(<<"literal">>, hd(AuthorList)), | |
#{<<"id">> => ID, <<"DOI">> => DOI, <<"title">> => Title, <<"author">> => Author, | |
<<"published">> => Published, <<"updated">> => Updated}. | |
parse_list(List) -> | |
parse_list(List, []). | |
parse_list([H|T], Acc) -> | |
Item = parse_item(H), | |
parse_list(T, [Item | Acc]); | |
parse_list([], Acc) -> | |
#{<<"listitems">> => Acc}. | |
parse_response({_Status, _Header, Body}) -> | |
R = list_to_binary(Body), | |
Json = jsx:decode(R, [return_maps]), | |
Data = maps:get(<<"data">>, Json), | |
parse_list(Data); | |
parse_response(_) -> | |
nodata. | |
execute_query() -> | |
URL = "https://api.datacite.org/works?query=%22tennessee+valley+authority%22&resource-type-id=dataset&sort=published&rows=2", | |
case httpc:request(URL) of | |
{ok, Result} -> | |
{ok, parse_response(Result)}; | |
Response -> | |
Response | |
end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment