Joe Conley Tagged integration Random thoughts on technology, books, golf, and everything else that interests me Swagger2Postman <table class="image"> <tr> <td><img src="/assets/swagger.png" alt="Swagger Logo" /></td> <td><img src="/assets/postman.jpg" alt="Postman Logo" /></td> </tr> </table> <p>I’ve recently been working on a Swagger-documented API where it’s been necessary to test the API endpoints in different environments. While <a href="">Swagger UI</a> is a simple, powerful tool for testing APIs, it doesn’t streamline well across multiple environments, nor does the UI allow for testing certain edge cases (i.e. what response will I get if I omit a required parameter?). However, <a href="">Postman</a> is very well-suited for both of these use cases, and so I’ve built a library called <strong>Swagger2Postman</strong> to convert Swagger docs into a Postman JSON collection.</p> <p><strong>Code</strong> - <a href=""></a></p> <p><strong>Live Demo</strong> - <a href=""></a></p> <p>It only works with the <a href="">Swagger 1.2 specification</a> as of now so if you have any feedback or want to see it work for Swagger 2.0, let me know in the comments section here or on the project’s <a href="">issues page</a>. Thanks!</p> Thu, 14 May 2015 00:00:00 +0000 Everything is a Function <p>I’ve been working on a platform that would help free us from our <a href="">dependence on apps</a>. I’m hoping this ends up being a <a href="">horizontal tool</a> with a combinatorial number of use cases (hence the name DataCombinator). The main idea is to turn all digital interactions into simple functions (a la Excel) which can be composed to do cool stuff.</p> <p>Here are the main concepts you need to know to understand how to execute and compose these functions</p> <ul> <li>HTTP methods/requests and APIs (<a href="">tutorial</a>)</li> <li>JSON (<a href="">tutorial</a>)</li> <li>JSONPath (<a href="">tutorial</a>)</li> <li>Handlebars.js for templating (<a href="">tutorial</a>)</li> </ul> <p>That’s it. Composing functions is as easy as writing a sequence of commands (separated by semi-colon). Each function will return a valid JSON value, which will be passed to the context of the subsequent function. <strike>The "current" JSON value can be accessed using the `_` character, and can also be used in argument strings using the Handlebars.js syntax.</strike></p> <p><strong>UPDATE</strong> As I was going through some various examples it became clear to me that I needed both the most recent JSON value as well as past ones, so to that end I’ve modified the scripting language to use <code class="highlighter-rouge">this</code> as the current JSON value and <code class="highlighter-rouge">_</code> to represent the array of all results in the script (with the 0th entry being the JSON value representing arguments passed to the script). I’ve updated the examples below accordingly.</p> <h2 id="examples">Examples</h2> <p>To quote George R. R. Martin, “Words are wind”, so let’s look at some actual code. You can test them out and see the full list of available functions <a href="">here</a>. If you’re confused by any of the following examples, try out the worksheet as each function call result is shown.</p> <h3 id="weather-alerts">Weather Alerts</h3> <p>Here’s an example of a custom weather alert service (one of the <a href="">most popular IFTTT recipes</a>).</p> <script src=""></script> <p>This script does the following:</p> <ul> <li>Performs a geo lookup using <a href="">Google Maps</a> to get the geocoding details of the address we’re interested in (in this case my office address). <ul> <li>I added a Handlebars.js helper called <code class="highlighter-rouge">urlEncode</code> to facilitate things like url encoding</li> </ul> </li> <li>Navigates the result to find the latitude and longitude</li> <li>Gets the short-term weather forecast from <a href=""></a> (sign up to get your own api key) using the <code class="highlighter-rouge">lat</code> and <code class="highlighter-rouge">long</code> from the previous call (using Handlebars to pass the values)</li> <li>Sends an SMS message to my phone if it’s below 40</li> </ul> <p>Pretty straightforward. The beauty of this is we can customize it to our preferred notification channel, and could easily swap out the SMS call with e-mail or Twitter.</p> <h3 id="auto-generate-rss-based-on-website-updates">Auto-generate RSS based on website updates</h3> <p>Despite the demise of Google Reader, I’ve always been a big fan of RSS. It tends to be a less noisy channel of information than social media, allowing for less frequent but longform communication. This example checks the AV Club for reviews of new episodes for a given show (I chose game-of-thrones-experts for this example, you’ll have to inspect the URL for your show) and generates an RSS feed.</p> <script src=""></script> <ul> <li>Performs a web request to get the html source code of the webpage</li> <li>Performs an XPath query to extract the HTML elements we need (and implicitly converts to JSON)</li> <li>Uses the resulting JSON to build an RSS xml file from a Handlebars.js template</li> </ul> <p>This has some more advanced concepts like <a href="">XPath</a> (not to mention knowing proper RSS formatting), but the script is still relatively easy to understand.</p> <h3 id="website-monitoring">Website monitoring</h3> <p>As an owner/maintainer of a few websites, it’s important to know when any of them go down. There are several free services that will do this for you, but what if we wanted a customized view into our website uptime? Let’s write a script that monitors a <a href="">really cool webapp you can use to track your golf scores and handicap, SwingStats</a></p> <script src=""></script> <ul> <li>Performs a web request to the site we’re monitoring</li> <li>Save the response info in a MongoDB database collection</li> <li>If the status is not 200 (OK), then send an e-mail</li> </ul> <p>This not only handles notifications but saves website data in a database (MongoDB). We can then build a separate script to access this data and calculate uptime percentage for SLA purposes.</p> <h2 id="next-steps">Next Steps</h2> <p>I hope these examples were interesting. DataCombinator will soon have the ability to save scripts, expose them as API endpoints, and schedule script execution on a specific time interval using <a href="">Cron expressions</a>. The next version willl also expose your favorite social network functions, which could lead to things like one massive interleaved activity stream for all of your social media sites. If you’re interested in updates, follow along on the <a href="">DataCombinator tag</a> or <a href="">sign-up for email updates</a>.</p> Mon, 26 Jan 2015 00:00:00 +0000