3

Is it possible to create an API using just webapp2 and Python on Google App Engine?

For example, let's my route /post/123 is handled by this RequestHandler:

class ShowPosts(webapp2.RequestHandler):
    def get(self):
        posts = Post.query().fetch()
        # return the post as data (JSON) here as response 

When a client makes a restful request to /post/123, it can be returned the data object (instead of a rendered html page).

Is this possible or recommended?

Ying Li
  • 2,500
  • 2
  • 13
  • 37
puoygae
  • 573
  • 7
  • 19

2 Answers2

5

You can build a python list or dict object out of the query, then send it as a JSON object, and send that as the response. Try something like this:

import json

posts     = Post.query()
post_json = []

for post in posts:
    post_dict = {
        'name' : post.name,
        'city' : post.city,
        'state': post.state
    }
    post_json.append( post_dict )

return json.dumps(post_json)

UPDATE: OP asked for example with POST method:

import json

class ShowPosts(webapp2.RequestHandler):
    def get(self):
        posts = Post.query()
        post_json   = []

        for post in posts:
            post_dict = {
                'name' : post.name,
                'city' : post.city,
                'state': post.state
            }

            post_json.append( post_dict )

        return json.dumps(post_json) 

    def post(self):
        posts = Post.query()
        post_json   = []

        for post in posts:
            post_dict = {
                'name' : post.name,
                'city' : post.city,
                'state': post.state
            }

            post_json.append( post_dict )

        post_json.append(
            {
                'posted_name': self.request.get('name'),
                'posted_msg': self.request.get('msg')
            }
        )
        return json.dumps(post_json) 
GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • Thank you for this example which demonstrates how to handle GET methods. Another part of the API should be able to handle the other http methods; how would we configure this to handle POST or DELETE methods? – puoygae Aug 28 '18 at 17:29
  • Not sure I understand. Handling a POST method is basic. You should read https://webapp2.readthedocs.io/en/latest/guide/request.html I don't want to spend any time doing a student's homework. See updated answer. – GAEfan Aug 28 '18 at 18:53
  • One thing to note is that when using webapp2 to handle POST requests with params, we use self.request.get('some_field'), whereas, when our app receives restful post requests, it looks like we access the sent data using self.request.body. – puoygae Aug 31 '18 at 13:12
1

You don't have to return a HTML page. You can return a JSON, or even just a string since it's your code that you are hosting. You can easily send up an URL with your App Engine that can respond to a REST call.

Ying Li
  • 2,500
  • 2
  • 13
  • 37
  • What if we are to POST data? How can we do this? If you are willing, would you offer an example in your answer? – puoygae Aug 25 '18 at 23:20