In addition to the built in support for core's data and actions the services module also provides a robust framework for exposing the data and actions of our custom modules as an API. In this lesson Joe writes a basic module which creates a custom resource for saving and retrieving a "checked in" status for an authenticated user.
Example commands:
Check a user's status
curl http://localhost/demos/services-7x-test/docroot/api/v1/drupalsquare/1 -H "Accept: application/json"
Check-in a user
curl http://localhost/demos/services-7x-test/docroot/api/v1/drupalsquare/checkin -X POST -H "Content-type: application/json" -H "Accept: application/json" -d '{"uid":1}'
Additional resources
An API that exposes data and actions to a 3rd party is only as good as the documentation for that API. Without good documentation no one will be able to make use of the API you just spent so much energy creating. In this lesson Joe shows some of his favorite API documentation examples and then walks through creating some basic documentation for the resource we created in the previous lesson.
Additional resources
Example - GitHub API Documentation
Example - Recurly Documentation
http://apiary.io - tool to assit in writing good documentation
This week, you'll take your HTTP response and request skills to the next level as you learn how to tackle common use cases such as filtering returned data to get only what you need and authenticating requests to get data behind a login. In this installment of our series, Building Web Services with Drupal 7, Joe Shindelar will walk you through how to query, test, and authenticate your D7-backed web services.
In this lesson Joe will demontrate using additional parameters and arguments to refine the returned data when making requests from our API. As well as walking through the code that shows where the various parameters are defined and how you can find what options are available. Finally, we install the services tools module which provides some additional documentation.
Additional resources
Services implements resources for all of core's basic data types but sometimes the information returned in the response is either to much or to little and we want to make changes to the response data. In this lesson Joe will look at how we can write a simple custom module that implments basic hooks provided by the services module to allow us to alter the response returned by a request to the node resource.
Most of the actions we want to perform and data we want to retrieve from an API is likely restricted to authenticated users. This lesson will outline the process of making an authenticated request and walk the user through the configuration that is required for our services endpoint in order to start allowing session based authentication.
In this lesson Joe walks through making authenticated requests to our API with cURL. Although cURL can be a bit verbose when making authenticated requests it serves as a good way to talk about all the headers that are a required and a lowest common denominator for how you could accomplish authentication in just about any language or application.
Example commands
curl http://services-demo.lan/api/v1/user/login -d '{"name":"admin","pass":"admin"}' -H "Content-type: application/json" -H "Accept: application/json"
curl http://services-demo.lan/api/v1/system/connect -X POST
curl http://services-demo.lan/api/v1/system/connect -H "Cookie: SESS60f8c5b86739b7e326223b4ef35867b2=A86XHGJWlnDcMOGcArbOT-qHrsIi5P2NrcoNTXwWluw" -H "X-CSRF-Token: T77haXwD7JKOJsBlKP3p3kLbjQO96bQWvGJAE1_PUZM" -X POST
curl http://services-demo.lan/api/v1/user/login -X POST -H "Content-type: application/json" -H "Accept: application/json"
In this lesson Joe walks through making authenticated requests using the Chrome REST Console Plugin
Additional resources
The Chrome plugin demonstrated in this video is no longer available but there are many alternatives available. Use Postman or search for "REST client" to find tooling options.
As mentioned in my previous blog post "Catching the Community Train" Lisa, Bojhan and myself will be working on a website to better facilitate the process of designers contributing to Drupal. Following my last blog post we sent out a questionnaire to current and previous contributors in order to gain some valuable insights that will help us move forward. In this blog post I will analyze the answers we received and share what they mean to me and how they will be instrumental in our success of helping designers more easily get involved in Drupal.
Drupalize.Me Podcast Episode 38
Blog postIn this week's Episode 38: Lessons from Distributed Companies, we talk to a few attendees of the Yonder conference about what it means to be a distributed compa
Are you ready to dive into creating RESTful Web Services? In this next set of videos on Building Web Services with Drupal 7, Joe Shindelar provides an overview of the Services UI in Drupal 7 and shows you how to set endpoints, enable resources, and test those resources using cURL on the command line and the Chrome REST plugin.
This lesson walks through defining the URL that will serve as the endpoint for your API as well as all the basic configuration for your new services endpoint with the exception of resource configuration which is covered in the following lesson.
Additional resources
The services module exposes most of Drupal core's data as resources and provides CRUD operations and additional actions for each. In this lesson Joe will look at the available resources, talk about the various CRUD operations and actions and then enable the node resource and demonstrate retrieving node data as JSON from the services endpoint.
Additional resources
JSON Formatter - Chrome plugin to display results of a JSON request in a nicer way.
While cURL may not be the simplest way to interact with a REST API it is the most ubiquitous and the one that is most often referenced in documentation through the web. So to ensure that students have at least a baseline of understanding, and will know how to read the documentation in the future, in this lesson Joe takes a quick look at how to test our API with cURL by retrieving, and creating data.
Example Commands:
`curl http://localhost/demos/services-7x/docroot/api/v1/node/1`
`curl --data '{"title":"hello world!","type":"page"}' --header "Content-Type:application/json" http://localhost/demos/services-7x/docroot/api/v1/node`
Additional resources
Instead of using the CLI to test our API we can use the powerful Chrome REST Console plugin instead. This provides a nice GUI and makes it a little easier to both test and understand what is going on. In this lesson Joe looks at installing the plugin and making basic requests to our API using the REST Console interface.
Additional resources
The Chrome plugin demonstrated in this video is no longer available but there are many alternatives available. Use Postman or search for "REST client" to find tooling options.
In Drupal 8, the entire block system got an overhaul, and there are lots of goodies in there. I took a tour of the new system with a short video. Some of the biggest changes to see are that you can now use blocks more than once on your site, and there is a new Custom block library that lets you create block types, which are very much like content types for blocks. They let you add fields to blocks, and give you more fine-grained control over how those fields are displayed depending on the location. Check out the video to see some of this in action.
This week, we're introducing a new series for you module developers out there: Building Web Services with Drupal 7. Why? Because you asked for it.
Web services, APIs, and structured data are all the rage right now, and with good reason. As more and more internet enabled devices start wanting to make use of the data in our websites, we need to give them some way to interact with that data that isn't point and click in a browser. Enter the Drupal 7 services module.
In this series Joe Shindelar will walk through the services module and teach you how to create a RESTful web services API that exposes Drupal's internal data like nodes and users as JSON or XML. This paves the way for a huge variety of clients using a number of programming languages to access the data contained within your Drupal site, from native mobile applications and partner websites to internet enabled refrigerators.
The first few lessons in the series are focused more on theory. They explain the various terms that are used both in Drupal and the wider web development sphere to describe web services and all their components. Joe describes REST itself and explains why it's a good fit, and discusses Drupal 7's content model and the ways that it lends itself nicely to serializing data into various formats via the services module in order to be consumed in structured and meaningful ways.
Throughout the series we'll see a couple of examples of how to first enable Drupal to return requested data elements as JSON and then subsequently how to properly format an HTTP request to retrieve those elements. Joe will demonstrate making requests to the API via both cURL and the Chrome REST Console. This demonstration will help students to learn about both the Drupal configuration that is required and the way in which any third party applications can request data from Drupal via concrete examples.
With the basics of creating an endpoint and making simple requests out of the way, Joe will demonstrate how to enable authentication via the services module and then use both cURL and the REST Console to explain the authentication handshake: a somewhat complex exchange of a username and password for a session authentication token that takes place in HTTP requests and headers whenever you want to perform an action via the API that requires an authenticated user. This will enable to you create third party applications that can access Drupal as a specific user, allowing for greater personalization.
After that, Joe looks at ways to integrate the lists that site administrators create using the views module with services in order to output their contents as structured data, as well as ways to use views to create entirely new custom services resources, followed by an example of leveraging the power of views exposed filters via services.
Finally, for those scenarios where you simply can't point and click your way to an answer, Joe will teach you how to implement your own custom services resources for both saving and retrieving data from within Drupal, demonstrating the basic knowledge required to allow you to use services to solve all of your own custom API needs.
Things you'll be able to do after completion of this series:
- Understand the basic principles of both web services and RESTful APIs.
- Explain why Drupal is a good fit for web applications that want to allow third party clients to access their data.
- Create a RESTful web services API using the services module for Drupal 7.
- Retrieve data from your Drupal site in both JSON and XML format.
- Create new nodes in Drupal via web services.
- Make authenticated requests to Drupal via web serivces.
- Integrate the views module with services to retrieve views data in various formats.
- Write your own module providing custom services data and actions.
- Create meaningful documentation that will enable others to take advantage of your newly created service.
This series provides information about the services module for both intermediate and advanced Drupal users. Since this module makes use of basic Drupal site building skills and custom views, users should be familiar with those topics. Also, while not required, knowledge of running commands via the command line will be helpful. Finally, if you want to write your own custom services resources you'll need to understand basic module development.
Additional resources
This lesson covers what an API is and why you might want to build one. Joe also explains the basic concepts behind any API and provides real world examples of existing APIs that you're probably familiar with as well as use cases for when and why you might want to build your own.
In this lesson Joe shows you where to locate the services module and it's documentation. Then walks through installing the services module and confirming that it's working. Followed by a quick overview of the module's codebase and a general overview of what the services module provides.