Successfully reported this slideshow.
Your SlideShare is downloading. ×

Austin Day of Rest - Introduction

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 27 Ad

Austin Day of Rest - Introduction

Download to read offline

Nick Batik introduces the concepts of the JASON REST APIs by explaining the logic behind APIs, and walking through a few practical uses for both personal and client websites. This is an informal discussion of the vocabulary and concepts to introduce the REST API to those who unfamiliar with the topic to help them prepare for a more technical understanding of the subject in order to take advantage of the possibilities.

Nick Batik introduces the concepts of the JASON REST APIs by explaining the logic behind APIs, and walking through a few practical uses for both personal and client websites. This is an informal discussion of the vocabulary and concepts to introduce the REST API to those who unfamiliar with the topic to help them prepare for a more technical understanding of the subject in order to take advantage of the possibilities.

Advertisement
Advertisement

More Related Content

Advertisement
Advertisement

Austin Day of Rest - Introduction

  1. 1. Introduction to the WordPress REST API Nick Batik
  2. 2. Nick Batik PleiadesServices.com @nick_batik What Is an API?
  3. 3. 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.
  4. 4. Nick Batik PleiadesServices.com @nick_batik What Is an API? ApplicationAPI I Want Informatio n From You What info I want The app’s info I Have Informatio n For You What info the app needs Did the app accept it?
  5. 5. 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
  6. 6. Nick Batik PleiadesServices.com @nick_batik What is REST? • Uniform Interface • Resources • Representations • Hypermedia (Links) • Metadata
  7. 7. Nick Batik PleiadesServices.com @nick_batik What is REST? • Stateless • Cacheable • Client-Server • Layered System • Code on Demand (optional)
  8. 8. 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.
  9. 9. Nick Batik PleiadesServices.com @nick_batik What is a REST API? Databases have 4 primary function - CRUD: • Create • Read • Update • Delete
  10. 10. 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
  11. 11. Nick Batik PleiadesServices.com @nick_batik What is a REST API? REST API turns the internet into the world’s largest database
  12. 12. Nick Batik PleiadesServices.com @nick_batik Acronym, Initialization, and Other Jargon • HTTP Verbs • HTTP Methods • URLS, routes and Endpoints • Representations • Response Codes • JSON
  13. 13. Nick Batik PleiadesServices.com @nick_batik HTTP Verbs • GET • PUT • DELETE •POST • HEAD • OPTIONS
  14. 14. Nick Batik PleiadesServices.com @nick_batik HTTP Methods • Safe methods • Unsafe methods • Idempotent methods
  15. 15. 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/123 POST 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”
  16. 16. Nick Batik PleiadesServices.com @nick_batik URLs, routes and Endpoints You can expose services in different endpoints: http://www.example.com/soap http://www.example.com/json http://www.example.com/xml
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. Nick Batik PleiadesServices.com @nick_batik JSON - Example {"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]}
  22. 22. 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()"} ] } }}
  23. 23. 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
  24. 24. 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/
  25. 25. Nick Batik PleiadesServices.com @nick_batik The WordPress Implementation of REST API WordPress as an Editing Interface • WYSIWYG / Text Editor • Media Library • Plugin Functionality • Database Customization • Custom Interfaces such as Calypso
  26. 26. Nick Batik PleiadesServices.com @nick_batik The WordPress Implementation of REST API Access and Authentication • cookie authentication • OAuth authentication •basic authentication
  27. 27. 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

Editor's Notes

  • Uniform interface: Components throughout the network share a uniform interface
    Resources: There are an infinite number of resources,
    all of which have unique addresses and share a common interface for manipulation
    Representations: A resource may be represented by multiple media types, and the representation is the primary vehicle for a resource’s transfer of state
    Hypermedia (Links): Resources should be linked to other resources
    Metadata: Metadata comes in the form of name-value pairs that can describe both the representation and the resource data returned in the response - rel=“alternate" type="text/html" hreflang="en" href="rest.en.html"
  • Stateless: Each request contains all of the information necessary to be understood and processed;
    no shared context exists between the client and server (i.e. cookies, sessions)
    Cache: Clients may reuse data that is labeled as cacheable, improving network efficiency
    Client-server: Provides full separation of concerns - assumes a disconnected system
    Layered: All components in the network can understand all requests as they travel from client to server but they cannot see beyond the current layer; provides scalability and efficiency of requests
    Don’t know who I’m talking to
    Code-on-demand: Extends the functionality of the client with client-side scripts, etc.
  • GET is the one that browsers use each time you click a link or type a URL into the address bar. It instructs the server to transmit the data identified by the URL to the client.
    PUT request is used when you wish to create or update the resource identified by the URL.
    REST is backend agnostic; nothing informs the server how the data should be created - just that it should.
    PUT requests contain the data to use in updating or creating the resource in the body.
    DELETE is be used when you want to delete the resource identified by the URL of the request.
    POST is used when the processing you wish to happen on the server should be repeated (idempotent)
    should cause processing of the request body as a subordinate of the URL you are posting to.
    HEAD: Used to check if a resource exists without returning its representation
    OPTIONS: Used to retrieve all the verbs supported by a resource
  • Safe methods are those that never modify resources.
    The only safe methods, from the four listed is GET.
    The others are unsafe, because they may result in a modification of the resources.

    The only non idempotent method is POST
    All other use exact URLs
  • By representation, we mean information, in a certain format, about the state of the resource or how that state should be in the future.

    The body can contain data in any format
    you can send plain text, pictures, HTML, and XML in any human language.
    Through request metadata or different URLs, you can choose between different representations for the same resource.
  • By representation, we mean information, in a certain format, about the state of the resource or how that state should be in the future.

    The body can contain data in any format
    you can send plain text, pictures, HTML, and XML in any human language.
    Through request metadata or different URLs, you can choose between different representations for the same resource.
  • 200 OK - This response code indicates that the request was successful.
    201 Created - The request was successful and a resource was created. Confirms success of a PUT or POST request.
    400 Bad Request - malformed request. POST / PUT request data does not pass validation, or is wrong format.
    401 Unauthorized - This error indicates that you need to perform authentication before accessing the resource.
    405 Method Not Allowed - The HTTP method used is not supported for this resource.
    409 Conflict - For instance, you are using a PUT request to create the same resource twice.
    410 - Gone: Means that a resource has been moved to another location. An example might be trying to delete an already deleted resource that has been moved to trash.
    500 Internal Server Error - When all else fails; generally, a 500 response is used when processing fails due to unanticipated circumstances on the server side, which causes the server to error out.
    501 - Not Implemented: Means that the server doesn’t support the functionality to complete the request. Usually occurs when a server receives a request method that it doesn’t recognize.

  • ◦ WordPress as an Editing Interface
    ◦ Access and Authentication

  • There are people using WordPress just for the Media Library

  • Basic authentication requires passing your username and password on every request, as well as giving your credentials to clients
    Application passwords - instead of providing your normal account password, unique and easily revokable passwords are generated from your edit profile screen in the WordPress admin.
    These application passwords are valid exclusively for the REST API - may not be used to log in to WordPress

×