austin day of rest - introduction

27
Introduction to the WordPress REST API Nick Batik

Upload: handsonwpcom

Post on 18-Jan-2017

67 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Austin Day of Rest - Introduction

Introduction to the WordPress REST API Nick Batik

Page 2: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

What Is an API?

Page 3: Austin Day of Rest - Introduction

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.

Page 4: Austin Day of Rest - Introduction

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?

Page 5: Austin Day of Rest - Introduction

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

Page 6: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

What is REST?• Uniform Interface

• Resources

• Representations

• Hypermedia (Links)

• Metadata

Page 7: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

What is REST?• Stateless

• Cacheable

• Client-Server

• Layered System

• Code on Demand (optional)

Page 8: Austin Day of Rest - Introduction

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.

Page 9: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

What is a REST API?

Databases have 4 primary function - CRUD:

• Create

• Read

• Update

• Delete

Page 10: Austin Day of Rest - Introduction

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

Page 11: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

What is a REST API?

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

Page 12: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

Acronym, Initialization, and Other Jargon•HTTP Verbs

•HTTP Methods

•URLS, routes and Endpoints

•Representations

•Response Codes

•JSON

Page 13: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

HTTP Verbs•GET

•PUT

•DELETE

•POST

• HEAD

•OPTIONS

Page 14: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

HTTP Methods•Safe methods

•Unsafe methods

•Idempotent methods

Page 15: Austin Day of Rest - Introduction

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”

Page 16: Austin Day of Rest - Introduction

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

Page 17: Austin Day of Rest - Introduction

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

Page 18: Austin Day of Rest - Introduction

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

Page 19: Austin Day of Rest - Introduction

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

Page 20: Austin Day of Rest - Introduction

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

Page 21: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

JSON - Example

{"employees":[

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

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

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

]}

Page 22: Austin Day of Rest - Introduction

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()"} ] }}}

Page 23: Austin Day of Rest - Introduction

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

Page 24: Austin Day of Rest - Introduction

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/

Page 25: Austin Day of Rest - Introduction

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

Page 26: Austin Day of Rest - Introduction

Nick Batik PleiadesServices.com

@nick_batik

The WordPress Implementation of REST API

Access and Authentication

•cookie authentication

•OAuth authentication

•basic authentication

Page 27: Austin Day of Rest - Introduction

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