-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added remove friend #298
Added remove friend #298
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made a few comments, and I fully understand if it seems confusing, the current system that _post
and _get
uses is super confusing, so feel free to ask me anything if you need clarification. Thanks for your contribution ;)
fbchat/client.py
Outdated
|
||
""" | ||
def removeFriend(self, friend_id=None): | ||
"""param friend_id: The id of the friend that you want to remove""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you could add a short description of the method, along with a documentation of the return value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will add it soon.
fbchat/client.py
Outdated
"confirm": "Confirm", | ||
} | ||
r = self._post(self.req_url.REMOVE_FRIEND,payload) | ||
return r.ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If r.ok
is not true, an exception could have been thrown by using fix_request=False
in Client._post
. Maybe the return value should be used for detecting whether the removal of the person actually succeeded? I tested four scenarios:
If the client isn't logged in, it's redirected to the login page
If the client is logged in, but hasn't been active in a while, it's presented with an error message (No redirect)
If the operation was successful, the client is redirected to /removefriend.php?friend_id=[friend_id]&removed&__req=e&_rdr
And if the friend_id was invalid, it's redirected to /removefriend.php?friend_id=[friend_id]&removed&err=1431010&__req=e&_rdr
So, I think we should update _post
to support the parameter allow_redirects
, just like _cleanGet
does, then use that, look at r['Location']
, and determine whether the redirect url starts with /removefriend
. If it does, but contains the err
parameter, we can either throw an error, or just return False
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I allowed redirects in the _post, but I can't get the location, only with r.url
. Can I use that?
Edit: Can we use this?
if "err=1431004" not in r.url: code here else: code here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of disabling redirects and using r['Location']
would be to reduce the number of requests that we need to send, but you can start by implementing it without, and just use r.url
.
Considering testing the error code, though your described method would probably work, I would argue we should use two functions from urllib.parse
, parse_qs
and urlparse
, and look at whether the dict
parse_qs(urlparse(url).query)
contains an error code.
I updated the code. I used print for tell if the remove was successful, but we can use something different if you want. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good, now all I have is nitpicky comments. Also, maybe the entire method should be moved to just below the friendConnect
method? Seems like it makes more sense in that group
fbchat/client.py
Outdated
payload = { | ||
"friend_id": friend_id, | ||
"unref": "none", | ||
"confirm": "Confirm", | ||
} | ||
r = self._post(self.req_url.REMOVE_FRIEND,payload) | ||
return r.ok | ||
dict = parse_qs(urlparse(r.url).query) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be careful about overwriting dict
, since its a builtin. Use a different, perhaps even a more descriptive name, like query
fbchat/client.py
Outdated
return r.ok | ||
dict = parse_qs(urlparse(r.url).query) | ||
if "err" not in dict: | ||
print ("Remove was successful!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you feel like it's needed, log output can be useful, but don't use print
, since people can't selectively disable the log output. Use log.info
or log.debug
here instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed that you addressed this in your comment
fbchat/client.py
Outdated
Removes a specifed friend from your friend list | ||
:param friend_id: The id of the friend that you want to remove | ||
:returns error when the removing was unsuccessful | ||
:returns True when the removing was successful |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The syntax in the docstrings is quite important, since it gets executed and published on RTFD. :returns
is wrong, it should be :return:
, and there should only be one of them
fbchat/client.py
Outdated
from urllib.parse import parse_qs | ||
except ImportError: | ||
from urlparse import urlparse | ||
from urlparse import parse_qs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When importing two things from the same module you can use commas, like this: from urlparse import urlparse, parse_qs
I updated the code. :) |
Looks good, I'll merge it as soon as I get TravisCI working again |
Okay, thank you! :) |
#294