-
Notifications
You must be signed in to change notification settings - Fork 2
/
api-sample.py
113 lines (95 loc) · 3.35 KB
/
api-sample.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
try:
import json
except ImportError:
import simplejson as json
import requests
import os
headers = {'content-type': 'application/json'}
def read_in_chunks(file_object, chunk_size=10485760):
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def login():
global headers
payload = {"apiKey": "XXXX"}
response = requests.request("POST", "/auth/api-key", data=json.dumps(payload), headers=headers)
jsonResponse = response.json()
headers = {'content-type': 'application/json', 'Authorization': 'Bearer '+jsonResponse['access_token']}
def video():
global headers
payload = {"title": "Test"}
response = requests.request("POST", "/videos", data=json.dumps(payload), headers=headers)
jsonResponse = response.json()
return jsonResponse
def patch(videoJson):
global headers
if 'content-type' not in headers:
headers['content-type'] = 'application/json'
videoId = videoJson['videoId']
payload = {"description": "Test description"}
response = requests.request("PATCH", "/videos/"+videoId, data=json.dumps(payload), headers=headers)
jsonResponse = response.json()
return jsonResponse
def upload(videoJson):
global headers
uriUpload = videoJson['source']['uri']
file = {'file': open('/path/to/source.mp4', 'rb')}
if 'content-type' in headers:
del headers['content-type']
response = requests.request("POST", uriUpload, files=file, headers=headers)
jsonResponse = response.json()
videoId = jsonResponse['videoId']
return videoId
def uploadByChunk(videoJson):
global headers
uriUpload = videoJson['source']['uri']
path = '/path/to/source.mp4'
if 'content-type' in headers:
del headers['content-type']
content_size = os.stat(path).st_size
headers['Expect'] = '100-Continue'
headers['Content-Type'] = 'application/octet-stream'
headers['Content-Disposition'] = 'form-data'
file = open(path)
index=0
offset=0
for chunk in read_in_chunks(file):
offset = index + len(chunk)
headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset, content_size)
index = offset
try:
response = requests.request("POST", uriUpload, files={'file': chunk}, headers=headers)
print response.status_code
print response.content
except Exception, e:
print e
def getVideo(videoId):
global headers
if 'content-type' not in headers:
headers['content-type'] = 'application/json'
response = requests.request("GET", "/videos/"+videoId, headers=headers)
jsonResponse = response.json()
def deleteVideo(videoId):
global headers
if 'content-type' not in headers:
headers['content-type'] = 'application/json'
response = requests.request("DELETE", "/videos/"+videoId, headers=headers)
def listWithoutPaginate():
global headers
if 'content-type' not in headers:
headers['content-type'] = 'application/json'
response = requests.request("GET", "/videos", headers=headers)
def listWithPaginate():
global headers
if 'content-type' not in headers:
headers['content-type'] = 'application/json'
paginate = {'page': 2, 'pageSize': 25}
response = requests.request("GET", "/videos", params=paginate, headers=headers)
def listWithPaginateAndOrder():
global headers
if 'content-type' not in headers:
headers['content-type'] = 'application/json'
paginateOrder = {'page': 2, 'pageSize': 25, 'sortBy': 'publishedAt', 'sortOrder': 'desc'}
response = requests.request("GET", "/videos", params=paginateOrder, headers=headers)