Permalink
Browse files

Refactored API call method to work on App Engine as well.

  • Loading branch information...
1 parent e6650c5 commit 10679e8a97cf477c9e1b1591833b71f25b0564c9 @voodoochild committed Dec 9, 2011
Showing with 18 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +14 −4 app.py
  3. +3 −0 templates/base.html
View
@@ -1 +1,2 @@
.DS_Store
+*.pyc
View
18 app.py
@@ -1,6 +1,5 @@
from flask import Flask, render_template, abort
import random
-import requests
import json
CONTENT_API = 'http://content.guardianapis.com/search'
@@ -34,9 +33,20 @@ def get_articles_for_section(section):
def call_content_api(url):
"""Attempts to retrieve data from the Content API."""
- r = requests.get(app.config['CONTENT_API'] + url)
- if r.status_code == 200:
- return r.content
+ try:
+ from google.appengine.api import urlfetch
+ r = urlfetch.fetch(app.config['CONTENT_API'] + url, method=urlfetch.GET)
+ if r.status_code == 200:
+ return r.content
+ except ImportError:
+ pass # not running on app engine
+ try:
+ import requests
+ r = requests.get(app.config['CONTENT_API'] + url)
+ if r.status_code == 200:
+ return r.content
+ except ImportError:
+ pass
return False
def process_json(data):
View
@@ -1,3 +1,6 @@
+<!-- {microapp-css:{{ url_for('static', filename='css/readnext.css') }}} -->
+<!-- {microapp-javascript:{{ url_for('static', filename='js/readnext.js') }}} -->
+
<!DOCTYPE html>
<html lang="en">
<head>

0 comments on commit 10679e8

Please sign in to comment.