11# -*- coding: utf-8 -*-
2+
3+ from syncano .exceptions import SyncanoRequestError
4+
25from . import fields
36from .base import Instance , Model , logger
47
@@ -31,24 +34,51 @@ class Meta:
3134 }
3235
3336 def upload_file (self , path , file ):
37+ """
38+ Upload a new file to the hosting.
39+ :param path: the file path;
40+ :param file: the file to be uploaded;
41+ :return: the response from the API;
42+ """
3443 files_path = self .links .files
3544 data = {'path' : path }
3645 connection = self ._get_connection ()
37- params = connection .build_params (params = {})
38- headers = params ['headers' ]
39- headers .pop ('content-type' )
40- response = connection .session .post (connection .host + files_path , headers = headers ,
46+ headers = self ._prepare_header (connection )
47+ response = connection .session .post ('{}{}' .format (connection .host , files_path ), headers = headers ,
4148 data = data , files = [('file' , file )])
4249 if response .status_code != 201 :
4350 logger .error (response .text )
4451 return
45- return response
52+ return HostingFile (** response .json ())
53+
54+ def update_file (self , path , file ):
55+ """
56+ Updates an existing file.
57+ :param path: the file path;
58+ :param file: the file to be uploaded;
59+ :return: the response from the API;
60+ """
61+ hosting_files = self ._get_files ()
62+ is_found = False
63+ for hosting_file in hosting_files :
64+ if hosting_file .path == path :
65+ is_found = True
66+ break
67+
68+ if not is_found :
69+ raise SyncanoRequestError ('File with path {} not found.' .format (path ))
4670
47- def list_files (self ):
48- files_path = self .links .files
4971 connection = self ._get_connection ()
50- response = connection .request ('GET' , files_path )
51- return [f ['path' ] for f in response ['objects' ]]
72+ headers = self ._prepare_header (connection )
73+ response = connection .session .patch ('{}{}' .format (connection .host , hosting_file .links .self ), headers = headers ,
74+ files = [('file' , file )])
75+ if response .status_code != 200 :
76+ logger .error (response .text )
77+ return
78+ return HostingFile (** response .json ())
79+
80+ def list_files (self ):
81+ return self ._get_files ()
5282
5383 def set_default (self ):
5484 default_path = self .links .set_default
@@ -57,3 +87,35 @@ def set_default(self):
5787 response = connection .make_request ('POST' , default_path )
5888 self .to_python (response )
5989 return self
90+
91+ def _prepare_header (self , connection ):
92+ params = connection .build_params (params = {})
93+ headers = params ['headers' ]
94+ headers .pop ('content-type' )
95+ return headers
96+
97+ def _get_files (self ):
98+ return [hfile for hfile in HostingFile .please .list (hosting_id = self .id )]
99+
100+
101+ class HostingFile (Model ):
102+ """
103+ OO wrapper around hosting file.
104+ """
105+
106+ path = fields .StringField (max_length = 300 )
107+ file = fields .FileField ()
108+ links = fields .LinksField ()
109+
110+ class Meta :
111+ parent = Hosting
112+ endpoints = {
113+ 'detail' : {
114+ 'methods' : ['delete' , 'get' , 'put' , 'patch' ],
115+ 'path' : '/files/{id}/' ,
116+ },
117+ 'list' : {
118+ 'methods' : ['post' , 'get' ],
119+ 'path' : '/files/' ,
120+ }
121+ }
0 commit comments