WADLLib

2.0.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a WADL document from a string, navigate its resources and methods, and inspect their properties using the `wadllib.application.Application` object. It illustrates how to programmatically explore an API's structure as defined by WADL.

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}")

view raw JSON →