Last active
August 29, 2015 14:10
-
-
Save jstacoder/46cf3db028e5f6bf4b70 to your computer and use it in GitHub Desktop.
multi-threaded requests to the new basecamp json api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
class BasecampApi(object): | |
_username = '' | |
_password = '' | |
_account_number = '' | |
_base_url = 'https://{}:{}@basecamp.com/{}/api/v1/' | |
def __init__(self,user,pw,acct_num): | |
self._user = user | |
self._password = pw | |
self._account_number = acct_num | |
self._base_url =\ | |
self._base_url.format( | |
self._user, | |
self._password, | |
self._account_number | |
) | |
def post(self,url,*args,**kwargs): | |
if not '@' in url: | |
url = self.fix_url(url) | |
return requests.post(url,*args,**kwargs).json() | |
def get(self,url,*args,**kwargs): | |
if not '@' in url: | |
url = self.fix_url(url) | |
return requests.get(url,*args,**kwargs).json() | |
def request(self,call,*args,**kwargs): | |
method = None | |
func = self.get | |
if 'method' in kwargs: | |
method = kwargs.pop('method') | |
if method is not None: | |
if method.lower() == 'post': | |
if 'params' in kwargs: | |
pass | |
else: | |
kwargs['params'] = {} | |
func = self.post | |
url = self._base_url + call | |
url = url + '.json' if not args else url + '/' + '/'.join(map(str,args)) + '.json' | |
return self.get(url,**kwargs) | |
def fix_url(self,url): | |
start,end = url.split('//') | |
fmt = '{}:{}@'.format(self._user,self._password) | |
fmt = '{}//' + fmt + '{}' | |
return fmt.format(start,end) | |
def init_app(self,app): | |
app.extensions['basecamp_api'] = self | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
from time import sleep | |
from basecamp_api import BasecampApi | |
class RThread(threading.Thread): | |
def __init__(self,func): | |
threading.Thread.__init__(self) | |
self.func = func | |
def run(self): | |
self.result = self.func() | |
def get_projects(api): | |
projects = RThread(func=lambda: api.request('projects')) | |
projects.start() | |
while projects.is_alive(): | |
sleep(.01) | |
return projects.result | |
def get_project(api,num,async=False): | |
project = RThread(func=lambda: api.request('projects',num)) | |
project.start() | |
if not async: | |
while project.is_alive(): | |
sleep(.01) | |
return project.result | |
return project | |
def get_full_projects(api): | |
projects = get_projects(api) | |
tmp = [] | |
rtn = [] | |
for project in projects: | |
tmp.append(get_project(api,project['id'],async=True)) | |
for itm in tmp: | |
itm.join() | |
rtn.append(itm.result) | |
return rtn | |
def get_topics(api,project_num): | |
project = get_project(api,project_num) | |
topics = RThread(func=lambda: api.get(project['topics']['url'])) | |
topics.start() | |
while topics.is_alive(): | |
sleep(.01) | |
return topics.result | |
def get_messages(api,project_num): | |
topics = get_topics(api,project_num) | |
msgs = [] | |
rtn = [] | |
for itm in topics: | |
curr = RThread(func=lambda: api.get(itm['topicable']['url'])) | |
msgs.append(curr) | |
curr.start() | |
for msg in msgs: | |
msg.join() | |
rtn.append(msg.result) | |
return rtn | |
def get_todolists(api,project_num): | |
project = get_project(api,project_num) | |
todolists = RThread(func=lambda: api.get(project.get('todolists').get('url'))) | |
todolists.start() | |
while todolists.is_alive(): | |
sleep(.01) | |
return todolists.result | |
def make_request(api): | |
projects = RThread(func=lambda: api.request('projects')) | |
projects.start() | |
while projects.is_alive(): | |
sleep(.01) | |
projects = projects.result | |
full_projects = RThread(func=lambda: [api.get(project['url']) for project in projects]) | |
full_projects.start() | |
while full_projects.is_alive(): | |
sleep(.01) | |
full_projects = full_projects.result | |
topics = RThread(func=lambda: { p['name'] : api.get(p['topics']['url']) for p in full_projects }) | |
topics.start() | |
while topics.is_alive(): | |
sleep(.01) | |
topics = topics.result | |
msgs = [] | |
rtn = [] | |
for itm in topics: | |
for topic in topics[itm]: | |
curr = RThread(func=lambda: api.get(topic['topicable']['url'])) | |
msgs.append(curr) | |
curr.start() | |
for msg in msgs: | |
msg.join() | |
rtn.append(msg.result) | |
return rtn | |
if __name__ == "__main__": | |
api = BasecampApi(user=USERNAME,password=PASSWORD,acct=BASECAMP_ACCT_NUM) | |
print make_request(api) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment