-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubic.py
122 lines (95 loc) · 4.96 KB
/
hubic.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
import base64
import requests
from requests.exceptions import HTTPError
import urllib
import json
class BadRequest(Exception):
"""An invalid request was submitted."""
class Unauthorized(Exception):
"""The provided email address and password were incorrect."""
class DestinationAlreadyExist(Exception):
"""Destination folder already exist"""
class Hubic:
def __init__(self, app_key, app_secret, redirect_uri,refresh_token=None, prefix_destination="/"):
self.app_key = app_key
self.app_secret = app_secret
self.prefix_destination = prefix_destination
if refresh_token == None:
url = "https://api.hubic.com/oauth/auth/?client_id=" + app_key + "&redirect_uri=" + urllib.quote_plus(redirect_uri) + "&scope=credentials.r&response_type=code"
print "Go on " + url
code = raw_input("And enter your code here : ")
application_token = base64.b64encode(app_key + ":" + app_secret)
url = "https://api.hubic.com/oauth/token/"
response = requests.post( url, data={ "code": code, "grant_type": "authorization_code", "redirect_uri": redirect_uri},
headers={'Authorization': 'Basic ' + application_token })
if response.status_code == 400:
raise BadRequest(response.content)
elif not response.ok:
# Unauthorized
raise Unauthorized(response.json())
self.hubic_refresh_token = response.json()["refresh_token"]
self.hubic_token = response.json()["access_token"]
else:
self.hubic_refresh_token = refresh_token
self.hubic_token = "falsetoken"
#Get openstacks credentials
self.openstack_token = self.get_openstack_credentials()
#Create prefix destination
try:
prefix_exist = self._os_call('get','default'+self.prefix_destination)
if prefix_exist.status_code == 200:
raise DestinationAlreadyExist()
except HTTPError as e:
if e.response.status_code == 404:
self._os_call('put','default'+self.prefix_destination,None,{'Content-Type':'application/directory'});
pass
else:
raise
def _renew_token(self, method, uri, data, headers, limit):
application_token = base64.b64encode(self.app_key + ":" + self.app_secret)
url = "https://api.hubic.com/oauth/token/"
response = requests.post( url, data={ "grant_type": "refresh_token", "refresh_token": self.hubic_refresh_token},headers={'Authorization': 'Basic ' + application_token })
self.hubic_token = response.json()["access_token"]
return self._hubic_call(method, uri, data, headers, limit)
def _hubic_call(self, method, uri, data=None, headers={}, limit=3):
req = getattr(requests, method.lower())
headers['Authorization'] = "Bearer " + self.hubic_token
url = 'https://api.hubic.com/1.0/' + uri
result = req(url, headers=headers, data=data)
if result.status_code == 401 and result.json()['error'] == 'invalid_token':
if limit > 0:
limit = limit - 1
return self._renew_token(method,uri,data,headers,limit)
else:
result.raise_for_status()
return result
def _os_renew_token(self, method, uri, data, headers, limit):
self.get_openstack_credentials()
return self._os_call(method, uri, data, headers, limit)
def _os_call(self, method, uri, data=None, headers={}, limit=3):
req = getattr(requests, method.lower())
headers['X-Auth-Token'] = self.os_token
url = self.os_endpoint + '/' + uri
result = req(url, headers=headers, data=data)
if result.status_code == 403:
if limit > 0:
limit = limit - 1
return self._os_renew_token(method,uri,data,headers,limit)
else:
result.raise_for_status()
return result
def get_openstack_credentials(self):
openstack_token = self._hubic_call('GET', 'account/credentials').json()
self.os_endpoint = openstack_token['endpoint']
self.os_token = openstack_token['token']
def create_folder(self,name):
return self._os_call('put','default'+self.prefix_destination +name,None,{'Content-Type':'application/directory'});
def upload_object(self,name,data):
return self._os_call('put','default'+self.prefix_destination +name,data);
def create_manifest_big_file(self,manifest_link,name):
headers = { 'X-Object-Manifest': 'default_segments/' + manifest_link}
return self._os_call('put','default'+self.prefix_destination +name,data=None,headers=headers);
def upload_segment_big_file(self,manifest_link,number,content):
return self._os_call('put','default_segments/'+manifest_link + '/' + number,data=content);
def delete_object(self,name):
return self._os_call('delete','default'+name);