[TBA]: API Client Development

TBA currently offers an API client in PHP that can be found here.

I know Eric Haskins has written a .NET port that can be found here for now, and Team 1515 is writing a Rails port. I think myself or Chris Jelly will be working with these guys to make their ports available from the TBA site.

Has anyone else written a port of the Client Library? What issues have you had porting it?

I just finished a Ruby (on Rails) client library.

Feel free to use it. :slight_smile:
Have any questions, comments, bug fixes, complaints, or just want to talk to someone who knows Ruby, contact me via:

AIM/Skype: talk2kern
MSN/Email: [email protected]

tba_ruby.zip (1.72 KB)

tba_ruby.zip (1.72 KB)

I attached a copy of my .net client. Currently the only documentation is the code, but if there is any interest I can finish better documentation.

It requires .net 3.5, and Visual Basic 2008 Expressor Visual Studio 2008 or greater.

Currently there are two projects, a class library with the client, and a console app which can be used to query the API. The readme file has instructions how to set your API key in the config file.

I have put the code under the Microsft Public License, so you are free to modify or redistribute it how ever you want.

If anyone has questions, comments, bug reports, ect., please contact me. My Skype, MSN, and e-mail, are in my profile, you can contact me using any of those methods.

EHaskins.TBALib v1.zip (265 KB)

EHaskins.TBALib v1.zip (265 KB)

I attached the compiled version of the code I posted above.

EHaskins.TBALib v1 Bin.zip (19.7 KB)

EHaskins.TBALib v1 Bin.zip (19.7 KB)

I’ve updated the documentation and files available for download at: http://www.thebluealliance.net/tbatv/api/

I’ll write a pretty page to wrap around it at some point.

Here is v1.1 of my library.

There is only one bug fix.

  1. When loading matches, some of the matches at the 2007 Florida regional are missing team numbers, and the parser would crash. That has been resolved.

    EHaskins.TBALib v1.1.zip (327 KB)
    EHaskins.TBALib v1.1 Bin.zip (19.9 KB)

    EHaskins.TBALib v1.1.zip (327 KB)
    EHaskins.TBALib v1.1 Bin.zip (19.9 KB)

Also found a bug in my Ruby code when I actually used it in practice for my application.

This’ll make the code more Ruby-like and return false if there are no results found but an error does not occur.

Happy coding!

tba.rb.zip (1.84 KB)

tba.rb.zip (1.84 KB)

Both updated libraries have been added to http://www.thebluealliance.net/tbatv/api/.

Thanks guys!

Would it be useful to create a SourceForge, CodePlex, GoogleCode, ect, site where we could mantain a record of any client libraries people develop?

Just a thought.

I’m not sure we’re at that level of complexity yet, but it is an idea worth keeping in mind if the number starts to get big or there is much need to change them. :slight_smile:

This is very cool stuff…I’m using the TBA API now for the Boilermaker Regional website, and I hope to expand our usage once I have some time to explore all of the possibilities…

I’ll try and pay it forward with a Python connector…I’ve only had my API key for 12 hours or so, so I haven’t tested this extensively but it should work fairly well. :smiley: If you notice any bugs or anything that could be improved, let me know!


tba.zip (1.79 KB)

tba.zip (1.79 KB)

Here’s an update to my Python connector…this adds a function to turn the results into plain ol’ Python dictionaries and lists, so it’s really easy to use, no XML module knowledge required.

tba.zip (2.38 KB)

tba.zip (2.38 KB)

Huh, great minds think alike. I’ve also been working on using TBA in python. Oh well. We’ll see where that goes; I’m done with the get<items>() functions, but I want to build on that so it is Object-Oriented.

I noticed other threads with people having problems. PHP5 has this great new thing called Exceptions. If there is an error, it will kill the execution of the script, unless the programmer has a method of catching and handling the exception.
The official PHP client is serverely lacking in error checking, for instance, if the API server returns a blank XML (XHTML, say) document, then no error will pop up.

I’ll keep it simple, my edits are public domain. I have not tested this, but it should work.

