when do you use getGraphObject and getGraphEdge - facebook-graph-api

I'm working on a php based site and integrating some facebook sdk into it to get some basic user information. One thing I've been running into is, when I make a request to the graph, how do I know if I should be using getGraphObject or getGraphEdge? I'm not seeing anything intuitive yet to tell me that.
I'm running the php-sdk4 -> version 5
Here's one example that I ran into last night..
$response = $fb->get('/me/friends', $fbToken);
$fbfriends = $response->getGraphEdge()->asArray();
In the documentation, if you look at the php example, they use getGraphObject. But when I use it, I get an error telling me I should probably use the Edge. There are a lot of "get" functions, but I don't see anything that tells me how to know what to use. getGraphObject, getGraphEdge, getGraphUser, etc. I'd love some insight into this one, because it's been a guessing game for me.

Basicly, when:
Getting /{node-id}, you should use getGraphNode() (getGraphObject() is deprecated)
Getting /{node-id-with-known-type}, you should use getGraph{Type}().
For example, getting /me then use getGraphUser(), getting /{event-id} then use getGraphEvent()
Getting /{node-id}/{edge-name}, you should use getGraphEdge() (getGraphList() is deprecated)
Getting /{node-id}/{edge-name-with-children-known-type}, you should use getGraphEdge({children-type-class}).
For example, getting /me/albums then use getGraphEdge('GraphAlbum')
Nodes class name are in Facebook\GraphNodes namespace.

Related

Django api - How to use DestroyAPIView and maintain RESTfulness

How would one use Django DestroyAPIView and DetailAPIView and still maintain generally accepted practices of RESTfulness?
If I understand REST correctly it should work as follow (only one example)
/api/game/222
then 1 view class(generics.DetailAPIView) or method, in Django would be created to handle the call
In a REST world, I believe we would use a generic API class to handle the
methods (get,...)
But if I wanted to use the class(generics.DestroyAPIView) to handle the calls to delete a game.
then I would have to use
/api/game/delete/222
to send the request to the correct view.
It seems to me this is not consistent with RESTFULness.
for the delete method of the HTTP should be used to send the delete request and use the same pattern matching /apt/game/222 to delete the game. It's redundant.
Question: Am I missing something?
In summary
option 1:
/api/game/delete/222 (DestroyAPIView)
/api/game/detail/222 (DetailAPIView)
option 2
/api/game/222 (RetrieveDestroyAPIView)
I guess either way works, and as long as it's clear and consistent as stated below. There is no "right" way.
I not sure 'RESTFULness' affect what you are asking here. It is perfectly fine to have different matching pattern as long as it make sense.
In the example given, usually we do not have /api/game/222 as a generics.ListCreateAPIView. This is because List return a list of all game, we do not pass a id in. Create is trying to create a new Game. You do not have id yet because it is not in database, therefore both of the matching pattern should usually be /api/game/ instead.
/api/game/222 - such pattern, we usually use for generics.RetrieveUpdateDestroyAPIView because with a given id in url, we can get the correct Game object to either retrieve, update or delete it.
Regarding the Restful API, the answers in this stack overflow question explain more about 'Restfulness'.

How to list tables in RethinkDb using C++

When I do something in Python or in JavaScript I always have a lot of opportunities, both, to read documentation of a particular library and to try tons of teeny-weeny examples.
Unfortunatelly, in C++ it is not so popular (for what reason?) to provide at least a little ammount of working examples in documentation. Two good examples are C++ clients for MongoDb and RethinkDb.
My question here concerns RethinkDB. In Python I know how to list all table names, not because there is documentation and I'm supposed to dive into the driver code, but just because there is a tiny handy example of doing this:
r.db('test').table_list().run(conn)
And I'm done. In C++ I do not know how to do this - how to list all table names. I do not know even if there is such a method. I wish someone could provide little instructions and share their knowledge.
EDIT
It seems, like I found an appropriate method table_list, but unfortunatelly I do not know how to use it. Besides, it seems that I try to connect to the database in a wrong manner - by this I mean that I connect to the server, but not to a particular database (and again I do not know how to implement this). So, this is what I have now:
std::unique_ptr<R::Connection> conn = R::connect("localhost",28105);
//^^^ I want to connect to a particular database "mydb" - how to do that?
R::Cursor cursor = R::table_list().run(*conn);
for(R::Datum& item : cursor){
do_something(R::write_datum(item).c_str());
// ^^^ is that right???
}
If I do it, like I showed - without specifying the database name, then I get nothing. If, however, I try to connect like this:
R::connect("localhost",28105,"mydb");
then inside for I get an infinite loop. So, I need some help. Thanks!
EDIT
Phew, I found a solution. And I must confess, that it is rather intuitive. I will post it below.
This is the solution:
std::unique_ptr<R::Connection> conn = R::connect("localhost",28105);
R::Cursor cursor = R::db("mydb").table_list().run(*conn);
for(R::Datum& item : cursor){
do_something(R::write_datum(item).c_str());
}
and it works great. I want to thank AtnNn - the sole developper of this great driver.

Mongoid 4 finding embedded documents by ID

