Python client library
Python client library
This is a Python implementation of a client library for RESTx.
Auto generated API documentation
Detailed documentation of the API is available through Python's help() function (for each object) as well as the library's epydoc documentation.
Examples
Function and API of the RESTx Python client library are explained via usage examples.
Example 1: Using a simple, already existing resource
import restxclient.api as restxapi # The complete client API # Establish connection to server server = restxapi.RestxServer(“http://localhost:8001”) # Access the 'status' service (sub-resource) of a Twitter resource service = server.get_resource("MyTwitter").get_service("status") status, data = service.access() # Return HTTP status and data
Example 2: Using a resource that requires input data
# Update the Twitter status
service = server.get_resource("MyTwitter").get_service("status")
service.input = “Updating my status with RESTx”
status, data = service.access()
Example 3: Using a resource that requires parameters
# Conducting a Google search service = server.get_resource("MyGoogle").get_service("search") service.set(“query”, “mulesoft restx”) # Or: service.params = { ... } status, data = service.access() # Data is a dictionary with results
Example 4: Finding out about the parameters of a service
# Exploring a service's parameter definitions all_params = service.get_all_parameters() # Print all parameter names print all_params.keys() # Find out more about the 'query' parameter (assuming we know the name) param = service.get_parameter(“query”) # Accessing meta information about the parameter print param.name print param.description print param.required print param.type_str # Returns string representation of base type # Checking if a proposed object can be cast to the required type param.sanity_check(“Some value”) # Raises exception if problem
Example 5: Getting names and info for all resource
# Get a list of resource names resource_names = server.get_all_resource_names() # Get a dictionary of resources, keyed by the resource name and # containing a small dict with 'desc' and 'uri' field for each resource. resources_info = server.get_all_resource_names_plus()
Example 6: Getting names and info for all components
# Get a list of component names component_names = server.get_all_component_names() # Get a dictionary of components, keyed by the component name and # containing a small dict with 'desc' and 'uri' field for each component. components_info = server.get_all_component_names_plus()
Example 7: Creating a new resource
# Get the component on which the new resource is based comp = server.get_component("TwitterComponent") # Provide the resource creation time parameters. # A 'resource template' is used for this task. # Suggested name and description are fields shared by all resources. res_templ = comp.get_resource_template() res_templ.suggested_name = “MyTwitter” res_templ.description = “My personal Twitter account” # Component specific parameters can be explored via the # comp.get_all_parameters() function. But assuming we know # the necessary parameters already... res_templ.params = { “account_name” : “MyTwitterAccountName”, “account_password” : “**********” } # Finally create the new resource, ready to use res = res_templ.create_resource()