function tba_send_request($method, $arguments=array()){
  $api_key = ""; //replace with your API key
  $api_url = "http://thebluealliance.net/tbatv/api.php"; //this should not change
  // prepend the default array elements
  $arguments = array('version'=>1, 'api_key'=>$api_key,'method'=>$method) + $arguments;
  $file = $api_url.'?'.http_build_query($arguments);
  $xml = simplexml_load_file($file);
  // check for a broken connection, bad XML, etc
  if($xml===false) throw new Exception("Bad XML returned getting file $file");
  // check for errors thrown by the server
  if (count($xml->error)){
    throw new Exception("Error generated by API server: ".(string)$xml->error->text, (int)$xml->error->code);
    return $xml;

While you could put these functions into a class, and have the API key be a property of the object, I would not recommend it (at least for PHP), there is no benefit to using object-oriented programming here (since the most objects you will ever need is one).

I’ve been experimenting with the API, and for compatibility reasons I’ve created a .net 2.0 port of my .net 3.5 client. I haven’t done any performance comparisions, so don’t ask about that.

The demo console app also has some small improvments, but nothing too major.

EDIT: Added license and readme to source file, and added compiled version.

TBA Test Console net20 bin.zip (19.8 KB)
TBA Test Console net20.zip (302 KB)

TBA Test Console net20 bin.zip (19.8 KB)
TBA Test Console net20.zip (302 KB)

Not to start a flame war, but no one cares about .net or Microsoft’s open-source-wannabe-license. Public domain or a simple “Redistribute alike” clause offers more then enough protection.
<rant>Look at Kevin Watsons code, the entire license is three sentences, including “Thanks.” And even if we did care about a bigo license, no one uses .net for anything anyways, since the number of platforms I can use it on I can count on one hand, no, one finger!</rant>

On a slightly related note, I was going to code up a test console in php-gtk, but there wasn’t enough room to attach php5.dll</irony>

<edit>I remember why I came here: You are not having any problems with server load at all? It takes a measurable amount of time to ping the page for data, are we interested in caching the data at all? You are going to need to send HTTP caching headers first, which I do not see currently (i.e. HTTP/1.1 304 Not Modified)</edit>

I don’t normally reply to people when they post something totally unrelated to the current thread, but I thought I’d do you a favor and correct some of your bigger misconceptions.

Its never a good idea to release code without an accompanying license, both for you and anyone who wants to use it. If I use someone’s code which isn’t licensed properly I could be leaving myself vulnerable, no matter how small that vulnerability is, its safer to attach a real license, any license, so everybody involved knows their rights and responsibilities.

MsPL just happened to be the first license I saw which had terms I agreed with.

That is just not true. For business apps .net is more popular than Java(#2), there are millions of ASP.net web servers, and over 4000 open source projects are hosted on CodeProject.

Also false, unless you have some serious disfigument, in which case I applogize for the next paragraph. .net currently runs on Linux, Mac, XBox 360, Zune*, and of course Windows. Also some people count the web scenarios seperatly, which includes Windows(IIS or Apache), Linux(Apache), and I believe Mac.

On a side note, did you download my code just so you could complain, or are you a closet .net fan?:wink:

  • Currently in beta.

That was supposed to be a silly fanboy rant, though I do come across as too serious too often. I do have a tiny issue with this project though, and isn’t with anything in particular, but it does seem like it was created just because, with no real purpose. That said I realize some really cool things have been implemented with the API/data server, like the BeachBots put in a flash player with videos of their matches. Still, it all seems hackish to me.

I have been using the data not to dynamically call another server every time I load a web page, but to import data to my own database of teams and scores (with penalties and a much more relational format).

Here is a web version of that simple test console for whoever to use: http://frcdb.redjacket.ws/tests/tba/console.php

If it wasn’t obvious, I am a fan and long time user of PHP and XML (I also realize PHP was not designed to be an event-based language). I am expirenced with SQL, JavaScript, and have not learned but really, really like anyways, Ruby. I don’t know what people like about Python, I have to use that thing inside SAGE Math to work out team strength algorithms, and it is the most reverse intuitive language I have ever seen (next to this one). I know C and C++ to work on the f/oss game BZFlag, and regularly use many different compilers and debuggers, including MSVC, which I will say has a great debugger (compared to, say, the text-based gdb). Never used .net, and have no need to.

Back to this API: It also seems like people are making this way overly complicated. Each of the get_* functions do the same thing, with the only variation being the method called in the URL. This is not how you program!! Good practice says you should make them the same function, and use an argument to provide different attributes. Sounds quite a bit like the tba_send_request which we are discouraged from using…
Now, In C++, different functions might be needed, as each function would return an array or std::list of structs or classes, but even then that isn’t always necessary, as you could just return a std::list of voids or extended objects.

And like I mentioned earlier, there is a serious lack of error checking in the library.

<edit>RFC 2616 is the specification for HTTP and HTTP headers, including caching. Calling get_matches with no argument results in a memory allocation error that returns invalid XML. Also, I like the fact SOAP or XML-RPC wasn’t used, but I would like to know the decision on why that was not the choice.</edit>

<edit><criticism type=“text/x-constructive+criticism”>
The API is horrible with relational data sets. I can’t go in and say “find me the links to videos for all matches played by teams in Arizona” without importing the data to a relational database and throwing together a query: SELECT c.video_url FROM team a, team_match b, match c WHERE a.state=“AZ” AND a.teamnum=b.teamnum AND b.teamnum=c.teamnum GROUP BY c.matchid

The purpose is to provide a single place to organize match data and videos of matches. Before TBA, there wasn’t a single good place to do this…FIRST seems to purge this information (at least from the Internet) yearly. For people that run team and regional event websites, this is a big timesaver because they don’t have to reinvent the wheel pulling and parsing HTML pages intended for human viewing from the FIRST website.

I think this is the preferred thing to do…I update the Boilermaker Regional website every day with TBA info using a cron job (every 5 minutes during our event), rather than hammering TBA every time someone requests a page.

I am not a fanboy of .NET or Windows by any stretch of the imagination, but disparaging the work of others (provided to you free of charge and in good faith) simply because you don’t like the platform is downright rude. Arguing the merits of one platform/programming environment/toolchain over another really has no place in this thread, and should be left to a flamewar on some less useful forum.

I would, however, encourage you to help improve the TBA API. If you really want to reduce the get* functions to one function, go ahead and do it. If you want to add more error checking, go ahead and do it. If your solution proves to be superior, we might start using it.

I will say that having those multiple functions are a good idea, since it provides functional separation of methods (though not logical).

I posted a slightly better call to tba_send_request which I have tested and works great with errors or without, in all the situations I threw at it (It will throw notices). By the way, is there a better way to link to posts?

make_array is not usually necessary (it has its purposes), because of the method-style member access provided by SimpleXML (e.g. $xml->teams[5]->teamnumber). The only reason I would want to use it is where passing an array would be necessary, like array_map or similar.

I am still interested in the choice to go with a custom format did you ever consider SOAP or XML-RPC (I don’t care for them, just want to know if that was a choice)?

Also, I know it looks cool, but PDFs are really inconvenient (I am talking about the API reference), I would focus more attention to developing media:print stylesheets, and then making PDFs second.