pangea-sdk 3.8.0__py3-none-any.whl → 3.8.0b2__py3-none-any.whl

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.
@@ -1,12 +1,10 @@
1
1
  # Copyright 2022 Pangea Cyber Corporation
2
2
  # Author: Pangea Cyber Corporation
3
- from __future__ import annotations
4
3
 
5
4
  from typing import Dict, List, Optional, Union
6
5
 
7
6
  import pangea.services.redact as m
8
7
  from pangea.asyncio.services.base import ServiceBaseAsync
9
- from pangea.config import PangeaConfig
10
8
  from pangea.response import PangeaResponse
11
9
 
12
10
 
@@ -37,24 +35,7 @@ class RedactAsync(ServiceBaseAsync):
37
35
 
38
36
  service_name = "redact"
39
37
 
40
- def __init__(
41
- self, token: str, config: PangeaConfig | None = None, logger_name: str = "pangea", config_id: str | None = None
42
- ) -> None:
43
- """
44
- Redact client
45
-
46
- Initializes a new Redact client.
47
-
48
- Args:
49
- token: Pangea API token.
50
- config: Configuration.
51
- logger_name: Logger name.
52
-
53
- Examples:
54
- config = PangeaConfig(domain="pangea_domain")
55
- redact = RedactAsync(token="pangea_token", config=config)
56
- """
57
-
38
+ def __init__(self, token, config=None, logger_name="pangea", config_id: Optional[str] = None):
58
39
  super().__init__(token, config, logger_name, config_id=config_id)
59
40
 
