austin day of rest - introduction

Post on 18-Jan-2017

67 Views

Category:

Internet

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to the WordPress REST API Nick Batik

Nick Batik PleiadesServices.com

@nick_batik

What Is an API?

Nick Batik PleiadesServices.com

@nick_batik

What Is an API?• API is short for Application Program Interface.

• An agreed set of standardized ways that a particular software program can be used;

• The rules define how one program can talk to a another, and how it will respond.

Nick Batik PleiadesServices.com

@nick_batik

What Is an API?

ApplicationAPI

I WantInformati

onFrom You

What info I want

The app’s info

I HaveInformati

onFor You

What info the app needs

Did the app accept it?

Nick Batik PleiadesServices.com

@nick_batik

Classic Problems of APIs• How do you know the rules?

• The content is different for each rule

• You can’t change the rules

Nick Batik PleiadesServices.com

@nick_batik

What is REST?• Uniform Interface

• Resources

• Representations

• Hypermedia (Links)

• Metadata

Nick Batik PleiadesServices.com

@nick_batik

What is REST?• Stateless

• Cacheable

• Client-Server

• Layered System

• Code on Demand (optional)

Nick Batik PleiadesServices.com

@nick_batik

What is a REST API?

A REST API defines a set of functions developers can use to perform requests and receive responses via HTTP protocol such as GET and POST.

Nick Batik PleiadesServices.com

@nick_batik

What is a REST API?

Databases have 4 primary function - CRUD:

• Create

• Read

• Update

• Delete

Nick Batik PleiadesServices.com

@nick_batik

What is a REST API?

The REST API implements the 4 main database functions in through the HTTP protocol:

• Create = PUT with a new URI

•          POST to a base URI returning a newly created URI

• Read   = GET

• Update = PUT with an existing URI

• Delete = DELETE

Nick Batik PleiadesServices.com

@nick_batik

What is a REST API?

REST API turns the internet into the world’s largest database

Nick Batik PleiadesServices.com

@nick_batik

Acronym, Initialization, and Other Jargon•HTTP Verbs

•HTTP Methods

•URLS, routes and Endpoints

•Representations

•Response Codes

•JSON

Nick Batik PleiadesServices.com

@nick_batik

HTTP Verbs•GET

•PUT

•DELETE

•POST

• HEAD

•OPTIONS

Nick Batik PleiadesServices.com

@nick_batik

HTTP Methods•Safe methods

•Unsafe methods

•Idempotent methods

Nick Batik PleiadesServices.com

@nick_batik

URLs, routes and Endpoints•Routes are URLs

•Endpoints - actions taken (HTTP verb) on the URL

e.g. GET http://example.com/wp-json/wp/v2/posts/123POST http://example.com/wp-json/wp/v2/posts/123

•“GET” and “POST” are endpoints

•“wp/v2/posts/123” is the route

•/wp-json/ is the API “base”

Nick Batik PleiadesServices.com

@nick_batik

URLs, routes and Endpoints

You can expose services in different endpoints:

http://www.example.com/soaphttp://www.example.com/jsonhttp://www.example.com/xml

Nick Batik PleiadesServices.com

@nick_batik

Representations•The HTTP client and HTTP server exchange information about resources identified by URLs

•Both request and response contain a representation of the resource

•The header and the body are part of the representation

•HTTP headers contain metadata, defined by the HTTP spec

Nick Batik PleiadesServices.com

@nick_batik

Representations•The HTTP response should specify the content type of the body, example possibilities include:

•HTML

•XML

•XHTML

•SOAP+XM

•JSON

Nick Batik PleiadesServices.com

@nick_batik

Response Codes

•405 Method Not Allowed

•409 Conflict

•410 Gone

•500 Internal Server Error

•501 Not Implemented

•200 OK

•201 Created

•400 Bad Request

•401 Unauthorized

•404 Not Found

Nick Batik PleiadesServices.com

@nick_batik

JSON•JSON: JavaScript Object Notation.

•JSON is a syntax for storing and exchanging data.

•Human readable / writable

•Machine parse-able

Nick Batik PleiadesServices.com

@nick_batik

JSON - Example

{"employees":[

{"firstName":"John", "lastName":"Doe"},

{"firstName":"Anna", "lastName":"Smith"},

{"firstName":"Peter", "lastName":"Jones"}

]}

Nick Batik PleiadesServices.com

@nick_batik

JSON - Example

{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] }}}

Nick Batik PleiadesServices.com

@nick_batik

The WordPress Implementation of REST API• WordPress as a Data Store

• WordPress as an Editing Interface

• Access and Authentication

Nick Batik PleiadesServices.com

@nick_batik

The WordPress Implementation of REST API

WordPress as a Data Store

https://css-tricks.com/thoughts-on-an-api-first-wordpress/

Nick Batik PleiadesServices.com

@nick_batik

The WordPress Implementation of REST APIWordPress as an Editing Interface

•WYSIWYG / Text Editor

•Media Library

•Plugin Functionality

•Database Customization

•Custom Interfaces such as Calypso

Nick Batik PleiadesServices.com

@nick_batik

The WordPress Implementation of REST API

Access and Authentication

•cookie authentication

•OAuth authentication

•basic authentication

Nick Batik PleiadesServices.com

@nick_batik

WordPress REST API – Conclusion•Open WordPress to new front-end platforms

•Heralds a new era in content exchange

•Introduces new levels of complexity

top related