Upgrade to Pro — share decks privately, control downloads, hide ads and more …

APIs the right way

APIs the right way

The goal of software engineering is to sustainably minimise lead time to create positive business impact, to ensure this and also get some good sleep at night, API developers need to understand the right way to build APIs. In these slides, I explain the key concepts and tools needed to achieve this.

Presented at the forLoop Ibadan: Learn, Engage and Grow that held June 3rd, 2017 at the Wennovation Hub.

Event Link: http://forloop.ng/events/forloop-ibadan-engage-learn-and-grow-j2u5ueys

Adeyemi Olaoye

June 03, 2017
Tweet

More Decks by Adeyemi Olaoye

Other Decks in Programming

Transcript

  1. Meet me Adeyemi Olaoye Ex Software Engineer Lead - Web

    Architecture and Integration adeyemiolaoye.me @yemexx1
  2. The right way? “...an API must be easy to find

    and easy to register to use, and it must be clear how much the API can be trusted in mission-critical solutions.” - IBM “The goal of software development is to sustainably minimise lead time to create positive business impact” - Dan North “...and for the sake of good sleep at night” - Me
  3. GET Vs POST ➔ Don’t POST https://example.com/customers/1/accounts?active=1 ➔ Do GET

    https://example.com/customers/1/accounts?active=1 • Use GET for retrieving data; bookmarkable resources with cacheable responses • Use POST for resources/endpoints that create, extend or annotate ➔ Don’t GET https://example.com/customers/1/beneficiaries?firstnam e=Ade&lastname=ola&account_number=1238213719 ➔ Do POST https://example.com/customers/1/accounts { "firstname":"Ade", "lastname":"ola", "account_number":1238213719 }
  4. POST Vs PUT • Use PUT for idempotent operations ➔

    Don’t POST https://example.com/accounts/2 ➔ Do PUT https://example.com/customers/1/accounts { "profile_image_url":"https://imgur.com/dhy325" } { "profile_image_url":"https://imgur.com/dhy325" }
  5. Transformers? { "name":"covfefe enterprises", "image_url":"https://example.com/dhy325.png", "status":"closed", "reviews_count":10, "store_tags":["press", "world"] }

    { "name": "covfefe enterprises", "image_url": "https://randomcdn.com/example/dhy325.png", "status": "closed", "reviews_count": 10, "store_tags": [ "press", "world" ], "products": { ... }, "reviews": { ... } } transformers
  6. Benefits ➔ Data sideloading ➔ Reduce boilerplate code ➔ Separate

    concerns ➔ Minimise service time by using eager loaders
  7. Samples //Task: load all products with their images and reviews

    //create transformer ... class ProductTransformer implements TransformerInterface { public function transform(Product $product) { return $product->toArray(); } public function includeImages(Product $product) foreach($product->images as $image){ $image->link = Commons::convertToCDNLink($image); } return $images->toArray(); } public function includeReviews(Product $product) { return $product->reviews->toArray(); } ... Full gist //fetch products and transform $products = Products::getProducts(); $products->eagerLoad(['Images', 'Reviews']); //transform data $transformerManager = new TransformerManager(); $collection = $tranformerManager->createCollection($products, new ProductTransformer()); //convert collection to array or prefferred output $productsData = $collection->createData()->toArray(); //ship it!
  8. Consistency ➔ Don’t GET https://example.com/stores/2 ➔ Do GET https://example.com/stores/1 {

    "name":"covfefe enterprises", "image_url":"https://imgur.com/dhy325", "status":"closed", "reviews_count":10, "store_tags":["press", "world"] } { "Name":"covfefe enterprises", "imageUrl":"https://imgur.com/dhy325", "status":"closed", "reviews_count":10, "store tags":["press", "world"] }