Joe Conley Tagged point.io Random thoughts on technology, business, books, and everything in between jpc2.org/name/point.io Building better APIs with Play! <p>This is the technical companion to my Point.io post, <a href="http://point.io/article/building-better-apis-play">Building better APIs with Play!</a>. Herein lies coding examples galore!</p> <h2 id="restful-architecture---routing">RESTful Architecture - Routing</h2> <p>The routes file of a Play app allows you to define the HTTP verb, the route path/pattern, and the corresponding method from the controller. In addition to denoting basic path variables, you can also use regular expressions to match on specific routes (i.e. xml or html formats for example). What’s great about this approach is this file is compiled along with the source code, so any mistakes like an incorrect controller method or invalid HTTP verb will be caught and thrown at compile time.</p> <script src="https://gist.github.com/josephpconley/9337208.js"></script> <h2 id="action-composition">Action composition</h2> <p>We define two types of custom actions: atomic and composed. Atomic actions can be used stand-alone or as building blocks to be composed with other actions. We use the following pattern for building an atomic action.</p> <script src="https://gist.github.com/josephpconley/9345681.js"></script> <p>The object allows us to use the action by itself, and the case class allows us to compose this action with other actions.</p> <script src="https://gist.github.com/josephpconley/9345730.js"></script> <p>A composed action is strictly syntactic sugar, making it more convenient to combine behaviors and keeping the controller code more concise. We define composed actions using just an object.</p> <script src="https://gist.github.com/josephpconley/9345746.js"></script> <h2 id="filters">Filters</h2> <p><a href="http://www.playframework.com/documentation/2.2.2/ScalaHttpFilters">Filters</a> are handy for cross-cutting concerns. We’ve had one use case where it was necessary to modify every JSON response with links to metadata. We achieved this using a filter and the <a href="http://www.playframework.com/documentation/2.2.2/Enumeratees">Play Enumeratee library</a></p> <script src="https://gist.github.com/josephpconley/9345957.js"></script> <h2 id="json---global-messaging">JSON - Global messaging</h2> <p>Building an effective API requires being responsive to users in a comprehensive manner. All concievable events should be handled appropriately, such as incorrect requests from the client or internal server errors. Creating a Global object allows you to generically craft responses to handle these situations. We define methods to handle events like internal errors, route not found, or a bad request.</p> <script src="https://gist.github.com/josephpconley/9345819.js"></script> <h2 id="conclusion">Conclusion</h2> <p>Play is well-equipped to handle the nuances of API development and maintenance. We’re pleased with the stability and performance we’ve seen thus far and are looking forward to continuing down this path of <a href="http://www.reactivemanifesto.org/">reactive goodness</a>.</p> Tue, 04 Mar 2014 00:00:00 +0000 jpc2.org/2014/03/04/building-better-apis-with-play.html jpc2.org/2014/03/04/building-better-apis-with-play.html (Triz)Swagging out at the Philly Codefest <p>This past weekend I teamed up with some buddies from <a href="http://point.io">Point.io</a> (<a href="https://twitter.com/twrivera">Angel</a>, <a href="https://twitter.com/jxshin75">Jon</a>, and <a href="https://twitter.com/dyang_pointio">Dylan</a>) to participate in my first hackathon, <a href="http://phillycodefest.com/">Philly Codefest</a>. We spent the weekend bringing Angel’s dream to life: a platform called TrizSwagger to analyze and track the use of “swag” (i.e. T-shirts, office supplies, and other marketing mathoms). We leveraged social media and geolocation to give companies real-time visibility to their marketing campaigns. Feel free to check out the app <a href="http://www.trizswagger.com/">here</a>.</p> <table class="image"> <caption align="bottom">Angel demoing Point.io's apiDoc service</caption> <tr><td><img src="/assets/angel.jpg" alt="Angel promoting Point.io" /></td></tr> </table> <p> <br /></p> <h2 id="lessons-learned">Lessons Learned</h2> <p>Good programming is always concerned with simplicity and efficiency, whether it’s using efficient data structures and algorithms, conciseness in your codebase, or even naming variables properly. Building an app in 24 hours, however, throws the need for simplicity and efficiency into sharp relief. Here are a few takeways from my experience.</p> <h3 id="coast-to-coast-json">Coast-to-Coast JSON</h3> <p>I’ve always been a big fan of <a href="http://en.wikipedia.org/wiki/Domain-driven_design">Domain-driven Design</a>. Writing POJOs in Java and case classes in Scala can provide a clear crystallization of the main actors of your application. However, models may not always be necessary, especially if your app is backed by a service/database which gives you JSON (TrizSwagger is backed in <a href="http://www.mongodb.com/">MongoDB</a> and served by <a href="http://flask.pocoo.org/">Flask</a>). The extra layer of complexity in marshalling/unmarshalling between JSON and your model can hinder performance and readability of your code, especially if you’re using heavy ORM frameworks like Hibernate. During a hackathon, if you’re rapidly making changes to the model you’ll surely get slowed down. For a more thorough treatment of JSON Coast-to-Coast, check out <a href="http://mandubian.com/2013/01/13/JSON-Coast-to-Coast/">the Mandubian Blog</a></p> <h3 id="knockoutjs-mapping-plugin">Knockout.js Mapping plugin</h3> <p>In order to implement coast-to-coast design effectively, it’s important to have a front-end framework that manages JSON well. One such framework I’m fond of is Knockout.js, more specifically their <a href="http://knockoutjs.com/documentation/plugins-mapping.html">mapping plugin</a>. This plugin automatically maps a JSON message into a Javascript observable object. You can then code the front-end directly against object properties without having to pre-define a viewmodel. You can also customize how objects are mapped by either modifying or enhancing the created object. This strategy proved quite helpful during the hackathon as any changes to our back-end API literally only had to be changed in one place (the front-end).</p> <p>One caveat is the creation of a new object using this plugin. Since the plugin requires a JSON object to build out the observable, I wrote a basic method in Play to take an expected JSON object and “empty” it, setting default values that would be used in the new object form.</p> <script src="https://gist.github.com/josephpconley/9207995.js"></script> <h3 id="understanding-your-tools">Understanding your tools</h3> <p>TrizSwagger integrates with both Twitter and Facebook. Understanding and setting up those integrations, however, occupied a lot of our time. We also ran into issues with a server on OpenShift, slowing us down further. Ultimately I think simpler is better, and every choice in technology needs to be well thought-out and well-suited to its use case.</p> <h2 id="conclusion">Conclusion</h2> <p>Overall it was an awesome experience. Even though we didn’t win (or place, or show for that matter), we learned a lot and we still took the time to mentor other teams who were using the <a href="http://point.io/pointio-platform">Point.io API</a>. Great job Team TrizSwagger!</p> <table class="image"> <caption align="bottom">Jon, Dylan, and Joe doing some last-minute coding</caption> <tr><td><img src="/assets/triz.jpg" alt="Jon, Dylan, and Joe doing some last-minute coding" /></td></tr> </table> Tue, 25 Feb 2014 00:00:00 +0000 jpc2.org/2014/02/25/phillycodefest-trizswagger-lessons-learned.html jpc2.org/2014/02/25/phillycodefest-trizswagger-lessons-learned.html Scala 101 <p>I recently gave an introductory talk about Scala for my unintiated Point.io hackers. Here’s the slides for future reference. Enjoy!</p> <div class="row"> <iframe src="//www.slideshare.net/slideshow/embed_code/43254707" width="800" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe> </div> Wed, 29 Jan 2014 00:00:00 +0000 jpc2.org/2014/01/29/scala-101.html jpc2.org/2014/01/29/scala-101.html