问题
I need to have list of tags with 3 fields:
tag_name
tag_description
counter_of_posts.
Since counter_of_posts
is a field of tag-object and tag_description (excerpt) is a field of tag_wiki
, how can I get needed information with one call?
回答1:
This is impossible - you need at least 2 calls (more if you have more than 20 tags). One call to /api.stackexchange.com/2.2/tags/tag_names/info?order=desc&sort=popular&site=sitename
to get the tag's name (which is given) and the counter and one to api.stackexchange.com/2.2/tags/tag_names/wikis?site=stackoverflow
. Of course, you can apply whatever filters you want and change the sitename and tag names. Here's a JavaScript sample:
const key = ''; // &key=xxxx - not necessary, but it increases daily API quota from 300 to 10000
const sitename = 'stackoverflow'; // default, change it to whatever you want
const tags = 'php;javascript;java;jquery;perl;python'; // semicolon-separated, must be =<20
// First API call: get tag name and counter
$.get(`//api.stackexchange.com/2.2/tags/${tags}/info?order=desc&sort=popular&site=${sitename}&filter=!-.G.68pp778y${key}`,
function(data_counter) {
// Second API Call: get tag's excerpt
$.get(`//api.stackexchange.com/2.2/tags/${tags}/wikis?site=${sitename}&filter=!*Ly1)NvM)n91RtK*${key}`,
function(data_excerpt) {
for (let i = 0; i < data_counter.items.length; i++) {
$('table').append(`
<tr>
<td>${data_counter.items[i].name}</td>
<td>${data_excerpt.items.find(name => name.tag_name === data_counter.items[i].name).excerpt}</td>
<td>${data_counter.items[i].count}</td>
</tr>
`);
}
$('#quota').html(`API Quota remaining: ${data_excerpt.quota_remaining}`);
}
)
}
)
table, p {
font-family: sans-serif;
border-collapse: collapse;
}
td, th {
border: 1px solid #2d2d2d;
padding: 4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Tag Name</th>
<th>Excerpt</th>
<th>Number of posts</th>
</tr>
</tbody>
</table>
<p id="quota"></p>
来源:https://stackoverflow.com/questions/61055161/how-to-get-information-from-different-objects-with-1-call