2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
30 const std::unordered_set<std::string>& batch,
34 return nlohmann::json::object();
38 ?
"https://www.wikidata.org/w/api.php"
39 :
"https://commons.wikimedia.org/w/api.php");
41 = (kind != corespace::entity_kind::entity_schema ? join_str(opt.props)
42 : join_str(opt.prop));
44 corespace::parameter_list base_params { opt.params };
46 base_params.emplace_back(
"action",
"query");
48 base_params.emplace_back(
"action",
"wbgetentities");
51 std::string prefix {};
53 prefix =
"EntitySchema:";
55 nlohmann::json combined = nlohmann::json::object();
56 for (
auto&& chunk : batch | std::views::chunk(opt.batch_threshold)) {
57 std::vector<std::string> chunk_vec;
58 for (
const auto& id : chunk) {
59 if (arachne::identify(id) != kind) {
62 chunk_vec.emplace_back(prefix + id);
64 corespace::parameter_list params { base_params };
65 auto entities = join_str(chunk_vec);
67 if (kind == corespace::entity_kind::entity_schema) {
68 params.emplace_back(
"titles", entities);
69 params.emplace_back(
"prop", props);
71 params.emplace_back(
"ids", entities);
72 params.emplace_back(
"props", props);
74 auto r = client.get(url, params);
75 auto data = nlohmann::json::parse(r.text,
nullptr,
true);
76 if (!data.is_object()) {
79 combined.merge_patch(data);
86 [method, url, query_params, form_params, body, content_type, accept,
87 timeout_sec, use_form_body]
90 return nlohmann::json::parse(
91 client.get(url, query_params, accept, timeout_sec).text,
nullptr,
96 return nlohmann::json::parse(
98 .post_form(url, form_params, query_params, accept, timeout_sec)
103 return nlohmann::json::parse(
106 url, body, content_type, query_params, accept, timeout_sec
115 request
.query = std::move(query);
116 return sparql(request);
120 return client.metrics_info();
152 const std::size_t threshold
153 = request.length_threshold == sparql_request::service_default
155 : request.length_threshold;
163 preview.accept = resolve_accept(request, profile, wdqs_opt.accept_override);
166 preview.query_params.emplace_back(
"query", request
.query);
169 const auto [content_type, use_form_body]
170 = resolve_body_strategy(request);
175 preview.form_params.emplace_back(
"query", request
.query);
176 sort_parameters(preview.form_params);
Batch courier for Wikidata/Commons: collects IDs, issues HTTP requests, and returns a merged JSON pay...
corespace::call_preview preview(const corespace::sparql_request &request) const
Produce a call preview describing the HTTP request that would be made.
nlohmann::json wdqs(std::string query)
Convenience wrapper to run a raw SPARQL query string.
corespace::call_preview build_call_preview(const corespace::sparql_request &request) const
corespace::wdqs_options wdqs_opt
nlohmann::json sparql(const corespace::sparql_request &request)
Execute a SPARQL query according to the provided request.
const corespace::network_metrics & metrics_info() const
Access aggregated network metrics of the underlying client.
nlohmann::json fetch_json(const std::unordered_set< std::string > &batch, corespace::entity_kind kind=corespace::entity_kind::any)
Fetch metadata for a set of entity IDs and return a merged JSON object.
http_method choose_http_method(const sparql_request &request, const std::size_t threshold)
Chooses the appropriate HTTP method for a SPARQL request.
http_method
HTTP method to use for a request.
entity_kind
Wikidata entity kind.
@ mediainfo
IDs prefixed with 'M'.
@ entity_schema
IDs prefixed with 'E'.
service_kind
Identifies supported SPARQL services.
const service_profile & get_service_profile(const service_kind kind)
Retrieve the service profile for a given service kind.
int timeout_sec
Per-request timeout in seconds (-1 for default).
std::string url
Full request URL (excluding query parameters).
http_method method
HTTP method to use for the request (GET, POST, etc.).
std::string content_type
Content-Type header value for the request body.
std::size_t length_threshold