Reputation: 3476
I've started working on a Cloud Endpoint API as a first-time Python programmer (with experience in Java and PHP).
I would like to keep everything together in one API but split different resource calls over different files. The documentation gives this as an example:
an_api = endpoints.api(name='library', version='v1.0')
@an_api.api_class(resource_name='shelves')
class Shelves(remote.Service):
...
@an_api.api_class(resource_name='books', path='books')
class Books(remote.Service):
...
endpoints.api_server([an_api])
What I would like to do is put the Shelves
and Book
classes in different files since they're both going to get quite long. I've tried just moving them, but that results in an empty API discovery document because the files probably aren't loaded/run/interpreted when the API is started.
How can I fix this? I have a feeling it's going to be something with import
but I can't figure it out...
Thanks!
Upvotes: 2
Views: 1197
Reputation: 163
https://cloud.google.com/appengine/docs/python/endpoints/api_server This worked for me
import endpoints
from main import an_api
from ShelvesClass import Shelves
from BookClass import Books
application = endpoints.api_server([an_api])
Upvotes: 1
Reputation: 7752
Yes, you have to sure that the api classes are properly imported, but if there was a problem with this you'd be getting some runtime exception rather than an empty discovery document.
The problem I can see is you're creating the api server with the an_api
object which you use to decorate your actual API classes. You should do the following instead:
an_api = endpoints.api(name='library', version='v1.0')
@an_api.api_class(resource_name='shelves')
class Shelves(remote.Service):
...
@an_api.api_class(resource_name='books', path='books')
class Books(remote.Service):
...
endpoints.api_server([Shelves, Books])
To then go from this to a multi-module API, you'll easily run into a circular dependency situation (something that Python cannot deal with). You then need a common module where you define an_api
; a set of API modules that implement a part of the API, all of which import
the common module; and then you'll need a main module that calls endpoints.api_server
.
A note: in the Python world it's not uncommon for a single module (file) to be quite long indeed and have very many classes in it; this may seem very odd coming from Java or well-structured PHP.
Upvotes: 5