Lesson 1: OData and REST
id.250123745.312456
Here’s an organized lesson plan on OData and REST, including 5 examples with explanations.
Lesson 1: OData and REST
Overview
OData (Open Data Protocol) is a standardized REST-based protocol for querying and manipulating data using CRUD operations (Create, Read, Update, Delete).
Key Concepts
RESTful principles apply to OData.
Supports query options like
$filter
,$select
,$orderby
.Data is exchanged in formats like JSON or XML.
Allows service metadata publishing for client discovery.
Example 1: Basic OData GET Request
Code Example (Python with requests
library):
import requests
BASE_URL = "https://services.odata.org/V4/TripPinServiceRW/People"
response = requests.get(BASE_URL)
if response.status_code == 200:
people = response.json()
for person in people['value']:
print(f"Name: {person['FirstName']} {person['LastName']}")
else:
print(f"Error: {response.status_code}")
Explanation:
Queries the
People
endpoint to fetch all records.JSON format is used to parse the response.
Displays the
FirstName
andLastName
of each person.Illustrates basic GET operation with OData.
Example 2: Filtering Data with $filter
Code Example:
import requests
BASE_URL = "https://services.odata.org/V4/TripPinServiceRW/People"
FILTER_QUERY = "?$filter=FirstName eq 'Russell'"
response = requests.get(BASE_URL + FILTER_QUERY)
if response.status_code == 200:
filtered_people = response.json()
for person in filtered_people['value']:
print(f"Name: {person['FirstName']} {person['LastName']}")
else:
print(f"Error: {response.status_code}")
Explanation:
$filter
is used to query only records withFirstName
equal toRussell
.Demonstrates advanced querying with OData parameters.
Emphasizes the flexibility of querying endpoints.
Response is dynamically filtered server-side.
Example 3: Selecting Specific Fields with $select
Code Example:
import requests
BASE_URL = "https://services.odata.org/V4/TripPinServiceRW/People"
SELECT_QUERY = "?$select=FirstName,LastName"
response = requests.get(BASE_URL + SELECT_QUERY)
if response.status_code == 200:
selected_fields = response.json()
for person in selected_fields['value']:
print(f"Name: {person['FirstName']} {person['LastName']}")
else:
print(f"Error: {response.status_code}")
Explanation:
$select
fetches only the specified fields (FirstName
andLastName
).Reduces payload size by omitting unnecessary data.
Improves performance for large datasets.
Commonly used when only specific details are needed.
Example 4: Sorting Results with $orderby
Code Example:
import requests
BASE_URL = "https://services.odata.org/V4/TripPinServiceRW/People"
ORDERBY_QUERY = "?$orderby=FirstName asc"
response = requests.get(BASE_URL + ORDERBY_QUERY)
if response.status_code == 200:
ordered_people = response.json()
for person in ordered_people['value']:
print(f"Name: {person['FirstName']} {person['LastName']}")
else:
print(f"Error: {response.status_code}")
Explanation:
$orderby
sorts results byFirstName
in ascending order.Supports both ascending (
asc
) and descending (desc
) orders.Enhances readability for client applications.
Useful for organizing data in UIs.
Example 5: Paging Data with $top
and $skip
Code Example:
import requests
BASE_URL = "https://services.odata.org/V4/TripPinServiceRW/People"
PAGING_QUERY = "?$top=5&$skip=10"
response = requests.get(BASE_URL + PAGING_QUERY)
if response.status_code == 200:
paged_people = response.json()
for person in paged_people['value']:
print(f"Name: {person['FirstName']} {person['LastName']}")
else:
print(f"Error: {response.status_code}")
Explanation:
$top
limits results to the first 5 records.$skip
skips the first 10 records in the dataset.Enables efficient paging for large datasets.
Essential for scalable applications with limited client memory.
Homework
Test all examples with your own OData endpoints.
Modify filters, selects, and paging to explore different results.