I would like to make a Tastypie based API adder. Here is how it works… the user would post to the two numbers they would like added and using Tastypie + Django I would like to include the added number on the return to the user.
I have no interest in putting it into the mySQL database.
class Adder(resource):
class Meta:
authorization = Authorization()
authentication = Authentication()
def hydrate(self,bundle):
_a = bundle.data['first_number']
_b = bundle.data['second_number']
self.create_response(request, return_dict)
return bundle
The documentation for Tastypie really seems to revolve around the models (for obvious reasons).
But I was curious if the create_response can be called from within the hydrate method and if calling the hydrate method is the right way of handling the post data.
I would probably skip the finer-grained things like hydrate, apply_sorting, build_filters, etc.
I’m assuming that without objects behind the api you’re using a list-looking url like
/api/v1/add_stuff/, and assuming you’re accepting POST requests. If these assumptions are wrong you can adjust by changing to post_detail, get_list, etc.Note that I think this code would work but I haven’t tested it. It’s meant to provide a starting point.
This section of the Tastypie docs describes the order in which the various methods are called, and toward the bottom of the page there is a full API reference so you can see what parameters things expect and what they are supposed to return.
Edit:
The flow for this situation will look something like this:
In
dispatch, the request uri is inspected. Depending on whether adetail or a list uri was requested (
/api/v1/add_stuff/<pk>/or/api/v1/add_stuff/), handling is delegated todispatch_detailordispatch_list. This is also where authentication, authorization,and throttling checks happen.
In
dispatch_list, the request method is inspected and the call isdelegated to a method named
'%s_list' % request.METHOD.lower().To answer your comment, these are magical method names. If the
request method is POST,
dispatch_listlooks for a method namedpost_listand an error is thrown if the appropriate handleris not defined.