WADLLib
wadllib is a Python library designed to help navigate HTTP resources by parsing and interpreting Web Application Description Language (WADL) files. It allows developers to programmatically explore API structures, resources, methods, and parameters defined in a WADL document, providing a machine-readable guide to RESTful services. The current version is 2.0.0, released in January 2024, with active maintenance targeting Python 3.8+.
Common errors
-
ImportError: cannot import name 'Application' from 'wadl' (/path/to/wadl/__init__.py)
cause Attempting to import `Application` using the old import path from `wadllib` 1.x after upgrading to 2.x.fixChange the import statement to `from wadllib.application import Application`. -
SyntaxError: invalid syntax (on a line of the library code itself)
cause Running `wadllib` 2.0.0+ with an older Python version (e.g., Python 3.7 or earlier), which does not meet the `requires_python >=3.8` requirement.fixUpgrade your Python environment to version 3.8 or newer to match the library's requirements. Alternatively, downgrade `wadllib` to a 1.x version if compatible with your Python version. -
wadllib.exceptions.WADLError: Root element must be 'application'
cause The provided XML string or file is not a valid WADL document, or its root element is not `<application>`.fixEnsure the XML document you are passing to `Application()` or `WADLFile()` is a well-formed WADL document, starting with the `<application>` root element and conforming to the WADL schema.
Warnings
- breaking The import paths for core classes like `Application` and `WADLFile` changed significantly in version 2.0.0. Code written for `wadllib` 1.x will fail with `ImportError`.
- breaking Version 2.0.0 of `wadllib` dropped support for older Python versions, now requiring Python 3.8 or newer. Running on unsupported Python environments will lead to `SyntaxError` or runtime issues.
- gotcha wadllib strictly adheres to the WADL specification (2009/02). Malformed or non-compliant WADL files, even if partially readable by humans, may lead to parsing errors (`WADLError`) or unexpected behavior.
Install
-
pip install wadllib
Imports
- Application
from wadl import Application
from wadllib.application import Application
- WADLFile
from wadllib.wadl import WADLFile
Quickstart
from wadllib.application import Application
from io import BytesIO
# A simple example WADL document as a string
wadl_xml = """
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://wadl.dev.java.net/2009/02">
<resources base="http://example.com/api">
<resource path="users">
<method name="GET">
<request/>
<response>
<representation mediaType="application/json"/>
</response>
</method>
<method name="POST">
<request>
<representation mediaType="application/json"/>
</request>
<response>
<representation mediaType="application/json"/>
</response>
</method>
</resource>
<resource path="products/{id}">
<param name="id" type="xsd:int" style="template" required="true"/>
<method name="GET">
<request/>
<response>
<representation mediaType="application/xml"/>
</response>
</method>
</resource>
</resources>
</application>
"""
# Load the WADL from a string (encoded to bytes)
app = Application(wadl_xml.encode('utf-8'))
print(f"API Base URL: {app.resources.base}\n")
print("Available Resources:")
for uri in app.resources:
resource = app.resources[uri]
print(f"- Path: {resource.path} (Full URI: {uri})")
for method in resource.methods:
print(f" - Method: {method.name}")
for response in method.responses:
for rep in response.representations:
print(f" - Responds with: {rep.mediaType}")
# Access a specific resource and its method
users_resource = app.resources.get('http://example.com/api/users')
if users_resource:
get_users_method = users_resource.method_by_name('GET')
if get_users_method:
print(f"\nGET /users method details:")
print(f" HTTP Verb: {get_users_method.name}")
print(f" Request params: {get_users_method.request.params}")