You are here: TUTWiki>Wille>Examples
This document is deprecated. For the latest version, please see http://packages.python.org/wille/

Examples

In this section, we will introduce and explain the functionalities of some example services for Wille. In order to try out the services, you can either paste the source code directly from the examples. The services are also delivered as part of Wille2 Core package.

Request Sniffer Service

Request Sniffer (requestsniffer) is a service that displays all information that gets passed to Wille services written using Python adapter, allowing inspection/"sniffing" of this information. The sniffer is used for the purpose of demonstrating what data is passed and how they can be accessed from Wille Services. The service is particularly useful also for inspecting the actual content during service development and usage as well.

The sniffer does not require any additional libraries.

Implementation (servicerequestsniffer.py)

import cgi

import wille

class RequestSnifferService(wille.Service): def execute(self, params, servicepool, keyring, workdir): data = '
---+ Request Data Sniffer

'

# Data data += '
---++ Input parameters:

' data += '
   * %s=%s
   * No input parameters. Try adding some!
'

data += '
---++ Services

' data += '
   * %s
'

data += '
---++ Keyring

' data += '
   * =%s=
   * _(empty)_
'

# Return tuple response: # First item: data # Second item: additional HTTP headers # Third item: HTTP response code (default=200) return (data, {'Content-type': 'text/html'})

Service Wrapper (willeservice.properties)

parameters=
description=Service for inspecting ("sniffing") information that gets passed to Wille services
profile=examples
adapter=PythonService
adapter.command=servicerequestsniffer.RequestSnifferService

Weather Report Service

Weather Report (weather) is a service that generates a seven-day weather report for a given location by utilising public web APIs.

The service demonstrates the use of Wille for following purposes:

  • Calling a HTTP-based web API (Google Maps Geocoding Service)
  • Generation of a Web Services client from WSDL and calling a service from the client (National Weather Service Web Services API)
The service uses the following Python libraries:

Implementation (weather.py)

import wille

class WeatherService(wille.Service): def execute(self, params, servicepool, keyring, workdir): # Parse end urlencode input parameter(s) from urllib import quote e_address = quote(params['address'])

# Define to get a seven day weather report, starting from today from datetime import datetime, timedelta starttime = datetime.today().strftime("%Y-%m-%d") endtime = (datetime.today()+timedelta(days=7)).strftime("%Y-%m-%d")

# Geocode address import httplib2 import simplejson as json geocoder_root_url = 'http://maps.google.com/maps/api/geocode/json?' geocoder_req_url = geocoder_root_url+'address='+e_address+'&sensor=false' h = httplib2.Http("") resp, content = h.request(geocoder_req_url) geocoder_result = json.loads(content) location = geocoder_result['results'][0]['geometry']['location']

# Get a day weather report from suds.client import Client forecast_wsdl_url = 'http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl' client = Client(forecast_wsdl_url) weather_report = client.service.NDFDgen(latitude = location['lat'], longitude = location['lng'], product = 'time-series', startTime = starttime, endTime = endtime, weatherParameters = False )

# Return weather report XML return (weather_report.encode('utf-8'), {'Content-type': 'application/xml; charset=utf-8'})

Service Wrapper (willeservice.properties)

description=Exemplary weather report generator service
parameters=address
profile=examples
adapter=PythonService
adapter.command=weather.WeatherService
Print version |  PDF  | History: r19 < r18 < r17 < r16 | 
Topic revision: r19 - 12 Mar 2013 - 14:43:12 - JaakkoSalonen
 

TUTWiki

Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TUTWiki? Send feedback