30
31:- module(api_json,
32 [
33 ]). 34:- use_module(library(semweb/rdf_db)). 35:- use_module(library(semweb/rdf_json)). 36:- use_module(library(semweb/rdf_describe)). 37:- use_module(library(http/http_dispatch)). 38:- use_module(library(http/http_parameters)). 39:- use_module(library(http/http_json)). 40
41:- http_handler(json(describe), json_describe, []). 42:- http_handler(json(prefixes), json_prefixes, []). 43:- http_handler(json(resource_representation), json_resource_representation, []). 44
66json_describe(Request) :-
67 http_parameters(Request,
68 [ r(URI,
69 [ description('The resource to describe')
70 ]),
71 how(_How,
72 [ 73 oneof([cbd]),
74 default(cbd),
75 description('Algorithm that determines \c
76 the description')
77 ])
78 ]),
79 resource_CBD(rdf, URI, Graph),
80 graph_json(Graph, JSON),
81 reply_json(JSON).
87json_prefixes(_Request) :-
88 findall(Prefix-URI,
89 rdf_current_ns(Prefix, URI),
90 Pairs),
91 dict_pairs(Dict, prefixes, Pairs),
92 reply_json(Dict).
98json_resource_representation(Request) :-
99 http_parameters(Request,
100 [ r(URI,
101 [ description('The resource to format')
102 ]),
103 language(Lang,
104 [ oneof([sparql,turtle,prolog,xml]),
105 default(turtle),
106 description('Target language')
107 ])
108 ]),
109 format_resource(Lang, URI, String),
110 reply_json_dict(String).
111
112format_resource(sparql, URI, String) :- !,
113 format_resource(turtle, URI, String).
114format_resource(turtle, URI, String) :-
115 ( rdf_global_id(Prefix:Local, URI)
116 -> format(string(String), '~w:~w', [Prefix, Local])
117 ; format(string(String), '<~w>', [URI])
118 ).
119format_resource(xml, URI, String) :-
120 ( rdf_global_id(Prefix:Local, URI),
121 xml_name(URI, utf8)
122 -> format(string(String), '~w:~w', [Prefix, Local])
123 ; format(string(String), '"~w"', [URI])
124 ).
125format_resource(prolog, URI, String) :-
126 ( rdf_global_id(Prefix:Local, URI)
127 -> format(string(String), '~q', [Prefix:Local])
128 ; format(string(String), '~q', [URI])
129 )