1

Not sure if this has an actual answer or not, but here goes.

I'm building a webapp, using webapp2 on appengine that will only expose a JSON API to the clients (other than shell HTML templates for browser-based clients).

What I have now is a main.py that routes incoming URLs to handlers. The handlers are separated into modules by function (ie user_auth, user_info, groups, etc...). I have a models.py module where my ndb.Models live.

The lowest level code involving the Models are contained in the Model subclasses in models.py (ie the models.Group has a classmethod called create_group, which is called by groups.AddGroupHandler.add_group, which is called by groups.AddGroupHandler.post.)

I also have an api.py module that contains classes for all of the webapp's behavior (ie api.UserAuth, api.Signup, api.RegistrationVerification, etc...) which contains classmethods that return JSON for those behaviors (ie api.UserAuth.session_expired, api.RegistrationVerification.bad_token, etc...).

The methods in api.py are called from the "low-level" methods in models, the "helper" methods in the handler modules (ie groups.AddGroupHandler.add_group, etc...), and the handler methods themselves (ie groups.AddGroupHandler.post, etc...).

Is this structure going to cause issues in the future, or is it fine as is? Also, are there any standard structures that are known to work for this kind of project?

EDIT: I'm not using REST or anything like that.

Eliezer
  • 7,209
  • 12
  • 56
  • 103
  • 3
    Are you familiar with the Model View Controller (MVC) design pattern? It's generally pretty useful. In your particular case, you'd want to separate your "Model" from your "Controller" to make it easier to maintain in the future. The "View" would essentially just be your JSON serialization code. – dragonx Aug 20 '13 at 14:07
  • If you're mapping URLs to APIs, you're using REST: http://en.wikipedia.org/wiki/Representational_state_transfer – Brent Washburne Aug 20 '13 at 16:37
  • @dragonx Thanks. I'm vaguely aware of it, but I'll look into it a little deeper now. – Eliezer Aug 20 '13 at 18:27
  • @BrentWashburne I'm fairly confident that many of my practices violate REST (I make use of sessions on the server, I don't strictly use the proper HTTP verbs [mostly just POST], etc...), but perhaps that would still be considered REST-like? – Eliezer Aug 20 '13 at 18:29
  • Sure! http://stackoverflow.com/q/6068113/584846 – Brent Washburne Aug 20 '13 at 18:35
  • @BrentWashburne Thanks. I should probably read up some more about REST. About my original question, do you have any suggestions other than MVC? – Eliezer Aug 21 '13 at 12:33
  • 1
    MVC is a really good suggestion. It helps to keep database calls separate from JSON formatting and "controller" logic. You will have a well-organized structure if you keep the relation between objects in mind. You already have models.py and api.py (for JSON), just add modules for your helper (control) methods. Refactor regularly when code becomes bulky or hard to understand. – Brent Washburne Aug 21 '13 at 14:05
  • @BrentWashburne So in this case, my models.py would be the "model," api.py would be the "view," and my handlers would be the "controller"? Does it follow that my handlers would receive a request, and respond with JSON received from a method or an object (depending on the situation) from api.py which would communicate with the `ndb.Model` subclasses in models.py that would give api.py the information it needs to build the JSON response? If so, should my handlers only have a call to `self.response.write(api.some_call)` or something like that? – Eliezer Aug 21 '13 at 15:46
  • 1
    Yes, you're on the right track. You can make the views more intelligent by performing authentication and customizing the views for each user. – Brent Washburne Aug 21 '13 at 16:28

1 Answers1

2

I'd highly recommend you look into Google Cloud Endpoints as it allows you to build out an API using the endpoints framework and then Google provides client libraries for you to leverage so you don't have to roll your own iOS, Android, Javascript, etc front end code to communicate with your service layer.

https://developers.google.com/appengine/docs/java/endpoints/

There's a bit of a learning curve but once you get through it, it's a nice feature to work with. It also includes things like OAuth 2.0 support built in which otherwise you'd need to roll your own if you want to have authentication on your service layer.

earthtrip
  • 498
  • 5
  • 18
  • It seems like this would only work with Java? I'm using Python. – Eliezer Dec 19 '13 at 20:41
  • Rats - sent the wrong URL :( Sorry about that.. Here's the python docs https://developers.google.com/appengine/docs/python/endpoints/ – earthtrip Dec 20 '13 at 21:40
  • Thanks so much! I underestimated how useful this would be the first time I read about it, but it's just what I was looking for. – Eliezer Dec 26 '13 at 04:59