blob: 5415a65fc9dafd8e161f0fd7aa33128b6495e6cd [file] [log] [blame]
Joe Gregorio0c73a672010-10-14 08:27:59 -04001from apiclient.discovery import build
Joe Gregoriofffa7d72011-02-18 17:20:39 -05002from apiclient.discovery import build
3from apiclient.errors import HttpError
4from apiclient.ext.authtools import run
5from apiclient.ext.file import Storage
6from apiclient.oauth import CredentialsInvalidError
7from apiclient.oauth import FlowThreeLegged
Joe Gregorio0c73a672010-10-14 08:27:59 -04008
9import Queue
10import httplib2
Joe Gregorio0c73a672010-10-14 08:27:59 -040011import threading
12import time
13
14# Uncomment to get detailed logging
15# httplib2.debuglevel = 4
16
17
18NUM_THREADS = 4
19NUM_ITEMS = 40
20
21queue = Queue.Queue()
22
23
24class Backoff:
25 """Exponential Backoff
26
27 Implements an exponential backoff algorithm.
28 """
Joe Gregorioaf276d22010-12-09 14:26:58 -050029
Joe Gregorio0c73a672010-10-14 08:27:59 -040030 def __init__(self, maxretries=8):
31 self.retry = 0
32 self.maxretries = maxretries
33 self.first = True
34
35 def loop(self):
36 if self.first:
37 self.first = False
38 return True
39 else:
40 return self.retry < self.maxretries
41
42 def fail(self):
43 self.retry += 1
Joe Gregorioaf276d22010-12-09 14:26:58 -050044 delay = 2 ** self.retry
Joe Gregorio0c73a672010-10-14 08:27:59 -040045 time.sleep(delay)
46
47
48def start_threads(credentials):
49 # Start up NUM_THREADS to handle requests
50
51 def process_requests():
52 http = httplib2.Http()
53 http = credentials.authorize(http)
Joe Gregoriofffa7d72011-02-18 17:20:39 -050054 credentials_ok = True
Joe Gregorio0c73a672010-10-14 08:27:59 -040055
Joe Gregoriofffa7d72011-02-18 17:20:39 -050056 while credentials_ok:
Joe Gregorio0c73a672010-10-14 08:27:59 -040057 request = queue.get()
58 backoff = Backoff()
59 while backoff.loop():
60 try:
61 request.execute(http)
62 break
63 except HttpError, e:
64 if e.resp.status in [402, 403, 408, 503, 504]:
65 print "Increasing backoff, got status code: %d" % e.resp.status
66 backoff.fail()
Joe Gregoriofffa7d72011-02-18 17:20:39 -050067 except CredentialsInvalidError:
68 print "Credentials no long valid. Exiting."
69 credentials_ok = False
70 break
Joe Gregorio0c73a672010-10-14 08:27:59 -040071
72 print "Completed request"
73 queue.task_done()
74
Joe Gregoriofffa7d72011-02-18 17:20:39 -050075
Joe Gregorio0c73a672010-10-14 08:27:59 -040076 for i in range(NUM_THREADS):
77 t = threading.Thread(target=process_requests)
78 t.daemon = True
79 t.start()
80
Joe Gregorio0c73a672010-10-14 08:27:59 -040081
Joe Gregorioaf276d22010-12-09 14:26:58 -050082def main():
Joe Gregoriofffa7d72011-02-18 17:20:39 -050083 storage = Storage('moderator.dat')
84 credentials = storage.get()
85 if credentials is None or credentials.invalid == True:
86 moderator_discovery = build("moderator", "v1").auth_discovery()
87
88 flow = FlowThreeLegged(moderator_discovery,
89 consumer_key='anonymous',
90 consumer_secret='anonymous',
Joe Gregorioccc79542011-02-19 00:05:26 -050091 user_agent='python-threading-sample/1.0',
Joe Gregoriofffa7d72011-02-18 17:20:39 -050092 domain='anonymous',
93 scope='https://www.googleapis.com/auth/moderator',
94 xoauth_displayname='Google API Client Example App')
95
96 credentials = run(flow, storage)
Joe Gregorio0c73a672010-10-14 08:27:59 -040097
98 start_threads(credentials)
99
100 http = httplib2.Http()
101 http = credentials.authorize(http)
102
Joe Gregorio1ae3e742011-02-25 15:17:14 -0500103 service = build("moderator", "v1", http=http)
Joe Gregorio0c73a672010-10-14 08:27:59 -0400104
105 series_body = {
Joe Gregorio913e70d2010-11-05 15:38:23 -0400106 "data": {
107 "description": "An example of bulk creating topics",
108 "name": "Using threading and queues",
109 "videoSubmissionAllowed": False
110 }
Joe Gregorio0c73a672010-10-14 08:27:59 -0400111 }
Joe Gregoriofffa7d72011-02-18 17:20:39 -0500112 try:
Joe Gregorio1ae3e742011-02-25 15:17:14 -0500113 series = service.series().insert(body=series_body).execute()
Joe Gregoriofffa7d72011-02-18 17:20:39 -0500114 print "Created a new series"
Joe Gregorio0c73a672010-10-14 08:27:59 -0400115
Joe Gregoriofffa7d72011-02-18 17:20:39 -0500116 for i in range(NUM_ITEMS):
117 topic_body = {
118 "data": {
119 "description": "Sample Topic # %d" % i,
120 "name": "Sample",
121 "presenter": "me"
122 }
Joe Gregorio0c73a672010-10-14 08:27:59 -0400123 }
Joe Gregorioafdf50b2011-03-08 09:41:52 -0500124 topic_request = service.topics().insert(
125 seriesId=series['id']['seriesId'], body=topic_body)
Joe Gregoriofffa7d72011-02-18 17:20:39 -0500126 print "Adding request to queue"
127 queue.put(topic_request)
128 except CredentialsInvalidError:
129 print 'Your credentials are no longer valid.'
130 print 'Please re-run this application to re-authorize.'
131
Joe Gregorio0c73a672010-10-14 08:27:59 -0400132
133 queue.join()
134
135
136if __name__ == "__main__":
137 main()