Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Zahra Rajabi
tweepy
Commits
58ee4790
Commit
58ee4790
authored
Jul 13, 2009
by
Josh Roesslein
Browse files
Added support for callbacks.
parent
2d152504
Changes
1
Hide whitespace changes
Inline
Side-by-side
binder.py
View file @
58ee4790
import
httplib
import
urllib
from
threading
import
Thread
from
parsers
import
parse_error
from
error
import
TweepError
def
bind_api
(
path
,
parser
,
allowed_param
=
None
,
method
=
'GET'
,
require_auth
=
False
):
def
_call
(
api
,
*
args
,
**
kargs
):
# If require auth, throw exception if credentials not provided
if
require_auth
and
not
api
.
_b64up
:
raise
TweepError
(
'Authentication required!'
)
# Filter out unallowed parameters
if
allowed_param
:
parameters
=
dict
((
k
,
v
)
for
k
,
v
in
kargs
.
items
()
if
k
in
allowed_param
)
else
:
parameters
=
None
def
do_request
(
url
,
parameters
,
api
):
# Open connection
if
api
.
secure
:
conn
=
httplib
.
HTTPSConnection
(
api
.
host
)
else
:
conn
=
httplib
.
HTTPConnection
(
api
.
host
)
# Build url with parameters
if
parameters
:
url
=
'%s?%s'
%
(
path
,
urllib
.
urlencode
(
parameters
))
else
:
url
=
path
# Assemble headers
headers
=
{
'User-Agent'
:
'tweepy'
...
...
@@ -53,4 +38,34 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False)
conn
.
close
()
return
out
return
_call
def
async_request
(
url
,
parameters
,
api
,
callback
):
out
=
do_request
(
url
,
parameters
,
api
)
callback
(
out
)
def
call
(
api
,
*
args
,
**
kargs
):
# If require auth, throw exception if credentials not provided
if
require_auth
and
not
api
.
_b64up
:
raise
TweepError
(
'Authentication required!'
)
# Filter out unallowed parameters
if
allowed_param
:
parameters
=
dict
((
k
,
v
)
for
k
,
v
in
kargs
.
items
()
if
k
in
allowed_param
)
else
:
parameters
=
None
# Build url with parameters
if
parameters
:
url
=
'%s?%s'
%
(
path
,
urllib
.
urlencode
(
parameters
))
else
:
url
=
path
# check for callback
callback
=
kargs
.
get
(
'callback'
)
if
callback
:
# execute request async
Thread
(
target
=
async_request
,
args
=
(
url
,
parameters
,
api
,
callback
,)).
start
()
else
:
# execute request sync
return
do_request
(
url
,
parameters
,
api
)
return
call
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment