phalcon 2 high performance apis - devweekpoa 2015

50
High Performance APIs

Upload: jackson-f-de-a-mafra

Post on 15-Apr-2017

432 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Phalcon 2 High Performance APIs - DevWeekPOA 2015

High PerformanceAPIs

Page 2: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Jacksonfdamhttp://about.me/jacksonfdamhttps://bitbucket.org/jacksonfdamhttps://github.com/jacksonfdamhttp://linkedin.com/in/jacksonfdam@jacksonfdam

Page 3: Phalcon 2 High Performance APIs - DevWeekPOA 2015

APIs: Windows To The Code

In the simplest terms, APIs are sets of requirements that govern how one application can talk to another.

Page 4: Phalcon 2 High Performance APIs - DevWeekPOA 2015

APIs: Windows To The Code

APIs do all this by exposing some of a program's internal functions to the outside world in a limited

fashion.

Page 5: Phalcon 2 High Performance APIs - DevWeekPOA 2015

API Analogy

Every time you want to access a set of data from an application, you have to call the API. But there is

only a certain amount of data the application will let you access, so you have to communicate to the

operator in a very specific language—a language unique to each application.

Page 6: Phalcon 2 High Performance APIs - DevWeekPOA 2015

API Analogy

Page 7: Phalcon 2 High Performance APIs - DevWeekPOA 2015

A simple classification of APIs

Web service APIs - SOAP - XML-RPC and JSON-RPC - REST

WebSocket APIs Library-based APIs

- JavaScript - TWAIN

Class-based APIs (object orientation)

- Java API - Android API

OS functions and routines - Access to file system - Access to user interface

Object remoting APIs - CORBA - .NET

Remoting Hardware APIs - Video acceleration - Hard disk drives - PCI buses

Page 8: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Webservices

A description of the communication methods allowed • Requesting information• Sending information• Updating information

Page 9: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Webservices

Most often-used types of web service:

• SOAP• XML-RPC• JSON-RPC• REST

Page 10: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Webservices

Most often-used types of web service:

• SOAP• XML-RPC• JSON-RPC• REST

Page 11: Phalcon 2 High Performance APIs - DevWeekPOA 2015

SOAP (Simple Object Access Protocol)

SOAP is a protocol that defines the communication method, and the structure of the messages. The data transfer format is XML.A SOAP service publishes a definition of its interface in a machine-readable document, using WSDL – Web Services Definition Language.

Page 12: Phalcon 2 High Performance APIs - DevWeekPOA 2015

XML-RPC

XML-RPC is an older protocol than SOAP. It uses a specific XML format for data transfer, whereas SOAP allows a proprietary XML format. An XML-RPC call tends to be much simpler, and to use less bandwidth, than a SOAP call. (SOAP is known to be “verbose”.) SOAP and XML-RPC have different levels of support in various libraries.

Page 13: Phalcon 2 High Performance APIs - DevWeekPOA 2015

JSON-RPC

JSON-RPC is similar to XML-RPC, but uses JSON instead of XML for data transfer.

Page 14: Phalcon 2 High Performance APIs - DevWeekPOA 2015

REST (Representational state transfer)

REST is not a protocol, but rather a set of architectural principles. The thing that differentiates a REST service from other web services is its architecture. Some of the characteristics required of a REST service include simplicity of interfaces, identification of resources within the request, and the ability to manipulate the resources via the interface. There are a number of other, more fundamental architectural requirements too.

Page 15: Phalcon 2 High Performance APIs - DevWeekPOA 2015

REST Principles

It is these principles that characterize most of REST-based APIs in use today:

• Precedence of the client-server relationship• Requirement for stateless communication• Implementation of cache constraints to engender

efficiencies in network communications• Use of a uniform interface through standardized data

elements such as resources and representations

Page 16: Phalcon 2 High Performance APIs - DevWeekPOA 2015

REST Principles

The common perception is that REST offers a lightweight alternative to service-orientated mechanisms such as SOAP.

Page 17: Phalcon 2 High Performance APIs - DevWeekPOA 2015

REST Principles

REST allows for the fine-grained control of objects through the use of resources (expressed as URIs) that also allows for greater efficiency in communications with the server (obviously important when implemented on a mobile device).

Page 18: Phalcon 2 High Performance APIs - DevWeekPOA 2015

XML and JSON

The growth of REST has been complemented by the increasing use of JavaScript Object Notation (JSON) as the mechanism by which resources are represented.

Page 19: Phalcon 2 High Performance APIs - DevWeekPOA 2015

XML and JSON

Mobile apps driving the adoption of web APIs and the need for a compact payloads over constrained communication links.

The consumption of web APIs by a web developer community and the resonance of JSON-encoded data with this audience

Page 20: Phalcon 2 High Performance APIs - DevWeekPOA 2015

XML and JSON

The use of JSON over XML is not a dichotomy. You will make design decisions where you support one, both, or even other formats dependent on the intended audience.

Page 21: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

What is the difference between REST and SOAP?

Page 22: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

What's the difference between MVC and HTTP?

Page 23: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

MVC and REST are architectural patterns.

Page 24: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

HTTP and SOAP are set of rules i.e. protocols.

Page 25: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

In layman terms you are asking - What's the difference between a building plan-map and building making

process.

Page 26: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

Building Plan map is deciding how you will place different things e.g. Bathroom, dining room, doors, bed-room etc. in a

particular geographical area.

Page 27: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

MVC is deciding how you will separate concerns regarding data, business logic and controls.

Page 28: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

REST is deciding how you will use different HTTP verbs to perform one of CRUD operation on your data.

Page 29: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

Building making process is following predefined steps like first create foundation then walls and in the end go for roof.

Page 30: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

HTTP is following predefined rules like you need to send request on port 80 which will consist of two parts: HTTP

headers and HTTP body.

Page 31: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

SOAP is following predefined set of rules how and where you can place function to be called and various parameters to be

passed in that function in your XML SOAP request.

Page 32: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

SOAP is rigid XML structure. XML is overwhelmed by data-size.

REST can use XML or JSON. JSON is just key - value pairs.

Page 33: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

Use SOAP:

1. when you have no other option provided by API exposer.2. when your end user want to have only SOAP response.3. Faster development. As tools Axis 2 and cxf exist. :)

Page 34: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Layman terms

Use REST(with JSON):

1. To have cleaner software architecture.2. To save server resources (JSON is smaller to process).3. To save network resources (JSON size is smaller than XML).4. To save client side resources (JSON is much faster to parse).

Page 35: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

http://social.yahooapis.com/v1/user/{guid}/profile http://api.linkedin.com/v1/people/{guid}

Page 36: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

<?xml version="1.0" encoding="UTF-8"?> <person>

<id>111222</id> <first-name>Jackson</first-name> <last-name>Mafra</last-name> <headline>Software Engineer</headline> <connections total=”1776"> …

</person>

Page 37: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

What’s Missing?

Ability to get exactly what you need (variety) • If you need more, may require multiple API calls (if they

exist) • If you need less, resources are wasted

Consistency of responses (uniformity) • “Same” object returned by different APIs may have different

structure • Once in production, hard to get consistent later

Page 38: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful APIMultiple Calls to Get What You Need

Want to get user’s friend’s profile? Do this… http://social.yahooapis.com/v1/user/123/connections <connections yahoo:start="0" yahoo:count="1" yahoo:total="1"> <connection yahoo:uri="http://social.yahooapis.com/v1/user/123/connection/456? view=usercard"> <guid>456</guid> <contactId>4</contactId> </connection> </connections>

Page 39: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful APIMultiple Calls to Get What You Need

… then make second call to get friend’s profile:

http://social.yahooapis.com/v1/user/456/profile

<profile yahoo:uri="http://social.yahooapis.com/v1/user/456/profile"> <guid>456</guid> <birthdate>3/3</birthdate> <created>2008-08-4T17:13:56Z</created> ... </profile>

- Latent, redundant data - Optimization requires stickiness

Page 40: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

Typical Solution

Variety versus Uniformity

