Hey there, fellow developer! Ready to dive into the world of Adobe Experience Manager (AEM) API integration? You're in for a treat. AEM is a powerhouse for content management, and its API opens up a whole new realm of possibilities. In this guide, we'll walk through the process of building an AEM API integration using Python. Buckle up!
Before we jump in, make sure you've got these basics covered:
requests
library installed (pip install requests
)Got all that? Great! Let's roll.
First things first, we need to get our API credentials. Head over to your AEM instance and grab your API key and secret. Once you've got those, let's set up authentication:
import requests base_url = "https://your-aem-instance.com" api_key = "your_api_key" api_secret = "your_api_secret" auth = (api_key, api_secret)
Now, let's establish a connection to the AEM API:
def make_request(endpoint, method="GET", data=None): url = f"{base_url}{endpoint}" response = requests.request(method, url, auth=auth, json=data) response.raise_for_status() return response.json() # Test the connection try: result = make_request("/api/assets.json") print("Connection successful!") except requests.exceptions.RequestException as e: print(f"Oops! Something went wrong: {e}")
Let's cover the basic CRUD operations:
# GET request def get_content(path): return make_request(f"/api/assets{path}.json") # POST request def create_content(path, data): return make_request(f"/api/assets{path}", method="POST", data=data) # PUT request def update_content(path, data): return make_request(f"/api/assets{path}", method="PUT", data=data) # DELETE request def delete_content(path): return make_request(f"/api/assets{path}", method="DELETE")
Time to play with some assets:
# Upload an asset def upload_asset(file_path, destination_path): with open(file_path, 'rb') as file: files = {'file': file} response = requests.post(f"{base_url}/api/assets{destination_path}", auth=auth, files=files) return response.json() # Get asset metadata def get_asset_metadata(asset_path): return make_request(f"/api/assets{asset_path}.json") # Update asset properties def update_asset_properties(asset_path, properties): return make_request(f"/api/assets{asset_path}", method="PUT", data=properties)
Let's manage some content:
# Create a page def create_page(parent_path, page_name, template): data = { "jcr:primaryType": "cq:Page", "jcr:content": { "jcr:primaryType": "cq:PageContent", "sling:resourceType": "foundation/components/page", "title": page_name, "cq:template": template } } return make_request(f"{parent_path}/{page_name}", method="POST", data=data) # Modify page content def update_page_content(page_path, content): return make_request(f"{page_path}/jcr:content", method="POST", data=content)
Need to find something specific? Use the QueryBuilder API:
def query_content(query_params): endpoint = "/bin/querybuilder.json" response = requests.get(f"{base_url}{endpoint}", auth=auth, params=query_params) return response.json() # Example: Find all pages under a certain path query = { "path": "/content/mysite", "type": "cq:Page", "p.limit": "-1" } results = query_content(query)
Always handle your errors gracefully:
try: result = make_request("/api/assets/nonexistent.json") except requests.exceptions.HTTPError as e: if e.response.status_code == 404: print("Asset not found!") else: print(f"An error occurred: {e}")
And remember, be kind to the API. Implement rate limiting and optimize your requests to avoid overwhelming the server.
There you have it! You're now equipped with the basics of AEM API integration using Python. Remember, this is just the tip of the iceberg. There's so much more you can do with the AEM API, from batch operations to setting up webhooks for real-time updates.
Keep exploring, keep coding, and most importantly, have fun! The world of content management is your oyster with AEM and Python at your fingertips.
Happy coding!
For complete code examples and more advanced use cases, check out our GitHub repository: AEM Python Integration Examples
(Note: This is a placeholder link. Replace with your actual repository if available.)