60
41
  async def redact(
@@ -0,0 +1,185 @@
1
+ # Copyright 2022 Pangea Cyber Corporation
2
+ # Author: Pangea Cyber Corporation
3
+ import io
4
+ from typing import List, Optional, Tuple
5
+
6
+ import pangea.services.sanitize as m
7
+ from pangea.asyncio.services.base import ServiceBaseAsync
8
+ from pangea.response import PangeaResponse, TransferMethod
9
+ from pangea.utils import FileUploadParams, get_file_upload_params
10
+
11
+
12
+ class SanitizeAsync(ServiceBaseAsync):
13
+ """Sanitize service client.
14
+
15
+ Examples:
16
+ import os
17
+
18
+ # Pangea SDK
19
+ from pangea.config import PangeaConfig
20
+ from pangea.asyncio.services import Sanitize
21
+
22
+ PANGEA_SANITIZE_TOKEN = os.getenv("PANGEA_SANITIZE_TOKEN")
23
+ config = PangeaConfig(domain="pangea.cloud")
24
+
25
+ sanitize = Sanitize(token=PANGEA_SANITIZE_TOKEN, config=config)
26
+ """
27
+
28
+ service_name = "sanitize"
29
+
30
+ async def sanitize(
31
+ self,
32
+ transfer_method: TransferMethod = TransferMethod.POST_URL,
33
+ file_path: Optional[str] = None,
34
+ file: Optional[io.BufferedReader] = None,
35
+ source_url: Optional[str] = None,
36
+ share_id: Optional[str] = None,
37
+ file_scan: Optional[m.SanitizeFile] = None,
38
+ content: Optional[m.SanitizeContent] = None,
39
+ share_output: Optional[m.SanitizeShareOutput] = None,
40
+ size: Optional[int] = None,
41
+ crc32c: Optional[str] = None,
42
+ sha256: Optional[str] = None,
43
+ uploaded_file_name: Optional[str] = None,
44
+ sync_call: bool = True,
45
+ ) -> PangeaResponse[m.SanitizeResult]:
46
+ """
47
+ Sanitize
48
+
49
+ Apply file sanitization actions according to specified rules.
50
+ [**Beta API**](https://pangea.cloud/docs/sdk/python/#beta-releases).
51
+
52
+ OperationId: sanitize_post_v1beta_sanitize
53
+
54
+ Args:
55
+ transfer_method: The transfer method used to upload the file data.
56
+ file_path: Path to file to sanitize.
57
+ file: File to sanitize.
58
+ source_url: A URL where the file to be sanitized can be downloaded.
59
+ share_id: A Pangea Secure Share ID where the file to be sanitized is stored.
60
+ file_scan: Options for File Scan.
61
+ content: Options for how the file should be sanitized.
62
+ share_output: Integration with Secure Share.
63
+ size: The size (in bytes) of the file. If the upload doesn't match, the call will fail.
64
+ crc32c: The CRC32C hash of the file data, which will be verified by the server if provided.
65
+ sha256: The hexadecimal-encoded SHA256 hash of the file data, which will be verified by the server if provided.
66
+ uploaded_file_name: Name of the user-uploaded file, required for `TransferMethod.PUT_URL` and `TransferMethod.POST_URL`.
67
+ sync_call: Whether or not to poll on HTTP/202.
68
+
69
+ Raises:
70
+ PangeaAPIException: If an API error happens.
71
+
72
+ Returns:
73
+ The sanitized file and information on the sanitization that was
74
+ performed.
75
+
76
+ Examples:
77
+ with open("/path/to/file.pdf", "rb") as f:
78
+ response = await sanitize.sanitize(
79
+ file=f,
80
+ transfer_method=TransferMethod.POST_URL,
81
+ uploaded_file_name="uploaded_file",
82
+ )
83
+ """
84
+
85
+ if file or file_path:
86
+ if file_path:
87
+ file = open(file_path, "rb")
88
+ if transfer_method == TransferMethod.POST_URL and (sha256 is None or crc32c is None or size is None):
89
+ params = get_file_upload_params(file) # type: ignore[arg-type]
90
+ crc32c = params.crc_hex if crc32c is None else crc32c
91
+ sha256 = params.sha256_hex if sha256 is None else sha256
92
+ size = params.size if size is None else size
93
+ else:
94
+ crc32c, sha256, size = None, None, None
95
+ files: List[Tuple] = [("upload", ("filename", file, "application/octet-stream"))]
96
+ else:
97
+ raise ValueError("Need to set file_path or file arguments")
98
+
99
+ input = m.SanitizeRequest(
100
+ transfer_method=transfer_method,
101
+ source_url=source_url,
102
+ share_id=share_id,
103
+ file=file_scan,
104
+ content=content,
105
+ share_output=share_output,
106
+ crc32c=crc32c,
107
+ sha256=sha256,
108
+ size=size,
109
+ uploaded_file_name=uploaded_file_name,
110
+ )
111
+ data = input.dict(exclude_none=True)
112
+ response = await self.request.post(
113
+ "v1beta/sanitize", m.SanitizeResult, data=data, files=files, poll_result=sync_call
114
+ )
115
+ if file_path and file is not None:
116
+ file.close()
117
+ return response
118
+
119
+ async def request_upload_url(
120
+ self,
121
+ transfer_method: TransferMethod = TransferMethod.PUT_URL,
122
+ params: Optional[FileUploadParams] = None,
123
+ file_scan: Optional[m.SanitizeFile] = None,
124
+ content: Optional[m.SanitizeContent] = None,
125
+ share_output: Optional[m.SanitizeShareOutput] = None,
126
+ size: Optional[int] = None,
127
+ crc32c: Optional[str] = None,
128
+ sha256: Optional[str] = None,
129
+ uploaded_file_name: Optional[str] = None,
130
+ ) -> PangeaResponse[m.SanitizeResult]:
131
+ """
132
+ Sanitize via presigned URL
133
+
134
+ Apply file sanitization actions according to specified rules via a
135
+ [presigned URL](https://pangea.cloud/docs/api/presigned-urls).
136
+ [**Beta API**](https://pangea.cloud/docs/sdk/python/#beta-releases).
137
+
138
+ OperationId: sanitize_post_v1beta_sanitize 2
139
+
140
+ Args:
141
+ transfer_method: The transfer method used to upload the file data.
142
+ params: File upload parameters.
143
+ file_scan: Options for File Scan.
144
+ content: Options for how the file should be sanitized.
145
+ share_output: Integration with Secure Share.
146
+ size: The size (in bytes) of the file. If the upload doesn't match, the call will fail.
147
+ crc32c: The CRC32C hash of the file data, which will be verified by the server if provided.
148
+ sha256: The hexadecimal-encoded SHA256 hash of the file data, which will be verified by the server if provided.
149
+ uploaded_file_name: Name of the user-uploaded file, required for `TransferMethod.PUT_URL` and `TransferMethod.POST_URL`.
150
+
151
+ Raises:
152
+ PangeaAPIException: If an API error happens.
153
+
154
+ Returns:
155
+ A presigned URL.
156
+
157
+ Examples:
158
+ presignedUrl = await sanitize.request_upload_url(
159
+ transfer_method=TransferMethod.PUT_URL,
160
+ uploaded_file_name="uploaded_file",
161
+ )
162
+
163
+ # Upload file to `presignedUrl.accepted_result.put_url`.
164
+
165
+ # Poll for Sanitize's result.
166
+ response: PangeaResponse[SanitizeResult] = await sanitize.poll_result(response=presignedUrl)
167
+ """
168
+
169
+ input = m.SanitizeRequest(
170
+ transfer_method=transfer_method,
171
+ file=file_scan,
172
+ content=content,
173
+ share_output=share_output,
174
+ crc32c=crc32c,
175
+ sha256=sha256,
176
+ size=size,
177
+ uploaded_file_name=uploaded_file_name,
178
+ )
179
+ if params is not None and (transfer_method == TransferMethod.POST_URL):
180
+ input.crc32c = params.crc_hex
181
+ input.sha256 = params.sha256_hex
182
+ input.size = params.size
183
+
184
+ data = input.dict(exclude_none=True)
185
+ return await self.request.request_presigned_url("v1beta/sanitize", m.SanitizeResult, data=data)