filesystempack 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ /shelf/
2
+ /workspace.xml
3
+
4
+ .idea
5
+
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.4
2
+ Name: filesystempack
3
+ Version: 0.1.0
4
+ Summary: File system to handle connection with microsevice
5
+ Project-URL: Repository, https://github.com/gufi2115/filesystem-integration
6
+ Project-URL: Microservice_repository, https://github.com/gufi2115/Microservice-to-save-files
7
+ Author-email: buber2006xdd@gmail.com
8
+ License: MIT
9
+ Keywords: Connection,File handling,Microservice
10
+ Classifier: Programming Language :: Python
11
+ Requires-Python: >=3.10
12
+ Requires-Dist: niquests
13
+ Requires-Dist: python-magic
14
+ Description-Content-Type: text/markdown
15
+
16
+ # filesystem-integration
17
+
18
+ Biblioteka służy do obsługi plików z mikroserwisu do twojego projektu.
19
+
20
+ ## Instalacja
21
+
22
+ Postaw kontenery z mikroserwisem
23
+ https://github.com/gufi2115/Microservice-to-save-files
24
+
25
+ ``pip install filesystemlib``
26
+
27
+ ## Jak używać
28
+
29
+
30
+ ```from filesystemlib.filesystem import Filesystem```
31
+
32
+ Storzenie instancji na przykład w helpers.py
33
+
34
+ ```file_system = Filesystem(url="http://host:6767/api/file/", api_key=twój_api_key_z_mikroserwisu)```
35
+
36
+ POST
37
+
38
+ ``uuid = file_system.save_file(file=plik, content_type=mime_type_opcjonalnie)``
39
+
40
+ GET
41
+
42
+ ``file, content_type = file_system.get_file(uuid)``
43
+
44
+ DELETE
45
+
46
+ ``delete = file_system.delete_file(uuid)``
47
+
48
+ status
49
+
50
+ ``status = file_system.status``
@@ -0,0 +1,35 @@
1
+ # filesystem-integration
2
+
3
+ Biblioteka służy do obsługi plików z mikroserwisu do twojego projektu.
4
+
5
+ ## Instalacja
6
+
7
+ Postaw kontenery z mikroserwisem
8
+ https://github.com/gufi2115/Microservice-to-save-files
9
+
10
+ ``pip install filesystemlib``
11
+
12
+ ## Jak używać
13
+
14
+
15
+ ```from filesystemlib.filesystem import Filesystem```
16
+
17
+ Storzenie instancji na przykład w helpers.py
18
+
19
+ ```file_system = Filesystem(url="http://host:6767/api/file/", api_key=twój_api_key_z_mikroserwisu)```
20
+
21
+ POST
22
+
23
+ ``uuid = file_system.save_file(file=plik, content_type=mime_type_opcjonalnie)``
24
+
25
+ GET
26
+
27
+ ``file, content_type = file_system.get_file(uuid)``
28
+
29
+ DELETE
30
+
31
+ ``delete = file_system.delete_file(uuid)``
32
+
33
+ status
34
+
35
+ ``status = file_system.status``
File without changes
@@ -0,0 +1,87 @@
1
+ import niquests
2
+ import magic
3
+ from urllib3.exceptions import MaxRetryError, NewConnectionError
4
+
5
+
6
+ class FileSystem:
7
+ def __init__(self, url, api_key):
8
+ self.url = url
9
+ self.api_key = api_key
10
+ self._status = None
11
+ self._content_type = None
12
+
13
+ def _api_key_headers(self):
14
+ return {'Authorization': f'api-key {self.api_key}'}
15
+
16
+ @property
17
+ def status(self):
18
+ return self._status
19
+
20
+ def get_file(self, uuid):
21
+ file, status = self.__requests_handling(method='get',uuid=uuid, stream=True)
22
+ self._status = status
23
+ return file, self._content_type
24
+
25
+ def save_file(self, file, content_type=None):
26
+ if not content_type:
27
+ if hasattr(file, 'content_type'):
28
+ content_type = file.content_type
29
+ else:
30
+ file.seek(0)
31
+ content_type = magic.Magic(mime=True).from_buffer(file.read(2048))
32
+ file.seek(0)
33
+ data = {
34
+ 'file': (file.name, file, content_type),
35
+ }
36
+ uuid, status = self.__requests_handling(method='post', data=data)
37
+ self._status = status
38
+ return uuid
39
+
40
+ def delete_file(self, uuid):
41
+ status = self.__requests_handling(method='delete', uuid=uuid)
42
+ self._status = status
43
+
44
+ def __requests_handling(self, method, uuid=None, data=None, stream=None):
45
+ headers = self._api_key_headers()
46
+ url = self.url
47
+ if uuid:
48
+ url = self.url + f"{uuid}" + '/'
49
+ if hasattr(niquests, method):
50
+ try:
51
+ r = getattr(niquests, method)(url=url, headers=headers, files=data, stream=stream)
52
+ status = r.status_code
53
+ if data and status == 201:
54
+ return str(r.json()['id']), status
55
+ elif uuid and status == 200:
56
+ self._content_type = r.headers.get('content-type')
57
+ file = r.raw
58
+ return file, status
59
+ elif uuid and status == 204:
60
+ return status
61
+ r.raise_for_status()
62
+ except niquests.HTTPError as e:
63
+ if method == 'delete':
64
+ return f"{e.response.status_code} {e.response.reason}"
65
+ return None, f"{e.response.status_code} {e.response.reason}"
66
+ except niquests.ConnectionError as e:
67
+ if method == 'delete':
68
+ return f"Connection Error: {e}"
69
+ return None, f"Connection Error: {e}"
70
+ except MaxRetryError as e:
71
+ if method == 'delete':
72
+ return f"Max Retry Error: {e}"
73
+ return None, f"Max Retry Error: {e}"
74
+ except NewConnectionError as e:
75
+ if method == 'delete':
76
+ return f"New Connection Error: {e}"
77
+ return None, f"New Connection Error: {e}"
78
+ except niquests.RequestException as e:
79
+ if method == 'delete':
80
+ return f"Request Exception: {e}"
81
+ return None, f"Request Exception: {e}"
82
+ except Exception as e:
83
+ if method == 'delete':
84
+ return f"Exception: {e}"
85
+ return None, f"Exception: {e}"
86
+ else:
87
+ return None, f"niquests have not {method} method"
@@ -0,0 +1,23 @@
1
+ [build-system]
2
+ requires = ["hatchling >= 1.27"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "filesystempack"
7
+ version = "0.1.0"
8
+ description = "File system to handle connection with microsevice"
9
+ authors = [
10
+ {username = "gufi2115", email="buber2006xdd@gmail.com"}
11
+ ]
12
+ dependencies = [
13
+ "niquests",
14
+ "python-magic",
15
+ ]
16
+ readme = "README.md"
17
+ license = {text="MIT"}
18
+ requires-python = ">=3.10"
19
+ keywords = ["Microservice", "Connection", "File handling"]
20
+ classifiers = ["Programming Language :: Python"]
21
+ [project.urls]
22
+ Repository = "https://github.com/gufi2115/filesystem-integration"
23
+ Microservice_repository = "https://github.com/gufi2115/Microservice-to-save-files"
File without changes
@@ -0,0 +1,29 @@
1
+ from filesystemlib.filesystem import FileSystem
2
+ from pathlib import Path
3
+
4
+ class TestFileSystem:
5
+ def test_api_key_headers(self):
6
+ file_system = FileSystem(url='http://100.64.0.1:6767/api/file/', api_key='32533b40-50e0-4267-8069-6e2af0944a88')
7
+ api_key = file_system._api_key_headers()
8
+ assert 'api-key 32533b40-50e0-4267-8069-6e2af0944a88' == api_key['Authorization']
9
+
10
+ def test_save_file(self):
11
+ base_dir = Path(__file__).resolve().parent.parent
12
+ file = open(Path(base_dir / 'tests/test_files/test_file.png'), 'rb')
13
+ file_system= FileSystem(url='http://100.64.0.1:6767/api/file/', api_key="32533b40-50e0-4267-8069-6e2af0944a88")
14
+ save_file_uuid = file_system.save_file(file=file)
15
+ print(save_file_uuid, file_system.status)
16
+ assert save_file_uuid is not None
17
+ assert file_system.status == 201
18
+
19
+ def test_get_file(self):
20
+ file_system = FileSystem(url='http://100.64.0.1:6767/api/file/', api_key='32533b40-50e0-4267-8069-6e2af0944a88')
21
+ file, content_type= file_system.get_file(uuid='0b0718d3-b3ee-445d-a0a4-fa1fca56fb58')
22
+ assert file is not None
23
+ assert content_type == 'image/png'
24
+ assert file_system.status == 200
25
+
26
+ def test_delete_file(self):
27
+ file_system = FileSystem(url='http://100.64.0.1:6767/api/file/', api_key='32533b40-50e0-4267-8069-6e2af0944a88')
28
+ file_system.delete_file(uuid='0b0718d3-b3ee-445d-a0a4-fa1fca56fb58')
29
+ assert file_system.status == 204