paulb@348 | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paulb@348 | 2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
paulb@348 | 3 | <head> |
paulb@348 | 4 | <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> |
paulb@348 | 5 | <title>Applications and Resources</title> |
paulb@348 | 6 | <meta name="generator" |
paulb@348 | 7 | content="amaya 8.1a, see http://www.w3.org/Amaya/" /> |
paulb@348 | 8 | <link href="styles.css" rel="stylesheet" type="text/css" /> |
paulb@348 | 9 | </head> |
paulb@348 | 10 | <body> |
paulb@348 | 11 | <h1>Applications and Resources</h1> |
paulb@348 | 12 | At its simplest a WebStack application is just a single Python |
paulb@348 | 13 | class that we call a "resource". We can either create a module file <code>MyApplication.py</code> |
paulb@348 | 14 | (for example) or a |
paulb@348 | 15 | package directory <code>MyApplication</code> (for example) containing |
paulb@348 | 16 | a module file <code>__init__.py</code>, and we write into that file a |
paulb@348 | 17 | definition |
paulb@348 | 18 | of the resource as follows: |
paulb@348 | 19 | <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> |
paulb@348 | 20 | <p>It is in this kind of resource class that we write the |
paulb@348 | 21 | actual application code or at least the beginnings of it. When a user |
paulb@348 | 22 | of the application sends us a request, |
paulb@348 | 23 | the <code>respond</code> method will be called and the code |
paulb@348 | 24 | within it executed. The parts of the pseudo-code in |
paulb@348 | 25 | the above text which aren't valid Python |
paulb@348 | 26 | (ie. the bits in square brackets) will use the <code>trans</code> |
paulb@348 | 27 | object to find out what any given user of the application has sent us, |
paulb@348 | 28 | and to send information back |
paulb@348 | 29 | to the |
paulb@348 | 30 | user in response.</p> |
paulb@348 | 31 | <h2>Starting Simple</h2> |
paulb@348 | 32 | <p>The simplest way to turn this into a working application is to |
paulb@348 | 33 | ignore the first two activities mentioned in the pseudo-code and just |
paulb@348 | 34 | to produce some kind of |
paulb@348 | 35 | response. Here is how we might do that (in our module file <code>MyApplication.py</code> |
paulb@348 | 36 | or <code>MyApplication/__init__.py</code>):</p> |
paulb@348 | 37 | <pre>class MyResource:<br /> def respond(self, trans):<br /> out = trans.get_response_stream()<br /> print >>out, "Hello world."</pre> |
paulb@348 | 38 | <p>To deploy this, we need an adapter - see <a href="deploying.html">"Deploying |
paulb@348 | 39 | a WebStack Application"</a> for something which will work with this |
paulb@348 | 40 | example. |
paulb@348 | 41 | </p> |
paulb@348 | 42 | <h2>How Resources are Created |
paulb@348 | 43 | </h2> |
paulb@348 | 44 | <p>In the above example, the only thing we need to |
paulb@348 | 45 | consider is |
paulb@348 | 46 | what our code does, not how resource objects are created and invoked |
paulb@348 | 47 | (that is |
paulb@348 | 48 | done in the <a href="deploying.html">adapter code</a>). In more |
paulb@348 | 49 | complicated applications, there may |
paulb@348 | 50 | be a |
paulb@348 | 51 | need to create our own resource objects explicitly, but this is not |
paulb@348 | 52 | particularly interesting to think about at this point - see <a |
paulb@348 | 53 | href="paths-filesystem.html">"Treating the Path Like |
paulb@348 | 54 | a Filesystem"</a> for a |
paulb@348 | 55 | discussion of multiple resource objects.</p> |
paulb@348 | 56 | <h2>Design Considerations</h2> |
paulb@348 | 57 | <p>When writing an application, we |
paulb@348 | 58 | must consider a number of factors which |
paulb@348 | 59 | have an impact on the code (and other things) that we will need to |
paulb@348 | 60 | provide as |
paulb@348 | 61 | part of the finished product or service.</p> |
paulb@348 | 62 | <ul> |
paulb@348 | 63 | <li><a href="paths.html">URLs and Paths</a></li> |
paulb@348 | 64 | <li><a href="methods.html">Request Methods</a></li> |
paulb@348 | 65 | <li><a href="parameters.html">Request Parameters and Uploads</a></li> |
paulb@348 | 66 | <li><a href="responses.html">Responses and Presentation</a></li> |
paulb@348 | 67 | <li><a href="state.html">Cookies, Sessions and Persistent |
paulb@348 | 68 | Information</a></li> |
paulb@348 | 69 | </ul> |
paulb@348 | 70 | </body> |
paulb@348 | 71 | </html> |