<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog - ShiftEleven &#187; Python</title>
	<atom:link href="http://shifteleven.com/articles/category/python/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 19 Sep 2009 11:19:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-alpha</generator>
		<item>
		<title>Problem with writing RESTful Google App Engine Code</title>
		<link>http://shifteleven.com/articles/2008/04/12/problem-with-writing-restful-google-app-engine-code</link>
		<comments>http://shifteleven.com/articles/2008/04/12/problem-with-writing-restful-google-app-engine-code#comments</comments>
		<pubDate>Sat, 12 Apr 2008 23:57:07 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[defect]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[webob]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=48</guid>
		<description><![CDATA[So after I used middleware to make browsers work with RESTful URLs in GAE I started to write more than simple little methods and I&#8217;m trying to write a test application.  Upon doing so, I ran into a defect with how request parameters are returned for a PUT request To be fair, this is [...]]]></description>
			<content:encoded><![CDATA[<p>So after I used middleware to <a href="http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine,">make browsers work with RESTful URLs in <acronym title="Google App Engine">GAE</acronym></a> I started to write more than simple little methods and I&#8217;m trying to write a test application.  Upon doing so, I ran into a <a href="http://code.google.com/p/googleappengine/issues/detail?id=170.">defect with how request parameters are returned for a PUT request</a> To be fair, this is more of a problem with <a href="http://pythonpaste.org/webob/">webob</a> than with GAE, but I hope that this gets fixed because my attempts at <a href="http://mail.python.org/pipermail/python-dev/2008-January/076194.html">monkey patching</a> the problem have not worked out too well.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/04/12/problem-with-writing-restful-google-app-engine-code/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Letting Browsers use all of the RESTful Methods in Google App Engine</title>
		<link>http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine</link>
		<comments>http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine#comments</comments>
		<pubDate>Wed, 09 Apr 2008 02:45:05 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=46</guid>
		<description><![CDATA[So there was much talk and whathave you about Google&#8217;s new App Engine and after viewing the tutorials and reading the documentation, I thought it was pretty rad too.  Because I was using rack for some previous stuff, WSGI felt right at home (probably because rack was modeled after WSGI).  There was one [...]]]></description>
			<content:encoded><![CDATA[<p>So there was much talk and whathave you about Google&#8217;s new <a href="http://code.google.com/appengine/,">App Engine</a> and after viewing the tutorials and reading the documentation, I thought it was pretty rad too.  Because I was using <a href="http://rack.rubyforge.org/">rack</a> for some previous stuff, <a href="http://www.wsgi.org/">WSGI</a> felt right at home (probably because rack was modeled after WSGI).  There was one thing that bothered me however.  When writing a class using their <a href="http://code.google.com/appengine/docs/webapp/">webapp framework</a> there isn&#8217;t any magic param name, like <code>_method</code>, that I could use to mock requests methods like <code>DELETE</code> and <code>PUT</code> since the browser doesn&#8217;t support those HTTP methods.</p>
<p>Well my friends, fear not.  If you enjoy the sweet freedom of RESTful urls, I&#8217;m here to help.  Since this is all built on top of WSGI, we can create a some new Middleware to sit before the application to resolve this little issue.</p>
<p><span id="more-46"></span></p>
<h3>MockHTTPMethodMiddleware</h3>
<pre class="python" title="code">from google.appengine.ext import webapp

class MockHTTPMethodMiddleware(object):
  def __init__(self, app):
    self.app = app

  def __call__(self, environ, start_response):
    method = webapp.Request(environ).get('_method')
    if method:
      environ['REQUEST_METHOD'] = method.upper()
    return self.app(environ, start_response)</pre>
<p>The code is pretty simple.  So when this middleware is called, it processes the <code>environ</code> into a <code>Request</code> object.  From there, I just inspect the params for a key called <code>_method</code>.  If that param exists, then it overwrites the <code>REQUEST_METHOD</code> and bam&#8230;we have mocked the HTTP method of our choosing.</p>
<h3>Putting that code to good use</h3>
<p>Let&#8217;s pretend that this is our class is something inane like this:</p>
<pre class="python" title="code">from google.appengine.ext import webapp

class MethodTest(webapp.RequestHandler):
  def get(self):
    self.response.headers["Content-Type"] = 'text/text'
    self.response.out.write("Get")

  def put(self):
    self.response.headers["Content-Type"] = 'text/text'
    self.response.out.write("Put")</pre>
<p>So now when you are writing your <code>main</code> method that creates the WSGI application, you would write it something like:</p>
<pre class="python" title="code">import wsgiref.handlers

def main():
  app = webapp.WSGIApplication(
      [('/', MethodTest)],
      debug=True)
  wsgiref.handlers.CGIHandler().run(MockHTTPMethodMiddleware(app))

if __name__ == "__main__":
  main()</pre>
<p>Notice that in the run method arguments, <code>MockHTTPMethodMiddleware</code> takes <code>app</code> as a parameter, thus <code>MockHTTPMethodMiddleware</code> will run before it runs <code>app</code>.</p>
<h3>Conclusion</h3>
<p>While there wasn&#8217;t support out of the box for mocking HTTP methods for browsers, with a little help from a WSGI Middleware class, it&#8217;s not too hard to add that functionality in, nor is it hard to add anything else you would want to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/04/08/letting-browsers-use-all-of-the-methods-in-google-app-engine/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
