1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/docs/resources.html Tue Apr 19 22:32:46 2005 +0000
1.3 @@ -0,0 +1,71 @@
1.4 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1.5 +<html xmlns="http://www.w3.org/1999/xhtml">
1.6 +<head>
1.7 + <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" />
1.8 + <title>Applications and Resources</title>
1.9 + <meta name="generator"
1.10 + content="amaya 8.1a, see http://www.w3.org/Amaya/" />
1.11 + <link href="styles.css" rel="stylesheet" type="text/css" />
1.12 +</head>
1.13 +<body>
1.14 +<h1>Applications and Resources</h1>
1.15 +At its simplest a WebStack application is just a single Python
1.16 +class that we call a "resource". We can either create a module file <code>MyApplication.py</code>
1.17 +(for example) or a
1.18 +package directory <code>MyApplication</code> (for example) containing
1.19 +a module file <code>__init__.py</code>, and we write into that file a
1.20 +definition
1.21 +of the resource as follows:
1.22 +<pre>class MyResource:<br /> def respond(self, trans):<br /> [Examine the transaction, decide what the user wants to do.]<br /> [Perform some kind of action with the information supplied.]<br /> [Produce some kind of response which tells the user what happened.]</pre>
1.23 +<p>It is in this kind of resource class that we write the
1.24 +actual application code or at least the beginnings of it. When a user
1.25 +of the application sends us a request,
1.26 +the <code>respond</code> method will be called and the code
1.27 +within it executed. The parts of the pseudo-code in
1.28 +the above text which aren't valid Python
1.29 +(ie. the bits in square brackets) will use the <code>trans</code>
1.30 +object to find out what any given user of the application has sent us,
1.31 +and to send information back
1.32 +to the
1.33 +user in response.</p>
1.34 +<h2>Starting Simple</h2>
1.35 +<p>The simplest way to turn this into a working application is to
1.36 +ignore the first two activities mentioned in the pseudo-code and just
1.37 +to produce some kind of
1.38 +response. Here is how we might do that (in our module file <code>MyApplication.py</code>
1.39 +or <code>MyApplication/__init__.py</code>):</p>
1.40 +<pre>class MyResource:<br /> def respond(self, trans):<br /> out = trans.get_response_stream()<br /> print >>out, "Hello world."</pre>
1.41 +<p>To deploy this, we need an adapter - see <a href="deploying.html">"Deploying
1.42 +a WebStack Application"</a> for something which will work with this
1.43 +example.
1.44 +</p>
1.45 +<h2>How Resources are Created
1.46 +</h2>
1.47 +<p>In the above example, the only thing we need to
1.48 +consider is
1.49 +what our code does, not how resource objects are created and invoked
1.50 +(that is
1.51 +done in the <a href="deploying.html">adapter code</a>). In more
1.52 +complicated applications, there may
1.53 +be a
1.54 +need to create our own resource objects explicitly, but this is not
1.55 +particularly interesting to think about at this point - see <a
1.56 + href="paths-filesystem.html">"Treating the Path Like
1.57 +a Filesystem"</a> for a
1.58 +discussion of multiple resource objects.</p>
1.59 +<h2>Design Considerations</h2>
1.60 +<p>When writing an application, we
1.61 +must consider a number of factors which
1.62 +have an impact on the code (and other things) that we will need to
1.63 +provide as
1.64 +part of the finished product or service.</p>
1.65 +<ul>
1.66 + <li><a href="paths.html">URLs and Paths</a></li>
1.67 + <li><a href="methods.html">Request Methods</a></li>
1.68 + <li><a href="parameters.html">Request Parameters and Uploads</a></li>
1.69 + <li><a href="responses.html">Responses and Presentation</a></li>
1.70 + <li><a href="state.html">Cookies, Sessions and Persistent
1.71 +Information</a></li>
1.72 +</ul>
1.73 +</body>
1.74 +</html>