User:Dagger/Widget drafts/API

From Guild Wars 2 Wiki
Jump to navigationJump to search

Description[edit]

I wrote a thing that can pull account data from the GW2 API and insert it into wiki pages. The end result looks like this: https://i.imgur.com/RwmC3f1.png or this: https://i.imgur.com/ICPIoLe.png. I've written support for displaying:

  • Which characters have a given recipe unlocked
  • How much of an item you have (in your bank, in your materials storage and on each char)
  • Whether you have a given skin, miniature or dye unlocked
  • How much of a given wallet currency you have (although I'm not sure where to display it)

Since I don't have access to the Widget namespace, it's currently implemented as a bunch of JS files, which you can manually import if you want to try it out.

Your API key gets stored in your browser's Local Storage. All the API queries are done client-side and the responses are also cached in Local Storage, to avoid hitting expensive API endpoints on every page load.

Instructions[edit]

You can test this out yourself if you want, however you really shouldn't run arbitrary JS code on your wiki account without checking over it first.

  • Add this: importScript("User:Dagger/Widget drafts/API cache.js"); to your common.js page (or you can run it in your browser's Scratchpad on every page load).
  • Click "Set an API key" in the sidebar, and set an API key.
  • Check out the test cases below, and/or any item/skin pages once I edit the infoboxes to include the appropriate spans.

Implementation[edit]

Each of these is supposed to correspond roughly to a widget in the style of [[Widget:TP prices]] (although it could all just be merged into one, or even put into site-wide JS -- no queries are made unless there's something on the page that's requesting them. The core API object in particular might be useful for other uses of the API on the wiki, especially if it's rewritten to not require whitelisting specific endpoints.)

Issues[edit]

  • Wallet currency pages have no infobox, so there's nowhere to neatly display how much of them you have. (Maybe it'd be useful in the tooltips of things like "Flame Legion Coat, Cost: 330 Flame Legion Charr Carvings"?)
  • Displaying "You have: None" for a ton of items sucks. But hiding this row until the API query comes back means that part of the infobox will jump downwards when it's shown.
  • How long to cache responses for? It's currently 5 minutes (which is how long the API itself caches responses for), but maybe much longer (1 hour? 12 hours?) would be better.

Test cases[edit]

Thing Result Expected result Markup
Basic Salvage Kit 2 on Wiki Test Character
1 on Other Test Character
<span class="api-youhave api hide" data-itemid="23040">…</span>
Mithril Ore 250 in materials
20 on Wiki Test Character
<span class="api-youhave api hide" data-itemid="19700">…</span>
Althea's Ashes On Wiki Test Character <span class="api-youhave api hide" data-itemid="39233">…</span>
20 Slot Fractal Uncommon Equipment Box 4 on Wiki Test Character <span class="api-youhave api hide" data-itemid="38013">…</span>
Xunlai Electrum Ingot None <span class="api-youhave api hide" data-itemid="46743">…</span>
Boon of Might None <span class="api-youhave api hide" data-itemid="8445">…</span>
Staff Yes <span class="api-skinunlocked api hide" data-skinid="3872">…</span>
Gold Fractal Staff No <span class="api-skinunlocked api hide" data-skinid="70785">…</span>
Handful of Trail Mix Wiki Test Character
Other Test Character
<span class="api-unlockedon api hide" data-recipeid="2841">…</span>
Handful of Trail Mix Wiki Test Character
Other Test Character
<span class="api-unlockedon api hide" data-recipeid="2841" data-disciplines="chef">…</span>
Recipe: Gift of Blades (List of chars with no dupes) <span class="api-unlockedon api hide" data-recipeid="8455, 8459, 8460, 8455, 8459, 8460">…</span>
Glob of Elder Spirit Residue Wiki Test Character
Other Test Character
<span class="api-unlockedon api hide" data-recipeid="7320" data-disciplines="artificer,huntsman,weaponsmith">…</span>
Glob of Elder Spirit Residue Wiki Test Character
Other Test Character
<span class="api-unlockedon api hide" data-recipeid="7320" data-disciplines=" artificer, Huntsman,weaponsmith ">…</span>
Tray of Garlic Bread No characters <span class="api-unlockedon api hide" data-recipeid="3264" data-disciplines="chef">…</span>
Winter Sky Dye Yes <span class="api-dyeunlocked api hide" data-colorid="362">…</span>
Enameled Solitude Dye No <span class="api-dyeunlocked api hide" data-colorid="1279">…</span>
Mini Queen Jennah Yes <span class="api-miniunlocked api hide" data-miniid="177">…</span>
Mini Karka No <span class="api-miniunlocked api hide" data-miniid="113">…</span>
Coin 12345678 <span class="api-wallet api hide" data-currencyid="1">…</span>
Karma 1234567 <span class="api-wallet api hide" data-currencyid="2">…</span>
Laurel 123 <span class="api-wallet api hide" data-currencyid="3">…</span>
Fractal Relic 1234 <span class="api-wallet api hide" data-currencyid="7">…</span>
PvP League Ticket 0 <span class="api-wallet api hide" data-currencyid="30">…</span>