32
Developing Brilliant and Powerful APIs in Ruby & Python

Developing Brilliant and Powerful APIs in Ruby & Python

Embed Size (px)

Citation preview

Page 1: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Developing Brilliant and Powerful APIs in Ruby & Python

Page 2: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Housekeeping • Session is being recorded • Email once presentation is posted • Continuous Q&A

–Twitter: #BrilliantAPIs @Ready_API –Goto Webinar chat panel –community.smartbear.com

Page 3: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Introductions

Shelby Switzer Notion Queen of API Engineering

Paul Bruce SmartBear API Team Product Manager

Page 4: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

What we’ll cover • API languages, why? • APIs in Ruby

– Rails 5 – Grape

• APIs in Python – Django – Flask

• Docs / M2M / Swagger • What else is there?

– Testing: function, load, security

– API virtualization / sandboxing

– API ecosystem

Page 5: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

APIs in language X? • Considerations

– What does my team already know? – How flush is the hiring market? – What is sustainable long-term? – What’s easier for *everyone* to deal with?

• Why in Ruby and Python? • Why not in something else?

Page 6: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

• Java (lots of overhead) • .NET C# / VB / F# (proprietary stack) • PHP (wild west) • Node.js (because javascript) • C++ (how old are you?) • GO (fledgling*)

• R (too mathematical) (WTH do I mean by that?)

Why today not other languages?

* https://www.quora.com/What-reasons-are-there-to-not-use-Go-programming-language

Page 7: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Ruby with @switzerly

Page 8: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Ruby terminology • Gem => an external library • Bundle => pack up all dependencies • Rack => HTTP pipeline and req/resp processor

•Rails, Sinatra, Grape • .rb => code • Scaffolding => resource MVC generation • Rake => make-like build utility

Page 9: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

So, Ruby & Rails: a love story • Ruby: an awesome programming language • Rails: a web framework for Ruby • Ruby on Rails

– MVC pattern – Convention over

configuration – ActiveRecord

Page 10: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Caching • Key-based caching: cache_key • ETags & conditional GETs:

stale?(last_modified: @book.updated_at.utc, etag: @book.cache_key)

Page 11: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Testing • Test::Unit or Rspec • Unit tests on controllers, models, services • Request tests for endpoints • http://matthewlehner.net/rails-api-testing-guidelines/

Page 12: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Hypermedia

ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi

See also: gem ‘roar’

Page 13: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Documentation • Swagger gem: https://github.com/richhollis/swagger-docs • Easy to integrate directly in your code • Generate docs with a rake task:

rake swagger:docs

Page 14: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Rails 5 API: Getting Started 1. Install Rails 5: git clone https://github.com/rails/rails.git cd rails/ bundle install 2. Generate API: bundle exec railties/exe/rails new ../books_app --edge --dev --api 3. Create Scaffold: bundle exec rails generate scaffold books title description:text page_count:integer 4. Migrate the database: bundle exec rake db:migrate

Page 15: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

API in Ruby: Example

Page 16: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Ruby+Grape: Sweet, not Sour • Great for small services • Can be used with Rails, Sinatra, or alone • Support for:

– Versioning – Validation – (Re)routing – Error handling / exceptions

• Very code-focused, less data/convention • Documentation? Boom.

http://www.sitepoint.com/build-great-apis-grape/ http://www.rubydoc.info/gems/grape/

Page 17: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Grape: Getting Started 1. Install Grape gem 'grape' bundle install 2. Create an API file: api.rb class API < Grape::API end

3. Create a rackup file (config.ru) to run the API app require './api' run API

Page 18: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Python with @paulsbruce

Page 19: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Similarities in Ruby & Python Lots of “MVC-like” commonalities

models | views | logical control

Templates vs. ViewSets Routing (routes file vs. urls file)

Database access (ActiveRecord vs. Django data model) Access control / permissions

Rate limiting ...

Page 20: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Python: Flask & Django

Fully featured* but a bit more work

Limited, but gets you there fast

* Gypsy jazz legend, Jean Baptiste "Django" Reinhardt

Page 21: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Python: Flask Install Python. pip install Flask-API Create a new directory Create a .py file Start typing Easy to get started, if you know REST Deployment: WSGI, FastCGI, … Database access: up to you

from flask.ext.api import FlaskAPI app = FlaskAPI(__name__)

@app.route("/example/", methods=['GET', 'POST']) def example(): if request.method == 'GET': return {'request data': request.data} elif request.method == 'POST': return '', status.HTTP_204_NO_CONTENT

raise exceptions.NotFound()

python ./yourfile.py

Page 22: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

API in Flask: Example

Page 23: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Python: Django Conventions for common API patterns and scaffolding.

Models Serializers

Permissions

Views

Routing Browsing

Page 24: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Django: Getting Started Install Python. pip install Django pip install DjangoRESTFramework Find a Python-savvy editor:

• PyCharm Community • PyScripter (Windows Only) • PyDev (Eclipse plugin)

django-admin startproject … cd …

python manage.py startapp … INSTALLED_APPS = (

“rest_framework”, “...”)

python manage.py syncdb

(write some implementation code)

python manage.py runserver

Page 25: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

API in Django: Example

Page 26: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Django vs. Flask Django

DjangoCon, increasing interest over time, Books and online tutorials; great for CRUD, overload for some

Flask

Easy to get started and create, maintenance is hard. Used by Pinterest & Linkedin

Page 27: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Django + Swagger:

Why? Because others, that’s why.

django-rest-swagger.readthedocs.org

PyYAML (mileage may vary) INSTALLED_APPS += ‘rest_framework_swagger’

patterns += url(r'^docs/', include('rest_framework_swagger.urls')), ...and poof!

/docs/ /docs/api-docs

Page 28: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Lots of APIs: what’s wrong with that?

Microservices lots of “little” distributed bits

Common problems: Spaghetti code is imminent.

Unit testing is not enough.

Functional, integration, performance,

and security testing are even more necessary than ever!

Hypermedia discrete operations / behaviors

Page 29: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

API quality across language barriers

Page 30: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Ready! API: testing & sandboxing

Page 31: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

What we’ve learned Ruby+Rails == Awesome for big projects Python == Great for as-needed APIs & scripting Importance of documentation (human & M2M)

Ready! API bridges gaps across technologies Links to blogs and examples by email

Page 32: Developing Brilliant and Powerful APIs in Ruby & Python

#BrilliantAPIs

Thank You!

Shelby Switzer @switzerly shelbyswitzer.com

• Paul Bruce • @paulsbruce • paulsbruce.io