-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidate.py
More file actions
245 lines (203 loc) · 6.31 KB
/
validate.py
File metadata and controls
245 lines (203 loc) · 6.31 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from typing import Any, Dict, List, Union, cast, overload
from typing_extensions import NotRequired, TypedDict
from ._config import ClientConfig
from ._types import BaseResponse
from .async_request import AsyncRequest, AsyncRequestConfig
from .helpers import build_path
from .request import Request, RequestConfig
class Spam(TypedDict):
is_spam: bool
score: float
class SpamCheckParams(TypedDict):
text: Union[str, List[str]]
class SpamCheckResponse(BaseResponse):
check: Union[Spam, List[Spam]]
class SpellCheckParams(TypedDict):
text: str
language_code: NotRequired[str]
class Misspelling(TypedDict):
word: Union[str, None]
startIndex: int
endIndex: int
expected: List[str]
auto_corrected: bool
class SpellCheckResponse(BaseResponse):
misspellings_found: bool
misspellings: List[Misspelling]
auto_correct_text: str
class ProfanityParams(TypedDict):
text: str
censor_replacement: NotRequired[str]
class Profanity(TypedDict):
profanity: Union[str, None]
startIndex: int
endIndex: int
class ProfanityResponse(BaseResponse):
message: str
clean_text: str
profanities: List[Profanity]
profanities_found: bool
class NSFWParams(TypedDict):
url: NotRequired[str]
file_store_key: NotRequired[str]
class NSFWResponse(BaseResponse):
nsfw: bool
nudity: bool
gore: bool
nsfw_score: float
nudity_score: float
gore_score: float
class Validate(ClientConfig):
config: RequestConfig
def __init__(
self,
api_key: str,
base_url: str,
headers: Union[Dict[str, str], None] = None,
):
super().__init__(api_key, base_url, headers)
self.config = RequestConfig(
base_url=base_url,
api_key=api_key,
headers=headers,
)
@overload
def nsfw(self, params: NSFWParams) -> NSFWResponse: ...
@overload
def nsfw(self, blob: bytes, options: NSFWParams = None) -> NSFWResponse: ...
def nsfw(
self,
blob: Union[NSFWParams, bytes],
options: NSFWParams = None,
) -> NSFWResponse:
path = "/validate/nsfw"
options = options or {}
if isinstance(
blob, dict
): # If params is provided as a dict, we assume it's the first argument
resp = Request(
config=self.config,
path="/validate/nsfw",
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp
files = {"file": blob}
resp = Request(
config=self.config,
path=path,
params=options,
files=files,
verb="post",
).perform_with_content()
return resp
def profanity(self, params: ProfanityParams) -> ProfanityResponse:
path = build_path(
base_path="/validate/profanity",
params=params,
)
resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp
def spellcheck(self, params: SpellCheckParams) -> SpellCheckResponse:
path = build_path(
base_path="/validate/spell_check",
params=params,
)
resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp
def spamcheck(self, params: SpamCheckParams) -> SpamCheckResponse:
path = "/validate/spam_check"
resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp
class AsyncValidate(ClientConfig):
config: AsyncRequestConfig
def __init__(
self,
api_key: str,
base_url: str,
headers: Union[Dict[str, str], None] = None,
):
super().__init__(api_key, base_url, headers)
self.config = AsyncRequestConfig(
base_url=base_url,
api_key=api_key,
headers=headers,
)
@overload
async def nsfw(self, params: NSFWParams) -> NSFWResponse: ...
@overload
async def nsfw(self, blob: bytes, options: NSFWParams = None) -> NSFWResponse: ...
async def nsfw(
self,
blob: Union[NSFWParams, bytes],
options: NSFWParams = None,
) -> NSFWResponse:
path = "/validate/nsfw"
options = options or {}
if isinstance(
blob, dict
): # If params is provided as a dict, we assume it's the first argument
resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp
files = {"file": blob}
resp = await AsyncRequest(
config=self.config,
path=path,
params=options,
files=files,
verb="post",
).perform_with_content()
return resp
async def profanity(self, params: ProfanityParams) -> ProfanityResponse:
path = build_path(
base_path="/validate/profanity",
params=params,
)
resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp
async def spellcheck(self, params: SpellCheckParams) -> SpellCheckResponse:
path = build_path(
base_path="/validate/spell_check",
params=params,
)
resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp
async def spamcheck(self, params: SpamCheckParams) -> SpamCheckResponse:
path = "/validate/spam_check"
resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp