Building a Rest API app
Mumble
Table of contents
Introduction
This tutorial provides step by step instructions about transforming a sample Rest API “ToDo”-app on the Xamarin Documentation website into a real app communicating with a Drupal 7 back-end. The source code for the sample app is downloadable from GitHub.
Here are the links to the documentation and the source code:
- Data & Cloud Sevices
- Understanding the sample
- Consuming Web Services
- Consuming a RESTful Web Service
- Developer.Xamarin (source code for just this project)
- GitHub (you can only download all the example source code in a huge file).
- GitHub (browse source).
The sources hosted on Developer.Xamarin and those hosted on GitHub are slightly different:
- Developer.Xamarin: TodoREST.snl specify VS 14. GitHub: specify VS 15.
- Developer.Xamarin: Supports speech.
- Developer.Xamarin: Dummy ToDos work.
- Developer.Xamarin: All buttons work (will respond by saying things cannot be saved if you try to save).
- Developer.Xamarin: Subfolder named
TodoRESTService
. GitHub has one namedTodoAPI
. - Developer.Xamarin: Also UWP project.
- There are lots of “
if DEBUG
” in both (but different ones). - The
README.md
differs. (See below.)
From the Developer.Xamarin README.md
:
This sample demonstrates a Todo list application where the data is stored and accessed from a RESTful web service. The web service is hosted by Xamarin and provides read-only access to the data. Therefore, the operations that create, update, and delete data will not alter the data consumed in the application. However, a hostable version of the REST service that provides read-write access to the data is stored in the
TodoRESTService
folder.
From the GitHub README.md
:
This sample demonstrates a Todo list application where the data is stored and accessed from a RESTful web service. The web service code is in the
TodoAPI
project.
Differences that don't matter:
- Developer.Xamarin: Metadata.xml has DOS-style line endings.
Debug existing Xamarin project
- Download the source code from GitHub and open the solution in Visual Studio.
- Make sure it is the startup project. Startup project are shown using a bold font in Solution Explorer.
- Install missing SDK versions. The error message will tell what level API is missing.
- Delete the iOS and UWP projects.
- Rename the “TodoREST.Droid” to “TodoREST.Android”.
The RestOrl
is defined in test.dev/TodoREST/Constants.cs
:
namespace TodoREST { public static class Constants { // URL of REST service public static string RestUrl = "https://developer.xamarin.com:8081/api/todoitems/{0}"; // Credentials that are hard coded into the REST service public static string Username = "Xamarin"; public static string Password = "Pa$$w0rd"; } }
Using RESTED to examine endpoint of Xamarin Todo app with Basic auth:
Or retrieved with cURL:
$ curl -u 'username:password' https://developer.xamarin.com:8081/api/todoitems/ [ { "ID":"6bb8a868-dba1-4f1a-93b7-24ebce87e243", "Name":"Learn app development", "Notes":"Attend Xamarin University", "Done":true }, { "ID":"b94afb54-a1cb-4313-8af3-b7511551b33b", "Name":"Develop apps", "Notes":"Use Xamarin Studio/Visual Studio", "Done":false }, { "ID":"ecfa6f80-3671-4911-aabe-63cc442c1ecf", "Name":"Publish apps", "Notes":"All app stores", "Done":false } ]
UUIDs are used for the IDs.
Consuming the Web Service
The REST service is written using ASP.NET Core and provides the following operations:
Operation | HTTP method | Relative URI | Parameters |
---|---|---|---|
Get a list of to-do items | GET | /api/todoitems/ | - |
Create a new to-do item | POST | /api/todoitems/ | A JSON formatted TodoItem |
Update a to-do item | PUT | /api/todoitems/ | A JSON formatted TodoItem |
Delete a to-do item | DELETE | /api/todoitems/{id} | - |
There are no request to get a single task. All the data for all the
tasks are returned by the JSON returned by the GET
request.
The CREATE
and UPDATE
requests comes with
a parameter.
Some of the URIs include the object ID in the path. For example, to
delete the TodoItem
whose ID is
6bb8a868-dba1-4f1a-93b7-24ebce87e243
, the client sends a
DELETE
request to:
http://hostname/api/todoitems/6bb8a868-dba1-4f1a-93b7-24ebce87e243
When the Web API framework receives a request it routes the request to an action. These actions are simply public methods in the TodoItemsController
class. The framework uses a routing table to determine which action to invoke in response to a request, which is shown in the following code example:
config.Routes.MapHttpRoute( name: "TodoItemsApi", routeTemplate: "api/{controller}/{id}", defaults: new { controller="todoitems", id = RouteParameter.Optional } );
A successful request with no content gets HTTP status code 204 in return.
The routing table contains a route template, and when the Web API framework receives an HTTP request, it tries to match the URI against the route template in the routing table. If a matching route cannot be found the client receives a 404 (not found) error. If a matching route is found, Web API selects the controller and the action as follows:
- To find the controller, Web API adds "controller" to the value of the {controller} variable.
- To find the action, Web API looks at the HTTP method and looks at controller actions that are decorated with the same HTTP method as an attribute.
- The {id} placeholder variable is mapped to an action parameter.
Final word
[TBA]
Last update: 2019-04-07 [gh].