api.py 7.13 KB
Newer Older
1 2 3
from django.db import models
from django.http import Http404
import json, requests, time
Őry Máté committed
4
from cloud.settings import store_settings as settings
5 6 7 8 9 10 11 12 13 14 15 16 17

# Create your models here.
#TODO Handle exceptions locally
class StoreApi:
#    store_url = 'https://store.cloud.ik.bme.hu'
#    store_url = 'http://store.cloud.ik.bme.hu:8080'
#    store_client_cert = '/opt/webadmin/cloud/client.crt'
#    store_client_key = ''/opt/webadmin/cloud/client.key
#    store_user = 'admin'
#    store_password = 'IQu8Eice'
#    ssl_auth = True
#    verify_ssl = False
    @staticmethod
18 19 20
    def get_host():
        return settings['store_host']
    @staticmethod
21 22 23 24
    def post_request(url, payload):
        headers = {'content-type': 'application/json'}
        if settings['ssl_auth'] == 'True' and settings['basic_auth'] == 'True':
            r = requests.post(url, data=payload, headers=headers,  \
x committed
25
            verify = settings['verify_ssl']=='True', \
26 27 28 29 30
            cert=(settings['store_client_cert'], settings['store_client_key']), \
            auth=(settings['store_client_user'], settings['store_client_pass'])
            )
        elif settings['ssl_auth'] == 'True':
            r = requests.post(url, data=payload, headers=headers,  \
x committed
31
            verify = settings['verify_ssl']=='True', \
32 33 34 35
            cert=(settings['store_client_cert'], settings['store_client_key']) \
            )
        elif settings['basic_auth'] == 'True':
            r = requests.post(url, data=payload, headers=headers,  \
x committed
36
            verify = settings['verify_ssl']=='True', \
37 38 39 40
            auth=(settings['store_client_user'], settings['store_client_pass']) \
            )
        else:
            r = requests.post(url, data=payload, headers=headers,  \
x committed
41
            verify = settings['verify_ssl']=='True' \
42 43 44 45 46 47 48
            )
        return r
    @staticmethod
    def get_request(url):
        headers = {'content-type': 'application/json'}
        if settings['ssl_auth'] == 'True' and settings['basic_auth'] == 'True':
            r = requests.get(url, headers=headers,  \
x committed
49
            verify = settings['verify_ssl']=='True', \
50 51 52 53 54
            cert=(settings['store_client_cert'], settings['store_client_key']), \
            auth=(settings['store_client_user'], settings['store_client_pass'])
            )
        elif settings['ssl_auth'] == 'True':
            r = requests.get(url, headers=headers,  \
x committed
55
            verify = settings['verify_ssl']=='True', \
56 57 58 59
            cert=(settings['store_client_cert'], settings['store_client_key']) \
            )
        elif settings['basic_auth'] == 'True':
            r = requests.get(url, headers=headers,  \
x committed
60
            verify = settings['verify_ssl']=='True', \
61 62 63 64
            auth=(settings['store_client_user'], settings['store_client_pass']) \
            )
        else:
            r = requests.get(url, headers=headers,  \
x committed
65
            verify = settings['verify_ssl']=='True' \
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            )
        return r
    @staticmethod
    def listfolder(neptun, path):
        url = settings['store_url']+'/'+neptun
        payload = json.dumps({ 'CMD' : 'LIST', 'PATH' : path })
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            tupplelist = json.loads(r.content)
            for item in tupplelist:
                item['MTIME'] = time.ctime(item['MTIME'])
            return tupplelist
        else:
            raise Http404
    @staticmethod
81 82 83 84 85 86 87 88 89 90 91 92
    def toplist(neptun):
        url = settings['store_url']+'/'+neptun
        payload = json.dumps({ 'CMD' : 'TOPLIST'})
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            tupplelist = json.loads(r.content)
            for item in tupplelist:
                item['MTIME'] = time.ctime(item['MTIME'])
            return tupplelist
        else:
            raise Http404
    @staticmethod
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 130
    def requestdownload(neptun, path):
        url = settings['store_url']+'/'+neptun
        payload = json.dumps({ 'CMD' : 'DOWNLOAD', 'PATH' : path })
        r = StoreApi.post_request(url, payload)
        response = json.loads(r.content)
        return response['LINK']
    @staticmethod
    def requestupload(neptun, path):
        url = settings['store_url']+'/'+neptun
        payload = json.dumps({ 'CMD' : 'UPLOAD', 'PATH' : path })
        headers = {'content-type': 'application/json'}
        r = StoreApi.post_request(url, payload)
        response = json.loads(r.content)
        if r.status_code == requests.codes.ok:
            return response['LINK']
        else:
            raise Http404
    @staticmethod
    def requestremove(neptun, path):
        url = settings['store_url']+'/'+neptun
        payload = json.dumps({ 'CMD' : 'REMOVE', 'PATH' : path })
        headers = {'content-type': 'application/json'}
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            return True
        else:
            return False
    @staticmethod
    def requestnewfolder(neptun, path):
        url = settings['store_url']+'/'+neptun
        payload = json.dumps({ 'CMD' : 'NEW_FOLDER', 'PATH' : path })
        headers = {'content-type': 'application/json'}
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            return True
        else:
            return False
    @staticmethod
Dányi Bence committed
131 132 133 134 135 136 137 138 139 140
    def requestrename(neptun, old_path, new_name):
        url = settings['store_url']+'/'+neptun
        payload = json.dumps({'CMD':'RENAME','NEW_NAME':new_name,'PATH':old_path})
        headers = {'content-type': 'application/json'}
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            return True
        else:
            return False
    @staticmethod
x committed
141 142 143 144 145 146 147 148
    def requestquota(neptun):
        url = settings['store_url']+'/'+neptun
        r = StoreApi.get_request(url)
        if r.status_code == requests.codes.ok:
            return json.loads(r.content)
        else:
            return False
    @staticmethod
tarokkk committed
149 150 151 152 153 154 155 156 157
    def set_quota(neptun, quota):
        url = settings['store_url']+'/quota/'+neptun
        payload = json.dumps({ 'QUOTA' : quota })
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            return True
        else:
            return False
    @staticmethod
158 159 160 161 162 163 164 165
    def userexist(neptun):
        url = settings['store_url']+'/'+neptun
        r = StoreApi.get_request(url)
        if r.status_code == requests.codes.ok:
            return True
        else:
            return False
    @staticmethod
tarokkk committed
166
    def createuser(neptun, password, key_list, quota):
167
        url = settings['store_url']+'/new/'+neptun
tarokkk committed
168
        payload = json.dumps({ 'SMBPASSWD' : password, 'KEYS' : key_list, 'QUOTA' : quota })
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            return True
        else:
            return False
    @staticmethod
    def updateauthorizationinfo(neptun, password, key_list):
        url = settings['store_url']+'/set/'+neptun
        payload = json.dumps({ 'SMBPASSWD' : password, 'KEYS' : key_list })
        r = StoreApi.post_request(url, payload)
        if r.status_code == requests.codes.ok:
            return True
        else:
            return False