Skip to content

Commit

Permalink
Added blobstore image example (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesandlord authored and Jon Wayne Parrott committed Apr 28, 2016
1 parent 3345110 commit fe59a33
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
52 changes: 52 additions & 0 deletions appengine/images/api/blobstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Sample application that demonstrates how to use the App Engine Images API.
For more information, see README.md.
"""

# [START all]
# [START thumbnailer]
from google.appengine.api import images
from google.appengine.ext import blobstore

import webapp2


class Thumbnailer(webapp2.RequestHandler):
def get(self):
blob_key = self.request.get("blob_key")
if blob_key:
blob_info = blobstore.get(blob_key)

if blob_info:
img = images.Image(blob_key=blob_key)
img.resize(width=80, height=100)
img.im_feeling_lucky()
thumbnail = img.execute_transforms(output_encoding=images.JPEG)

self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(thumbnail)
return

# Either "blob_key" wasn't provided, or there was no value with that ID
# in the Blobstore.
self.error(404)
# [END thumbnailer]


app = webapp2.WSGIApplication([('/img', Thumbnailer)], debug=True)
# [END all]
45 changes: 45 additions & 0 deletions appengine/images/api/blobstore_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import blobstore
import mock
import pytest
import webtest


@pytest.fixture
def app(testbed):
return webtest.TestApp(blobstore.app)


def test_img(app):
with mock.patch('blobstore.images') as mock_images:
with mock.patch('blobstore.blobstore') as mock_blobstore:
mock_blobstore.get.return_value = b'123'
mock_images.resize.return_value = 'asdf'
mock_images.im_feeling_lucky.return_value = 'gsdf'

response = app.get('/img?blob_key=123')

assert response.status_int == 200


def test_img_missing(app):
# Bogus blob_key, should get error
app.get('/img?blob_key=123', status=404)


def test_no_img_id(app):
# No blob_key, should get error
app.get('/img', status=404)
2 changes: 1 addition & 1 deletion appengine/images/api/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ def test_img_missing(app):


def test_no_img_id(app):
# Bogus image id, should get error
# No image id, should get error
app.get('/img', status=404)

0 comments on commit fe59a33

Please sign in to comment.