API Request Java Class Generator

Avatar image for kingschiebi
kingschiebi

103

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#1  Edited By kingschiebi

Hi,

while working on my other stuff, I found it a bit tedious to write all the API requests manually. Therefore I wrote a little helper tool that parses the API documentation page and creates a couple of classes that automagically create the correct request URL for all available filters at each resource. That means, if the API changes, simply run it again and you'll have a fresh set of Java classes with all the correct fields set and the description added as a javadoc comment.

It's just a quick'n'dirty hack to save some time, but maybe someone finds it useful.

I considered doing the same for the result fields of a request, but figured that it'll take too long to write each format implementation and come up with some elaborate testing to define the return types.

Feel free to modify the code to your needs or add support for more languages and remember to allow the application to contact http://api.giantbomb.com/documentation.

Edit: (Some usage info)

To use the classes, download the tool and run it using:

java -jar gb-api-parser.jar [DIRECTORY]

You can specify your preferred directory which will also set the default package name or leave it empty and it will default to gbapi.

After that, copy the results into your project src folder (you might have to adjust the package name manually).

Here is a simple example on how to use one of the classes:

GBApi api = new GBApi(); Search searchRes = api.new Search(); searchRes.setApi_key(_api_key); searchRes.setField_list("name,image,api_detail_url,site_detail_url"); searchRes.setFormat("jsonp"); searchRes.setJson_callback("callback"); searchRes.setOffset("10")); searchRes.setQuery("Stuff"); searchRes.setResources(resource);
String url = "http://api.giantbomb.com/search/" + searchRes.toUrl();

After that, use your preferred method of invoking the URL and handle the results.

Cheers

Avatar image for flashdim
flashdim

52

Forum Posts

703

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#2  Edited By flashdim

Great idea, but I'm getting the following when I try to run it:

javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,118]

Message: Scanner State 24 not Recognized

at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)

at de.dnadns.gbomb.apibuilder.ApiParser$ApiBuilder.readApiDoc(ApiParser.java:247)

at de.dnadns.gbomb.apibuilder.ApiParser$ApiBuilder.<init>(ApiParser.java:149)

at de.dnadns.gbomb.apibuilder.ApiParser.main(ApiParser.java:43)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)