Request against a Taffy resource from within another Taffy resource

115 views
Skip to first unread message

Matt Walker

unread,
Jul 31, 2013, 5:30:45 PM7/31/13
to taffy...@googlegroups.com
Hi! Is it possible to make a request against a Taffy resource from within another Taffy resource without using http? For example, I have a collection /students and its members /students/{id}. Within my GET request against /students, I'd like to get a list of student ids from my db and then iterate over the list calling /students/{id} for each id to build the full /students response. Would I need to use <cfhttp> within the loop to make individual /students/{id} requests or can I invoke the /students/{id} CFC directly? 

Thanks!

  -- Matt

Adam Tuttle

unread,
Jul 31, 2013, 5:51:57 PM7/31/13
to taffy...@googlegroups.com
It would be possible, by grabbing a reference to the factory object and requesting the bean, but remember that you're going to get a representation class back from, for example, resp = studentMember.get(id)...

You could then call resp.getData() to get the native data that was set into the representation instance.

However, I would say that this is inadvisable without knowing more about the implementation of the individual student get method. If you call it 20 times you're likely going to end up with 20 database calls (or worse, 20 file reads, if you're storing the data in files, for example)...

By contrast, you could query the database for the same students by id:

select * from students where id in (1,2,3,7,9,42)

... so I question your motives. But it's possible. I won't recommend it, and it's not supported behavior, and I won't promise not to break it in the future.

Can you make a convincing defense? :)

Adam



  -- Matt

--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matt Walker

unread,
Jul 31, 2013, 6:42:02 PM7/31/13
to taffy...@googlegroups.com
The students collection was just for purposes of example, the actual resources are much more complex. But your response is enough to dissuade me from going further with it. My question should have been "Without hacking the internals of Taffy, is it possible to make a request against a Taffy resource from within another Taffy resource without using http?" ;-)

Thanks again for the quick response and for building such a great framework!

  -- Matt

Adam Tuttle

unread,
Jul 31, 2013, 6:56:36 PM7/31/13
to taffy...@googlegroups.com
Fair enough. Thanks for using it! :)

Adam
Message has been deleted

Adam Tuttle

unread,
Dec 12, 2013, 10:52:28 AM12/12/13
to taffy...@googlegroups.com
Hi Richard,

I'm still not convinced this is ever a good idea. That said, it's totally possible.

Say you've got a CFC named fooCollection.cfc and you want to call its GET method from another one of your resources. I'm also assuming you're NOT using ColdSpring or any other external bean factory.

Taffy has an internal object factory saved at application._taffy.factory, so this should work:

result = application._taffy.factory.getBean('fooCollection').get(/* don't forget to pass any args necessary */);

Don't forget that the GET method does not return pure data, it's wrapped in a Taffy Representation object. If you just want to call that get method and return its data then return this result as you've received it. Otherwise, if you want to manipulate the returned data or do something else with it, you should call the
getData() method to get the underlying result data out of the representation object.

rawdata = application._taffy.factory.getBean('fooCollection').get(/* args */).getData();

Again, I recommend against this and it is _NOT_ supported behavior. It might break in a later release without warning. It's also a sign that things probably are not organized properly.

Adam


On Sun, Dec 8, 2013 at 6:08 PM, Richard Poole <rpo...@gmail.com> wrote:
Hi Adam,

I've got a case where being able to leverage an existing resource would be very helpful. I have an existing resource that fetches messages based on a range of filters that the requester can set. I also need to build a call that fetches all data for a person including all of the messages they have sent. It would be great if I could use the existing messages resource so I only need to update one place in the future.

I'm not a CF expert by any stretch so could you give me a pointer or two on how I could do this?

Cheers,

Richard

Matt Walker

unread,
Dec 12, 2013, 1:00:58 PM12/12/13
to taffy...@googlegroups.com
@Richard - If performance isn't a concern, you can always use cfhttp calls within your person resource to access your messages collection.

The way I worked around having to make additional API requests was to abstract resources and collections into another layer of CFCs so that my Taffy CFCs define the interface, load the resource CFCs, and return the response and my resource CFCs handle the implementation of the resources. Then if I need to build a collection, I can iterate over the resource CFCs rather than iterating over API requests. 



--
You received this message because you are subscribed to a topic in the Google Groups "Taffy Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/taffy-users/ATi8-YaoIOM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to taffy-users...@googlegroups.com.

Richard Poole

unread,
Dec 12, 2013, 1:45:08 PM12/12/13
to taffy...@googlegroups.com
Thanks Adam and Matt. I've looked into some other options and this still seems like the best way. Just to explain a bit further the scenario:

I have a resource that grabs communication data based on filters the user provides (such as date, recipient, project that the message is attached to, etc) and that resource can return thousands of messages with no performance impact. There are a lot of checks in that resource to ensure the user making the call only gets the communications they are allowed.

I have another resource that grabs all of the data for a single project (this includes a HEAP of data including the communication data for just that project) and I am using the communication resource to fetch that data, it's only being called once.

I am not (and don't plan to) call another resource multiple times, it will only ever be the case that a resource is called once from any other resource.


Reply all
Reply to author
Forward
0 new messages