Explore practical sample projects for building robust serverless APIs on Azure.
A comprehensive example demonstrating how to build a RESTful API for processing customer orders using Azure Functions, Cosmos DB for data storage, and Azure API Management for security and throttling.
# Sample Azure Function (HTTP Trigger) snippet
import logging
import azure.functions as func
import json
async def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
order_id = req.route_params.get('orderId')
req_body = req.get_json() if req.get_body() else None
if order_id:
# Logic to retrieve or process order by order_id
response_message = f"Processing order with ID: {order_id}"
elif req_body and 'items' in req_body:
# Logic to create a new order
response_message = "New order received and will be processed."
else:
response_message = "Please provide an order ID or order details in the request body."
return func.HttpResponse(
json.dumps({"message": response_message}),
mimetype="application/json",
status_code=200
)
Demonstrates creating a serverless API endpoint to ingest real-time healthcare data into Azure API for FHIR, enabling compliance and interoperability.
# Sample Python script for FHIR data validation
from fhirclient.models import patient, observation, bundle
def validate_fhir_resource(resource_data):
# Placeholder for actual FHIR validation logic
# using fhirclient or similar libraries
if resource_data.get("resourceType") == "Patient":
try:
p = patient.Patient(resource_data)
p.validate()
return True, "Patient resource is valid."
except Exception as e:
return False, f"Patient validation failed: {e}"
else:
return False, "Unsupported resource type for this sample validation."
# Example usage:
# resource = {"resourceType": "Patient", "id": "example", "name": [{"family": "Smith", "given": ["John"]}]}
# is_valid, message = validate_fhir_resource(resource)
# print(f"Validation: {is_valid}, Message: {message}")
Build a scalable product catalog API using Azure App Service for hosting and Azure SQL Database for persistent storage. Includes examples for CRUD operations.
// Sample C# Controller action for GET /products
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly ProductCatalogDbContext _context;
public ProductsController(ProductCatalogDbContext context)
{
_context = context;
}
// GET: api/Products
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
{
return await _context.Products.ToListAsync();
}
// ... other CRUD operations
}