-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.py
129 lines (97 loc) · 3.24 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from time import sleep
from flask import Flask, request, render_template, jsonify
from boxview import BoxViewClient
from boxviewerror import BoxViewError
app = Flask(__name__)
NO_EXPIRE = 'no_expire'
THE_FUTURE = '4013-12-23T05:21:09.697Z'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/desktop-upload', methods=['POST'])
def desktop_upload_document():
"""
"""
print str(request.form)
box_view_client = BoxViewClient()
uploaded_file = request.files['file']
try:
document = box_view_client.multipart_upload_document(uploaded_file)
except(BoxViewError):
return jsonify({'error': 'an error occurred'}), 400
document_id = document.json()['id']
print 'Document ID is {}'.format(document_id)
return jsonify(document.json())
@app.route('/url-upload', methods=['POST'])
def url_upload_document():
"""
"""
box_view_client = BoxViewClient()
document_url = request.form['document-url']
try:
document = box_view_client.upload_document(document_url)
except(BoxViewError):
return jsonify({'error': 'an error occurred'}), 400
document_id = document.json()['id']
print 'Document ID is {}'.format(document_id)
return jsonify(document.json())
@app.route('/sessions', methods=['POST'])
def create_session():
"""
"""
sleep(2)
box_view_client = BoxViewClient()
document_id = request.form['document_id']
should_expire = request.form['expire']
if should_expire == NO_EXPIRE:
expires_at = THE_FUTURE
else:
expires_at = None
try:
api_response = box_view_client.create_session(document_id, expires_at, True)
except(BoxViewError):
return jsonify({'error': 'an error occurred'}), 400
if api_response.status_code == 202:
return jsonify({'status': 'much undone'}), 202
return jsonify(api_response.json())
@app.route('/webhook', methods=['POST', 'GET'])
def webhook_success():
print request.data
return "OK"
@app.route('/test-webhook', methods=['POST', 'GET'])
def test_webhook_success():
print '##### BEGIN TEST ####\n'
print request.data
print '##### END TEST ####\n'
return "OK"
def create_session_for_document(document_id,
box_view_client,
expires_at=None):
"""
Given a box_view_client and document_id,
creates a session for that document by polling
the session endpoint
"""
# The approximate conversion time for logging purposes
wait_time = 0
while True:
sleep(1)
wait_time += 1
try:
api_response = box_view_client.create_session(document_id,
expires_at)
if api_response.status_code == 201:
session = api_response.json()
break
except(BoxViewError):
return jsonify({'error': 'an error occurred'}), 400
session_url = box_view_client.create_session_url(session['id'])
print 'Session is {}'.format(session_url)
combined_response = {
'session_url': session_url,
'session': session
}
return combined_response
if __name__ == '__main__':
app.debug = True
app.run()