-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhosting.py
More file actions
122 lines (102 loc) · 3.63 KB
/
hosting.py
File metadata and controls
122 lines (102 loc) · 3.63 KB
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
# -*- coding: utf-8 -*-
from . import fields
from .base import Model
from .instances import Instance
class Hosting(Model):
"""
OO wrapper around hosting.
"""
name = fields.StringField(max_length=253)
is_default = fields.BooleanField(read_only=True)
is_active = fields.BooleanField(default=True)
description = fields.StringField(read_only=False, required=False)
domains = fields.ListField(default=[])
links = fields.LinksField()
created_at = fields.DateTimeField(read_only=True, required=False)
updated_at = fields.DateTimeField(read_only=True, required=False)
class Meta:
parent = Instance
endpoints = {
'detail': {
'methods': ['delete', 'get', 'put', 'patch'],
'path': '/hosting/{id}/',
},
'list': {
'methods': ['post', 'get'],
'path': '/hosting/',
}
}
def upload_file(self, path, file):
"""
Upload a new file to the hosting.
:param path: the file path;
:param file: the file to be uploaded;
:return: the response from the API;
"""
files_path = self.links.files
data = {'path': path}
connection = self._get_connection()
headers = self._prepare_header(connection)
response = connection.session.post('{}{}'.format(connection.host, files_path), headers=headers,
data=data, files=[('file', file)])
if response.status_code != 201:
return
return HostingFile(**response.json())
def update_file(self, path, file):
"""
Updates an existing file.
:param path: the file path;
:param file: the file to be uploaded;
:return: the response from the API;
"""
hosting_files = self._get_files()
is_found = False
for hosting_file in hosting_files:
if hosting_file.path == path:
is_found = True
break
if not is_found:
# create if not found;
hosting_file = self.upload_file(path, file)
return hosting_file
connection = self._get_connection()
headers = self._prepare_header(connection)
response = connection.session.patch('{}{}'.format(connection.host, hosting_file.links.self), headers=headers,
files=[('file', file)])
if response.status_code != 200:
return
return HostingFile(**response.json())
def list_files(self):
return self._get_files()
def set_default(self):
default_path = self.links.set_default
connection = self._get_connection()
response = connection.make_request('POST', default_path)
self.to_python(response)
return self
def _prepare_header(self, connection):
params = connection.build_params(params={})
headers = params['headers']
headers.pop('content-type')
return headers
def _get_files(self):
return [hfile for hfile in HostingFile.please.list(hosting_id=self.id)]
class HostingFile(Model):
"""
OO wrapper around hosting file.
"""
path = fields.StringField(max_length=300)
file = fields.FileField()
links = fields.LinksField()
class Meta:
parent = Hosting
endpoints = {
'detail': {
'methods': ['delete', 'get', 'put', 'patch'],
'path': '/files/{id}/',
},
'list': {
'methods': ['post', 'get'],
'path': '/files/',
}
}