- Solution: introduce another call - Desire for variety of responses undermines uniformity of requests - Leads to RPC-like REST APIs - Many APIs + Great Documentation = Lots of Reading + Lack of Automation

Page 41: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful APIDomain Model as Foundation Sample Domain Model

/people : Person[] // collection of Person resources /id : string // primary key /name : string /email : string // unique key /photo : url /best-friend : Person /friends : Person[] /jobs : Job[] // collection of Job resources

/company : Company /title : string /start-date : date /end-date : date …

/companies : Company[] /name : string /ceo : Person

Page 42: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful APIDomain Model as Foundation Follow request URL to navigate through your model

To get a person’s profile: http://api.linkedin.com/v2/people/123<person uri=“urn:linkedin:v2:people/123” key=“123”>

<id>123</id><name>Reid Hoffman</name><email>[email protected]</email><best-friend uri=“urn:linkedin:v2:people/456”/></person>

Conventional URL in request Default representation in response

/people[/id=123] /id /name /email /photo /best-friend /friends … /jobs /company /title /start-date /end-date … /companies /name /ceo …

Page 43: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

Domain Model as Foundation Fine-grained Request

What if you only need certain fields (e.g., name and photo)?

http://api.linkedin.com/v2/people/123:(name,photo)

<person> <name>Reid Hoffman</name> <photo>http://media.linkedin.com/photos/123.jpeg</photo> </person> …

/people[/id=123] /id /name /email /photo /best-friend /friends /jobs /company /title /start-date /end-date …

Page 44: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful APIDomain Model as Foundation Fine-grained Request

To get names and photos of one’s friends and their best friends: …/v2/people/456/friends:(name,photo,best-friend: (name,photo)) <friends total=“66” start=“0”> <friend uri=“urn:linkedin:v2:people/123” key=“123”> <name>Reid Hoffman</name> <photo>http://media.linkedin.com/photos/123.jpeg</photo> <best-friend uri=“urn:linkedin:v2:people/456” key=“456”> <name>Brandon Duncan</name> <photo>http://media.linkedin.com/photos/456.jpeg</photo> </best-friend> </friend> <friend>…</friend> </friends> …

/people[/id=456] /id /name /email /photo /best-friend /friends /123 /id /name /email /photo /best-friend /name /photo /jobs

Page 45: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

Domain Model as Foundation Fine-grained Request

Allows client to construct custom callsBetter than digging for the closest matching API: http://social...com/v1/user/123/profile http://social...com/v1/user/123/profile/usercard http://social...com/v1/user/123/profile/tinyusercardAllows optimization on the backend

Page 46: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

Domain Model as Foundation Fine-grained Request

Benefits - Provides a frame for both request and response semantics - Still allows for flexible syntax - Requests – path, query params, matrix params… - Responses – JSON, XML… - Helps to unify and automate many development tasks on both ends - Request / response creation, parsing, marshalling - Code (and documentation) generation - Discovery services

Page 47: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

Domain Model as Foundation Fine-grained Request

Benefits - Provides a frame for both request and response semantics - Still allows for flexible syntax - Requests – path, query params, matrix params… - Responses – JSON, XML… - Helps to unify and automate many development tasks on both ends - Request / response creation, parsing, marshalling - Code (and documentation) generation - Discovery services

Page 48: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

Examples of LinkedIn APIs HTTP GET - Read

…/people/[email protected]/friends?sort=name …/people/123/friends;sort=name:(name,jobs;sort=start-date) …/people:(id,name,photo)?name=page&company=google …/people::(123,456) …/people::(123,456):(name,photo)

Page 49: Phalcon 2 High Performance APIs - DevWeekPOA 2015

RESTful API

Incentive System

• Multiple ways to get at the same data• Partner can ask for exactly what they need• Associate cost with resources, system of accounting creates

incentives for partners • Throttling by resource rather than API

Page 50: Phalcon 2 High Performance APIs - DevWeekPOA 2015

Thank you!

[email protected] tfin

Email Social media

@jacksonfdam

@mafra.jackson

@jacksonfdam