Jump to content
  • Sign Up

Requesting all Trading post items from API


Lumina.9648

Recommended Posts

What is the proper way to receive all the items on the Trading post via the API? I have sent a request to " https://api.guildwars2.com/v2/commerce/listings/ " but it doesn't seem to give the response in the right format? ( i save it as a string but i cant convert it afterwards... . single ids work fine) Maybe i am doing something wrong or do i actually have to somehow find all the id's for all the items and then request every item individually? Or at least request 200 at a time since i read that that is the max the server responds to or something.

Link to comment
Share on other sites

You're almost there. /v2/commerce/listings (or /v2/commerce/prices) is actually a list of item IDs, so you can already get a list of the items on the trading post right there.If you want more, it depends on what kind of data you're looking for. Do you only want a quick overview of the items on the trading post, or do you care about seeing each individual listing for each item?

https://api.guildwars2.com/v2/commerce/prices?page=1&page_size=200 for a quick overview (shows the lowest buy cost and highest sell cost)https://api.guildwars2.com/v2/commerce/listings?page=1&page_size=200 for all the details (every individual buy/sell listing)

These requests will give you the items, 200 at a time, and you'll have to increment the page number with each request. The API returns an HTTP Header "X-Page-Total" that will tell you how many total pages there are to go through.

Link to comment
Share on other sites

Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the https://api.guildwars2.com/v2/items call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

Link to comment
Share on other sites

@"Lumina.9648" said:Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the https://api.guildwars2.com/v2/items call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

Yes, you absolutely, positively should cache data you retrieve from the GW2 API appropriately. It is a very, very bad idea not to do that.

It doesn't matter at all how you choose to implement that on your side, however. You can use a class, or procedural code, or functional code, and store it in whatever you wish, including flat files, databases, or even in ping packets sent around the local network, if that tickles your fancy.

Link to comment
Share on other sites

@"Lumina.9648" said:Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the https://api.guildwars2.com/v2/items call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

Are you trying to work with the raw text? You should try and find a proper JSON parsing library for whatever language you're doing this in. That way, you'll be able to easily pick and choose which parts you take out of the API's response.

Link to comment
Share on other sites

@Eearslya.6309 said:

@"Lumina.9648" said:Another dumb question probably.. do i actually have to make a class that can save all the data that i get from the
call or is there a way to only get the name? :D I really don't want to create a class for that lol. I thought i would be enough to just save all the items to a txt file and read the Name by ID from there but im actually not really sure how to do that.

Are you trying to work with the raw text? You should try and find a proper JSON parsing library for whatever language you're doing this in. That way, you'll be able to easily pick and choose which parts you take out of the API's response.

Well i am doing my project in C# and creating a windows application with WPF and am using "Newtonsoft.Json". I saved the JSON Respons for all the items in a text file and wanted to read the name of the item based on the ID i got from the Trading post request. But not sure if that works the way i wanted it to. My programming knowledge is pretty basic i would say so i probably don't have the best approach to things :D which is why i ask all these question.

Link to comment
Share on other sites

Aha, now I understand why you were saying you had to create a class.. Boy, that JSON library is a little heavy-handed. But it looks like you should be able to get what you want with a little trick. You can create an Item class, and only put in one member, just a string for name. Then, when you're deserializing through Newtonsoft.JSON, you can do something like this (forgive me if I mess up, I don't do C#..):

try {    Item myItem = JsonConvert.DeserializeObject<Item>(json, new JsonSerializerSettings {        MissingMemberHandling = MissingMemberHandling.Ignore    });}catch (JsonSerializationException ex) {    Console.WriteLine(ex.Message);}

The MissingMemberHandling.Ignore part will tell Newtonsoft to simply ignore if the JSON has more data than the class can handle.

Link to comment
Share on other sites

Is there anywhere i can check how many items there are total for my requests? E.g. i request "https://api.guildwars2.com/v2/commerce/listings?page=0" and read the total page number from the header. Then i request every page ("https://api.guildwars2.com/v2/commerce/listings?page=" + i + "&page_size=200" ). and i would like to check if i actually get all the items. For this example i usually get around 24k.

Link to comment
Share on other sites

Yep! Just like there is a "X-Page-Total" header, there is also a header for "X-Result-Total". This will tell you the total amount of things there are on that endpoint (items, listings, skins, etc.). Not to be confused with "X-Result-Count" which only tells you how many things it returned with that single request.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...