2. Objectives
- Understand what is a Web service
- Understand what is a REST API and how to use in Xamarin
- Consume a real API in Xamarin
3. Web Service
A web service is a collection of open protocols and standards used for exchanging
data between applications or systems.
For transferring data it uses:
- XML
- JSON
SOAP RESTFUL
TYPES
4. API (Application Programming Interface)
Is a set of defined functions and methods for interfacing with the underlying
operating system or another program or service running on the computer.
6. What are REST services?
REST (Representational State Transfer) is an architecture for creating distributed
applications which is modeled around the HTTP specification
7. Definition
URL HTTP methods
A media type for the
data
https://www.some_address.com/customers GET/POST/PUT/PATCH/DELETE JSON/ XML
9. Rest operations: GET
This operation is used to retrieve data from the web service.
https://www.some_address.com/users/
https://www.some_address.com/users/5 https://www.some_address.com/users?id=5
10. Rest operations: POST
This operation is used to create a new item of data on the web service.
https://www.some_address.com/users/5
11. Rest operations: PUT
This operation is used to update an item of data on the web service.
https://www.some_address.com/users/5
12. Rest operations: PATCH
This operation is used to update an item of data on the web service by describing
a set of instructions about how the item should be modified.
https://www.some_address.com/users/5
13. Rest operations: DELETE
This operation is used to delete an item of data on the web service.
https://www.some_address.com/users/5
15. JSON
JSON (JavaScript Object Notation) is a
lightweight format that is used for data
interchanging. It is based on a subset of
JavaScript language (the way objects are
built in JavaScript).
16. HttpStatus
The REST service sends an HTTP status code in the
HttpResponseMessage.StatusCode property, to indicate whether the HTTP
request succeeded or failed. The common responses for this operation are:
201 (CREATED)
400 (BAD
REQUEST)
500 (SERVER
ERROR)
404 (NOT FOUND)
18. Security in REST
- Security is ultimately decided by the service
– the client can only conform to what the
service allows
- Should always prefer https to protect the
data peer-to-peer
20. Xamarin API Options
HttpClient ServiceStack RestSharp Platform Specific
Most common
approach, built into
.NET
Full fledged 3rd party
web services
framework, has client
PCL for consuming
REST services
Full 3rd party client
library that supports
file downloads,
authentication,
chunking, etc
Native Platform Apis
for networking calls
21. HttpClient
The HttpClient class is used to send and receive requests over HTTP. It
provides functionality for sending HTTP requests and receiving HTTP
responses from a URI identified resource. Each request is sent as an
asynchronous operation.
22. HttpClient - Get
JsonConvert is a Json.NET class that can serialize and
deserialize data from a JSON string or stream based on a
specified Type
26. Let’s do it step by step
1-Create an Interface IApiService for your call definitions
2-Create a class ApiService to handle your requests and implement the
previously created interface
3-Implement your request calls in your Api Service and define them in your
Interface
4-Do your calls in your ViewModel
5-Show the data in your views
29. IOS - NSAppTransportSecurity
App Transport Security (ATS) enforces
secure connections between internet
resources (such as the app's back-end
server) and your app.
32. Refit
Refit is a library heavily inspired by Square's Retrofit library, and it turns
your REST API into a live interface
https://github.com/paulcbetts/refit
33. Helpful tools for Json and Request Handlers
- http://json2csharp.com/
- https://www.getpostman.com/docs/postman/launching_postman/navi
gating_postman
35. Recommended Lectures
- Web Service
https://developer.xamarin.com/guides/xamarin-forms/cloud-services/consuming/
- HttpClient in Xamarin Forms
http://blog.xhackers.co/httpclient-with-xamarin-forms/
https://www.youtube.com/watch?v=xNP-K37mssA
36. By Rendy Del Rosario and Charlin Agramonte
Week 5
THANK YOU!!!