Joe Conley Tagged integration Random thoughts on technology, business, books, and everything in between jpc2.org/name/integration 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="https://github.com/swagger-api/swagger-ui">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="https://www.getpostman.com/">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="https://github.com/josephpconley/swagger2postman">https://github.com/josephpconley/swagger2postman</a></p> <p><strong>Live Demo</strong> - <a href="http://app.josephpconley.com/swagger2postman">http://app.josephpconley.com/swagger2postman</a></p> <p>It only works with the <a href="https://github.com/swagger-api/swagger-spec/blob/master/versions/1.2.md">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="https://github.com/josephpconley/swagger2postman/issues">issues page</a>. Thanks!</p> Thu, 14 May 2015 00:00:00 +0000 jpc2.org/2015/05/14/swagger-2-postman.html jpc2.org/2015/05/14/swagger-2-postman.html Everything is a Function <p>I’ve been working on a platform that would help free us from our <a href="http://www.josephpconley.com/2014/12/08/apps-are-dead.html">dependence on apps</a>. I’m hoping this ends up being a <a href="http://www.joelonsoftware.com/items/2012/01/06.html">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="http://www.restapitutorial.com/">tutorial</a>)</li> <li>JSON (<a href="http://www.w3schools.com/json/default.asp">tutorial</a>)</li> <li>JSONPath (<a href="http://goessner.net/articles/JsonPath/">tutorial</a>)</li> <li>Handlebars.js for templating (<a href="http://handlebarsjs.com/">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="language-plaintext highlighter-rouge">this</code> as the current JSON value and <code class="language-plaintext 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="http://www.datacombinator.com/worksheet">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="https://ifttt.com/recipes/popular">most popular IFTTT recipes</a>).</p> <script src="https://gist.github.com/josephpconley/adcca77c201b3f5a55b7.js"></script> <p>This script does the following:</p> <ul> <li>Performs a geo lookup using <a href="https://developers.google.com/maps/documentation/geocoding/">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="language-plaintext 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="https://developer.forecast.io/">Forecast.io</a> (sign up to get your own api key) using the <code class="language-plaintext highlighter-rouge">lat</code> and <code class="language-plaintext 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="https://gist.github.com/josephpconley/11a556fbecd20f159eb8.js"></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="http://www.w3schools.com/xpath/">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="http://www.swingstats.com/">really cool webapp you can use to track your golf scores and handicap, SwingStats</a></p> <script src="https://gist.github.com/josephpconley/798a18bc5f789d6747c4.js"></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="http://en.wikipedia.org/wiki/Cron">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="http://www.josephpconley.com/tags/datacombinator/">DataCombinator tag</a> or <a href="http://www.datacombinator.com">sign-up for email updates</a>.</p> Mon, 26 Jan 2015 00:00:00 +0000 jpc2.org/2015/01/26/everything-is-a-function.html jpc2.org/2015/01/26/everything-is-a-function.html