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

  1. RESTful principles apply to OData.

  2. Supports query options like $filter, $select, $orderby.

  3. Data is exchanged in formats like JSON or XML.

  4. 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:

  1. Queries the People endpoint to fetch all records.

  2. JSON format is used to parse the response.

  3. Displays the FirstName and LastName of each person.

  4. 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:

  1. $filter is used to query only records with FirstName equal to Russell.

  2. Demonstrates advanced querying with OData parameters.

  3. Emphasizes the flexibility of querying endpoints.

  4. 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:

  1. $select fetches only the specified fields (FirstName and LastName).

  2. Reduces payload size by omitting unnecessary data.

  3. Improves performance for large datasets.

  4. 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:

  1. $orderby sorts results by FirstName in ascending order.

  2. Supports both ascending (asc) and descending (desc) orders.

  3. Enhances readability for client applications.

  4. 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:

  1. $top limits results to the first 5 records.

  2. $skip skips the first 10 records in the dataset.

  3. Enables efficient paging for large datasets.

  4. Essential for scalable applications with limited client memory.


Homework

  1. Test all examples with your own OData endpoints.

  2. Modify filters, selects, and paging to explore different results.