|
| 1 | + |
| 2 | +import sys, os |
| 3 | +import unittest |
| 4 | +import urllib2 |
| 5 | +from oauthlib.common import urlencode, urldecode |
| 6 | +from oauthlib import oauth1 |
| 7 | +from oauthlib.oauth1 import SIGNATURE_PLAINTEXT, SIGNATURE_TYPE_AUTH_HEADER, SIGNATURE_TYPE_BODY, SIGNATURE_TYPE_QUERY |
| 8 | +import requests |
| 9 | +import json |
| 10 | +import urllib |
| 11 | +from urlparse import parse_qs |
| 12 | + |
| 13 | +#BASE_URL = "http://10.30.239.237:8080/oauth" |
| 14 | +BASE_URL = 'http://localhost:8080/v1' |
| 15 | +BASE_URL_OAUTH = "http://10.30.233.214:8080/oauth" |
| 16 | +CLIENT_KEY = "b3af4e669daf880fb16563e6f36051b105188d413" |
| 17 | +CLIENT_SECRET = "c168e65c18d75b35d8999b534a3776cf" |
| 18 | +REQUEST_TOKEN_ENDPOINT = "/request_token" |
| 19 | +ACCESS_TOKEN_ENDPOINT = "/access_token" |
| 20 | +STACKSYNC_AUTHORIZE_ENDPOINT = "/authorize" |
| 21 | + |
| 22 | + |
| 23 | +client = oauth1.Client(CLIENT_KEY, |
| 24 | + client_secret=CLIENT_SECRET, |
| 25 | + signature_type=SIGNATURE_TYPE_QUERY, |
| 26 | + signature_method=SIGNATURE_PLAINTEXT, |
| 27 | + resource_owner_key='GmOFBjhSzC0A2rxrnM6gQWKnm6m3Ew', |
| 28 | + resource_owner_secret='RtA8hMYzgxnpoMxd8kBUtVQmcSvM4D') |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +# Main definition - constants |
| 34 | +menu_actions = {} |
| 35 | + |
| 36 | +# ======================= |
| 37 | +# MENUS FUNCTIONS |
| 38 | +# ======================= |
| 39 | + |
| 40 | +# Main menu |
| 41 | +def main_menu(): |
| 42 | + os.system('clear') |
| 43 | + |
| 44 | + print "Welcome,\n" |
| 45 | + print "Please choose the menu you want to start:" |
| 46 | + print "1. Menu 1" |
| 47 | + print "\n0. Quit" |
| 48 | + choice = raw_input(" >> ") |
| 49 | + exec_menu(choice) |
| 50 | + |
| 51 | + return |
| 52 | + |
| 53 | +# Execute menu |
| 54 | +def exec_menu(choice): |
| 55 | + os.system('clear') |
| 56 | + ch = choice.lower() |
| 57 | + if ch == '': |
| 58 | + menu_actions['main_menu']() |
| 59 | + else: |
| 60 | + try: |
| 61 | + menu_actions[ch]() |
| 62 | + except KeyError: |
| 63 | + print "Invalid selection, please try again.\n" |
| 64 | + menu_actions['main_menu']() |
| 65 | + return |
| 66 | + |
| 67 | +# Menu 1 |
| 68 | +def menu1(): |
| 69 | + while True: |
| 70 | + print "Hello Menu 1 !\n" |
| 71 | + print "2. Create new file" |
| 72 | + print "3. Update file data" |
| 73 | + print "4. Update file metadata" |
| 74 | + print "5. Get metadata file" |
| 75 | + print "6. Get file data (caution with the length of the file)" |
| 76 | + print "7. Create new folder" |
| 77 | + print "8. Share folder" |
| 78 | + print "9. Unshare folder" |
| 79 | + print "10. Update folder metadata" |
| 80 | + print "11. Delete a file" |
| 81 | + print "12. Get folder" |
| 82 | + print "0. Quit" |
| 83 | + choice = raw_input(" >> ") |
| 84 | + exec_menu(choice) |
| 85 | + return |
| 86 | + |
| 87 | +# Back to main menu |
| 88 | +def back(): |
| 89 | + menu_actions['main_menu']() |
| 90 | + |
| 91 | +def create_new_file(): |
| 92 | + name = raw_input("File name: ") |
| 93 | + if name: |
| 94 | + parent = raw_input("Parent id: ") |
| 95 | + if parent: |
| 96 | + url = BASE_URL +"/file?name="+name+"&parent="+parent |
| 97 | + else: |
| 98 | + url = BASE_URL +"/file?name="+name |
| 99 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 100 | + |
| 101 | + content_file = raw_input("Content file: ") |
| 102 | + headers['StackSync-API'] = "v2" |
| 103 | + headers['Content-Type'] = "text/plain" |
| 104 | + |
| 105 | + r = requests.post(uri,content_file, headers=headers) |
| 106 | + print 'response', r |
| 107 | + print 'response', r.text |
| 108 | + else: |
| 109 | + print 'Can not create a new file without name' |
| 110 | + |
| 111 | +def update_file_data(): |
| 112 | + file_id = raw_input("File id: ") |
| 113 | + url = BASE_URL +'/file/'+str(file_id)+'/data' |
| 114 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 115 | + content_file = raw_input("Content file: ") |
| 116 | + headers['StackSync-API'] = "v2" |
| 117 | + |
| 118 | + headers['Content-Type'] = "text/plain" |
| 119 | + r = requests.put(uri,content_file, headers=headers) |
| 120 | + print 'response', r |
| 121 | + print 'response', r.text |
| 122 | + |
| 123 | +def update_file_metadata(): |
| 124 | + file_id = raw_input("File id: ") |
| 125 | + url = BASE_URL +'/file/'+str(file_id) |
| 126 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 127 | + new_name = raw_input("New name: ") |
| 128 | + new_parent = raw_input("New parent id: ") |
| 129 | + if not new_name and not new_parent: |
| 130 | + print 'Can not update metadata without any parameter' |
| 131 | + else: |
| 132 | + if not new_parent: |
| 133 | + parameters = {"name":str(new_name)} |
| 134 | + elif not name: |
| 135 | + parameters = {"parent":new_parent} |
| 136 | + else: |
| 137 | + parameters = {"name":str(new_name), "parent":new_parent} |
| 138 | + headers['StackSync-API'] = "v2" |
| 139 | + headers['Content-Type'] = "application/json" |
| 140 | + r = requests.put(uri, json.dumps(parameters), headers=headers) |
| 141 | + print 'response', r |
| 142 | + print 'response', r.text |
| 143 | + |
| 144 | +def get_metadata_file(): |
| 145 | + file_id = raw_input("File id: ") |
| 146 | + url = BASE_URL +'/file/'+str(file_id) |
| 147 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 148 | + headers['StackSync-API'] = "v2" |
| 149 | + headers['Content-Type'] = "application/json" |
| 150 | + r = requests.get(uri, headers=headers) |
| 151 | + print 'response', r |
| 152 | + print 'response', r.text |
| 153 | + |
| 154 | +def get_file_data(): |
| 155 | + file_id = raw_input("File id: ") |
| 156 | + url = BASE_URL +'/file/'+str(file_id)+'/data' |
| 157 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 158 | + headers['StackSync-API'] = "v2" |
| 159 | + headers['Content-Type'] = "application/json" |
| 160 | + r = requests.get(uri, headers=headers) |
| 161 | + print 'response', r |
| 162 | + print 'response', r.text |
| 163 | +def create_new_folder(): |
| 164 | + url = BASE_URL +'/folder' |
| 165 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 166 | + new_name = raw_input("Name: ") |
| 167 | + new_parent = raw_input("Parent id: ") |
| 168 | + if not new_name: |
| 169 | + print 'Can not create folder without name' |
| 170 | + else: |
| 171 | + if not new_parent: |
| 172 | + parameters = {"name":str(new_name)} |
| 173 | + else: |
| 174 | + parameters = {"name":str(new_name), "parent":new_parent} |
| 175 | + headers['StackSync-API'] = "v2" |
| 176 | + headers['Content-Type'] = "application/json" |
| 177 | + r = requests.post(uri, json.dumps(parameters), headers=headers) |
| 178 | + print 'response', r |
| 179 | + print 'response', r.text |
| 180 | +def share_folder(): |
| 181 | + None |
| 182 | + |
| 183 | +def unshare_folder(): |
| 184 | + None |
| 185 | + |
| 186 | +def update_folder_metadata(): |
| 187 | + folder_id = raw_input("Folder id: ") |
| 188 | + url = BASE_URL +'/folder/'+str(folder_id) |
| 189 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 190 | + new_name = raw_input("New name: ") |
| 191 | + new_parent = raw_input("New parent id: ") |
| 192 | + if not new_name and not new_parent: |
| 193 | + print 'Can not update metadata without any parameter' |
| 194 | + else: |
| 195 | + if not new_parent: |
| 196 | + parameters = {"name":str(new_name)} |
| 197 | + elif not name: |
| 198 | + parameters = {"parent":new_parent} |
| 199 | + else: |
| 200 | + parameters = {"name":str(new_name), "parent":new_parent} |
| 201 | + headers['StackSync-API'] = "v2" |
| 202 | + |
| 203 | + headers['Content-Type'] = "application/json" |
| 204 | + r = requests.put(uri,json.dumps(parameters), headers=headers) |
| 205 | + print 'response', r |
| 206 | + print 'response', r.text |
| 207 | + |
| 208 | +def delete_file(): |
| 209 | + file_id = raw_input("File id: ") |
| 210 | + url = BASE_URL +'/file/'+str(file_id) |
| 211 | + uri, headers, _ = client.sign(url, http_method='GET') |
| 212 | + headers['StackSync-API'] = "v2" |
| 213 | + headers['Content-Type'] = "text/plain" |
| 214 | + r = requests.delete(uri, headers=headers) |
| 215 | + print 'response', r |
| 216 | + print 'response', r.text |
| 217 | + |
| 218 | +def get_folder(): |
| 219 | + folder_id = raw_input("Folder id: ") |
| 220 | + url =BASE_URL+'/folder/' + str(folder_id) |
| 221 | + #url ='http://localhost:8080/v1/file/578/data' |
| 222 | + #url ='http://localhost:8080/v1/file?name=tiririri.txt' |
| 223 | + |
| 224 | + uri, headers, _ = client.sign(url, |
| 225 | + http_method='GET') |
| 226 | + headers['StackSync-API'] = "v2" |
| 227 | + |
| 228 | + headers['Content-Type'] = "text/plain" |
| 229 | + r = requests.get(uri, headers=headers) |
| 230 | + print 'response status', r |
| 231 | + print 'response', r.text |
| 232 | + |
| 233 | + |
| 234 | +# Exit program |
| 235 | +def exit(): |
| 236 | + sys.exit() |
| 237 | + |
| 238 | +# ======================= |
| 239 | +# MENUS DEFINITIONS |
| 240 | +# ======================= |
| 241 | + |
| 242 | +# Menu definition |
| 243 | +menu_actions = { |
| 244 | + 'main_menu': main_menu, |
| 245 | + '1': menu1, |
| 246 | + '2': create_new_file, |
| 247 | + '3': update_file_data, |
| 248 | + '4': update_file_metadata, |
| 249 | + '5': get_metadata_file, |
| 250 | + '6': get_file_data, |
| 251 | + '7': create_new_folder, |
| 252 | + '8': share_folder, |
| 253 | + '9': unshare_folder, |
| 254 | + '10': update_folder_metadata, |
| 255 | + '11': delete_file, |
| 256 | + '12': get_folder, |
| 257 | + '0': exit, |
| 258 | +} |
| 259 | + |
| 260 | +# ======================= |
| 261 | +# MAIN PROGRAM |
| 262 | +# ======================= |
| 263 | + |
| 264 | +# Main Program |
| 265 | +if __name__ == "__main__": |
| 266 | + # Launch main menu |
| 267 | + main_menu() |
0 commit comments