My Selfishly Needed Flights App
The inspiration
I ran into a problem.
I was searching for flights to Japan for a trip which I was pretty flexible on the particular departure and return dates, so I could get the best deal.
Kayak offers a search with a +/-3 days on to either side of the dates, but even this wasn't enough. Google Flights was a little better offering a full calendar view of dates and prices. However, even
this wasn't good enough for what I was looking for. The Google Flights calendar wasn't always updating when I wanted it to, (eg, the month I was departing from loaded but for a month long trip the next month wasn't showing anything until I moved it forward another month, perhaps a config option, but not an easy one to turn on and off, more likely to save from doing extra processing). And, the I wanted to check out different cities. Perhaps, I could purchase two separate tickets instead of one ticket on a flight with a connection. Or, maybe I could drive to Houston or Dallas if it would save a significant amount of money. And, there were some airlines I preferred to be on for the 14 hr flight, and I was even okay with mixing and matching them. (ANA, Delta, and Singapore Air were my preferred choices, ended up going with Delta's new airbus A330 from LA to Tokyo and Southwest from Austin to LA)
Then the light switched on, and I said, "I'll just build a simple flight search app myself."
The Prep
This was the perfect opportunity to learn some new skills, do something I enjoy, and getting something useful out of it at the same time.
I checked for to see if Google had the API available for use. And to my glorious surprise, they did!
https://developers.google.com/qpx-express/
And to my luck, you could use the api for free up to 50 search requests a day. This was perfect, I didn't need anything more than that. Also, they have an demo page where you can input your search parameters in a simple UI and see what the search request would look like formatted in a JSON string.
First I had to decide the tech stack. I've worked mostly in Java, so I thought I'd try to make that work. I was looking for http client libraries like Apache HttpClient, when I came across one of the new features to Java 9: an integrated http client. Sweet! My next goal was to simply use jdk 9 for the entire project.
Writing the Program and Testing
I moved forward finding out how to use Java 9's new Http client classes. It wasn't too much of a problem, actually quite simple and quick. I wanted to POST as I would be sending a JSON string with the http search request to include all of the parameters. It would've been a nightmare to try to include all of the parameters programatically in the URL even in the simplest of searches. I fired up the demo site and set up a simple search and copied the JSON string to a file. I just wanted to see if the POST would work, and even better if the search request was valid.
Alas, after some configuration to the credentials, I was able to get it to hit the API. Success! But the search was in a bad format! Oh no!
I copied the JSON string straight from the demo site, and double checked it to make sure nothing was changed. I thought that was sufficient evidence that it's likely not the formatting of the string but my program was messing with it or I was using the http client wrong. After a lot of debugging, I determined it was that the JSON string being parsed from a text file using FileReader and BufferedReader required the newline (\n) and carriage return (\r) escapes to be entered in the file. The demo site didn't include these things.
What eventually keyed me off was using Postman to get the search to run successfully, just created the right URL and copied the JSON string from the demo site. This worked, and Postman has a cool feature that allows you to get the request in several different programming formats. One of them included the newline and carriage returns, and I tried it with success in my program.
The Kind of Sad Conclusion
This was roughly two weeks ago, and, alas, earlier this week I receive an email that Google will be shutting down the API. I learned that there was some history in how Google came to own the software and there was a 5 year ticking clock that was nearing the end.
I still view this as a success as I was able to learn some cool features of Java 9 and work through debugging my own simple program. I could've just used curl or some Postman, but I wanted to create my own app. The next step would've been creating the JSON string from user inputted search parameters instead of just reading the already created JSON string from a text file. If I do continue with this, (I have until April 2018 to continue using it), I would have to make a tough decision about trying to do the entire project with just jdk 9 or using an external library as the JSON classes that helped create JSON objects and strings in the proper format were stripped out.
*I have no affiliations with any of the companies or sites mentioned.