I have a project that is my first serious dive into Mongoid.
I saw a tip to use the following command:
Parent.where('childrens._id' => Moped::BSON::ObjectId(params[:id])).first
But this doesn't work. Error message was:
NameError: uninitialized constant Moped::BSON
I found that BSON is no longer included, so I added it to my Gemfile, as well as Moped. Then, I did another fix I found (placing Moped::BSON=BSON in application.rb).
This still didn't work, but the error changed to:
NoMethodError: undefined method `ObjectId' for BSON:Module
So I am assuming that this method got deprecated or something. Does anyone have any other tips?
Just to be clear, I am finding myself in the situation where I want to sort embedded documents using jquery-sortable. This requires me to update them in the database, but the serialize from that doesn't include the parent document in the hash. So I figured I'd try to get it on the back end using an ID from the embedded document. That is why I need it.
Thanks again for any help you can provide.
Try simply:
Parent.where('childrens._id' => params[:id]).first
I have solved the question though this won't be of much help to people in the future. The requirements have changed and now I am using human-readable strings as IDs to assist in friendly URLs and some other stuff.
Therefore, I don't have any issues with ObjectIds. Cortex's solution should (from what I have read) work for dealing with ObjectIds but I cannot verify it now.

RESTful API and Foreign key handling for POSTs and PUTs

I'm helping develop a new API for an existing database.
I'm using Python 2.7.3, Django 1.5 and the django-rest-framework 2.2.4 with PostgreSQL 9.1
I need/want good documentation for the API, but I'm shorthanded and I hate writing/maintaining documentation (one of my many flaws).
I need to allow consumers of the API to add new "POS" (points of sale) locations. In the Postgres database, there is a foreign key from pos to pos_location_type. So, here is a simplified table structure.
pos_location_type(
id serial,
description text not null
);
pos(
id serial,
pos_name text not null,
pos_location_type_id int not null references pos_location_type(id)
);
So, to allow them to POST a new pos, they will need to give me a "pos_name" an a valid pos_location_type. So, I've been reading about this stuff all weekend. Lots of debates out there.
How is my API consumers going to know what a pos_location_type is? Or what value to pass here?
It seems like I need to tell them where to get a valid list of pos_locations. Something like:
GET /pos_location/
As a quick note, examples of pos_location_type descriptions might be: ('school', 'park', 'office').
I really like the "Browseability" of of the Django REST Framework, but, it doesn't seem to address this type of thing, and I actually had a very nice chat on IRC with Tom Christie earlier today, and he didn't really have an answer on what to do here (or maybe I never made my question clear).
I've looked at Swagger, and that's a very cool/interesting project, but take a look at their "pet" resource on their demo here. Notice it is pretty similar to what I need to do. To add a new pet, you need to pass a category, which they define as class Category(id: long, name: string). How is the consumer suppose to know what to pass here? What's a valid id? or name?
In Django rest framework, I can define/override what is returned in the OPTION call. I guess I could come up with my own little "system" here and return some information like:
pos-location-url: '/pos_location/'
in the generic form, it would be: {resource}-url: '/path/to/resource_list'
and that would sort of work for the documentation side, but I'm not sure if that's really a nice solution programmatically. What if I change the resources location. That would mean that my consumers would need to programmatically make and OPTIONS call for the resource to figure out all of the relations. Maybe not a bad thing, but feels like a little weird.
So, how do people handle this kind of thing?
Final notes: I get the fact that I don't really want a "leaking" abstaction here and have my database peaking thru the API layer, but the fact remains that there is a foreign_key constraint on this existing database and any insert that doesn't have a valid pos_location_type_id is raising an error.
Also, I'm not trying to open up the URI vs. ID debate. Whether the user has to use the pos_location_type_id int value or a URI doesn't matter for this discussion. In either case, they have no idea what to send me.
I've worked with this kind of stuff in the past. I think there is two ways of approaching this problem, the first you already said it, allow an endpoint for users of the API to know what is the id-like value of the pos_location_type. Many API's do this because a person developing from your API is gonna have to read your documentation and will know where to get the pos_location_type values from. End-users should not worry about this, because they will have an interface showing probably a dropdown list of text values.
On the other hand, the way I've also worked this, not very RESTful-like. Let's suppose you have a location in New York, and the POST could be something like:
POST /pos/new_york/
You can handle /pos/(location_name)/ by normalizing the text, then just search on the database for the value or some similarity, if place does not exist then you just create a new one. That in case users can add new places, if not, then the user would have to know what fixed places exist, which again is the first situation we are in.
that way you can avoid pos_location_type in the request data, you could programatically map it to a valid ID.

cakePHP, encoding problem when using find+list, no problem when using find+all

Well, I've configured the database.php file, added the 'encoding'=>'utf8' option.
also added the $this->charset('utf8') to my view.
Now, when I use the find('list') and echo its content I get those known question-marks. But, when I use the find('all) method, the data is delivered correctly.
And my questions are:
Why?
Who is to blame?
How to solve ?(I really prefer the list way..)
Should I drink more coffee?
can you try $this->find('all') and then use set to extract the values, like Set::extract('/Post/title', $posts); and print it out. if the find all was good and the set::extract is bad there could be a bug. If it works like normal then there is something weird as that is what the core does.