schemathesis 3.25.6__py3-none-any.whl → 4.0.0a1__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.
- schemathesis/__init__.py +27 -65
- schemathesis/auths.py +102 -82
- schemathesis/checks.py +126 -46
- schemathesis/cli/__init__.py +11 -1760
- schemathesis/cli/__main__.py +4 -0
- schemathesis/cli/commands/__init__.py +37 -0
- schemathesis/cli/commands/run/__init__.py +662 -0
- schemathesis/cli/commands/run/checks.py +80 -0
- schemathesis/cli/commands/run/context.py +117 -0
- schemathesis/cli/commands/run/events.py +35 -0
- schemathesis/cli/commands/run/executor.py +138 -0
- schemathesis/cli/commands/run/filters.py +194 -0
- schemathesis/cli/commands/run/handlers/__init__.py +46 -0
- schemathesis/cli/commands/run/handlers/base.py +18 -0
- schemathesis/cli/commands/run/handlers/cassettes.py +494 -0
- schemathesis/cli/commands/run/handlers/junitxml.py +54 -0
- schemathesis/cli/commands/run/handlers/output.py +746 -0
- schemathesis/cli/commands/run/hypothesis.py +105 -0
- schemathesis/cli/commands/run/loaders.py +129 -0
- schemathesis/cli/{callbacks.py → commands/run/validation.py} +103 -174
- schemathesis/cli/constants.py +5 -52
- schemathesis/cli/core.py +17 -0
- schemathesis/cli/ext/fs.py +14 -0
- schemathesis/cli/ext/groups.py +55 -0
- schemathesis/cli/{options.py → ext/options.py} +39 -10
- schemathesis/cli/hooks.py +36 -0
- schemathesis/contrib/__init__.py +1 -3
- schemathesis/contrib/openapi/__init__.py +1 -3
- schemathesis/contrib/openapi/fill_missing_examples.py +3 -5
- schemathesis/core/__init__.py +58 -0
- schemathesis/core/compat.py +25 -0
- schemathesis/core/control.py +2 -0
- schemathesis/core/curl.py +58 -0
- schemathesis/core/deserialization.py +65 -0
- schemathesis/core/errors.py +370 -0
- schemathesis/core/failures.py +285 -0
- schemathesis/core/fs.py +19 -0
- schemathesis/{_lazy_import.py → core/lazy_import.py} +1 -0
- schemathesis/core/loaders.py +104 -0
- schemathesis/core/marks.py +66 -0
- schemathesis/{transports/content_types.py → core/media_types.py} +17 -13
- schemathesis/core/output/__init__.py +69 -0
- schemathesis/core/output/sanitization.py +197 -0
- schemathesis/core/rate_limit.py +60 -0
- schemathesis/core/registries.py +31 -0
- schemathesis/{internal → core}/result.py +1 -1
- schemathesis/core/transforms.py +113 -0
- schemathesis/core/transport.py +108 -0
- schemathesis/core/validation.py +38 -0
- schemathesis/core/version.py +7 -0
- schemathesis/engine/__init__.py +30 -0
- schemathesis/engine/config.py +59 -0
- schemathesis/engine/context.py +119 -0
- schemathesis/engine/control.py +36 -0
- schemathesis/engine/core.py +157 -0
- schemathesis/engine/errors.py +394 -0
- schemathesis/engine/events.py +337 -0
- schemathesis/engine/phases/__init__.py +66 -0
- schemathesis/{runner → engine/phases}/probes.py +50 -67
- schemathesis/engine/phases/stateful/__init__.py +65 -0
- schemathesis/engine/phases/stateful/_executor.py +326 -0
- schemathesis/engine/phases/stateful/context.py +85 -0
- schemathesis/engine/phases/unit/__init__.py +174 -0
- schemathesis/engine/phases/unit/_executor.py +321 -0
- schemathesis/engine/phases/unit/_pool.py +74 -0
- schemathesis/engine/recorder.py +241 -0
- schemathesis/errors.py +31 -0
- schemathesis/experimental/__init__.py +18 -14
- schemathesis/filters.py +103 -14
- schemathesis/generation/__init__.py +21 -37
- schemathesis/generation/case.py +190 -0
- schemathesis/generation/coverage.py +931 -0
- schemathesis/generation/hypothesis/__init__.py +30 -0
- schemathesis/generation/hypothesis/builder.py +585 -0
- schemathesis/generation/hypothesis/examples.py +50 -0
- schemathesis/generation/hypothesis/given.py +66 -0
- schemathesis/generation/hypothesis/reporting.py +14 -0
- schemathesis/generation/hypothesis/strategies.py +16 -0
- schemathesis/generation/meta.py +115 -0
- schemathesis/generation/modes.py +28 -0
- schemathesis/generation/overrides.py +96 -0
- schemathesis/generation/stateful/__init__.py +20 -0
- schemathesis/{stateful → generation/stateful}/state_machine.py +68 -81
- schemathesis/generation/targets.py +69 -0
- schemathesis/graphql/__init__.py +15 -0
- schemathesis/graphql/checks.py +115 -0
- schemathesis/graphql/loaders.py +131 -0
- schemathesis/hooks.py +99 -67
- schemathesis/openapi/__init__.py +13 -0
- schemathesis/openapi/checks.py +412 -0
- schemathesis/openapi/generation/__init__.py +0 -0
- schemathesis/openapi/generation/filters.py +63 -0
- schemathesis/openapi/loaders.py +178 -0
- schemathesis/pytest/__init__.py +5 -0
- schemathesis/pytest/control_flow.py +7 -0
- schemathesis/pytest/lazy.py +273 -0
- schemathesis/pytest/loaders.py +12 -0
- schemathesis/{extra/pytest_plugin.py → pytest/plugin.py} +106 -127
- schemathesis/python/__init__.py +0 -0
- schemathesis/python/asgi.py +12 -0
- schemathesis/python/wsgi.py +12 -0
- schemathesis/schemas.py +537 -261
- schemathesis/specs/graphql/__init__.py +0 -1
- schemathesis/specs/graphql/_cache.py +25 -0
- schemathesis/specs/graphql/nodes.py +1 -0
- schemathesis/specs/graphql/scalars.py +7 -5
- schemathesis/specs/graphql/schemas.py +215 -187
- schemathesis/specs/graphql/validation.py +11 -18
- schemathesis/specs/openapi/__init__.py +7 -1
- schemathesis/specs/openapi/_cache.py +122 -0
- schemathesis/specs/openapi/_hypothesis.py +146 -165
- schemathesis/specs/openapi/checks.py +565 -67
- schemathesis/specs/openapi/converter.py +33 -6
- schemathesis/specs/openapi/definitions.py +11 -18
- schemathesis/specs/openapi/examples.py +139 -23
- schemathesis/specs/openapi/expressions/__init__.py +37 -2
- schemathesis/specs/openapi/expressions/context.py +4 -6
- schemathesis/specs/openapi/expressions/extractors.py +23 -0
- schemathesis/specs/openapi/expressions/lexer.py +20 -18
- schemathesis/specs/openapi/expressions/nodes.py +38 -14
- schemathesis/specs/openapi/expressions/parser.py +26 -5
- schemathesis/specs/openapi/formats.py +45 -0
- schemathesis/specs/openapi/links.py +65 -165
- schemathesis/specs/openapi/media_types.py +32 -0
- schemathesis/specs/openapi/negative/__init__.py +7 -3
- schemathesis/specs/openapi/negative/mutations.py +24 -8
- schemathesis/specs/openapi/parameters.py +46 -30
- schemathesis/specs/openapi/patterns.py +137 -0
- schemathesis/specs/openapi/references.py +47 -57
- schemathesis/specs/openapi/schemas.py +478 -369
- schemathesis/specs/openapi/security.py +25 -7
- schemathesis/specs/openapi/serialization.py +11 -6
- schemathesis/specs/openapi/stateful/__init__.py +185 -73
- schemathesis/specs/openapi/utils.py +6 -1
- schemathesis/transport/__init__.py +104 -0
- schemathesis/transport/asgi.py +26 -0
- schemathesis/transport/prepare.py +99 -0
- schemathesis/transport/requests.py +221 -0
- schemathesis/{_xml.py → transport/serialization.py} +143 -28
- schemathesis/transport/wsgi.py +165 -0
- schemathesis-4.0.0a1.dist-info/METADATA +297 -0
- schemathesis-4.0.0a1.dist-info/RECORD +152 -0
- {schemathesis-3.25.6.dist-info → schemathesis-4.0.0a1.dist-info}/WHEEL +1 -1
- {schemathesis-3.25.6.dist-info → schemathesis-4.0.0a1.dist-info}/entry_points.txt +1 -1
- schemathesis/_compat.py +0 -74
- schemathesis/_dependency_versions.py +0 -17
- schemathesis/_hypothesis.py +0 -246
- schemathesis/_override.py +0 -49
- schemathesis/cli/cassettes.py +0 -375
- schemathesis/cli/context.py +0 -58
- schemathesis/cli/debug.py +0 -26
- schemathesis/cli/handlers.py +0 -16
- schemathesis/cli/junitxml.py +0 -43
- schemathesis/cli/output/__init__.py +0 -1
- schemathesis/cli/output/default.py +0 -790
- schemathesis/cli/output/short.py +0 -44
- schemathesis/cli/sanitization.py +0 -20
- schemathesis/code_samples.py +0 -149
- schemathesis/constants.py +0 -55
- schemathesis/contrib/openapi/formats/__init__.py +0 -9
- schemathesis/contrib/openapi/formats/uuid.py +0 -15
- schemathesis/contrib/unique_data.py +0 -41
- schemathesis/exceptions.py +0 -560
- schemathesis/extra/_aiohttp.py +0 -27
- schemathesis/extra/_flask.py +0 -10
- schemathesis/extra/_server.py +0 -17
- schemathesis/failures.py +0 -209
- schemathesis/fixups/__init__.py +0 -36
- schemathesis/fixups/fast_api.py +0 -41
- schemathesis/fixups/utf8_bom.py +0 -29
- schemathesis/graphql.py +0 -4
- schemathesis/internal/__init__.py +0 -7
- schemathesis/internal/copy.py +0 -13
- schemathesis/internal/datetime.py +0 -5
- schemathesis/internal/deprecation.py +0 -34
- schemathesis/internal/jsonschema.py +0 -35
- schemathesis/internal/transformation.py +0 -15
- schemathesis/internal/validation.py +0 -34
- schemathesis/lazy.py +0 -361
- schemathesis/loaders.py +0 -120
- schemathesis/models.py +0 -1234
- schemathesis/parameters.py +0 -86
- schemathesis/runner/__init__.py +0 -570
- schemathesis/runner/events.py +0 -329
- schemathesis/runner/impl/__init__.py +0 -3
- schemathesis/runner/impl/core.py +0 -1035
- schemathesis/runner/impl/solo.py +0 -90
- schemathesis/runner/impl/threadpool.py +0 -400
- schemathesis/runner/serialization.py +0 -411
- schemathesis/sanitization.py +0 -248
- schemathesis/serializers.py +0 -323
- schemathesis/service/__init__.py +0 -18
- schemathesis/service/auth.py +0 -11
- schemathesis/service/ci.py +0 -201
- schemathesis/service/client.py +0 -100
- schemathesis/service/constants.py +0 -38
- schemathesis/service/events.py +0 -57
- schemathesis/service/hosts.py +0 -107
- schemathesis/service/metadata.py +0 -46
- schemathesis/service/models.py +0 -49
- schemathesis/service/report.py +0 -255
- schemathesis/service/serialization.py +0 -199
- schemathesis/service/usage.py +0 -65
- schemathesis/specs/graphql/loaders.py +0 -344
- schemathesis/specs/openapi/filters.py +0 -49
- schemathesis/specs/openapi/loaders.py +0 -667
- schemathesis/specs/openapi/stateful/links.py +0 -92
- schemathesis/specs/openapi/validation.py +0 -25
- schemathesis/stateful/__init__.py +0 -133
- schemathesis/targets.py +0 -45
- schemathesis/throttling.py +0 -41
- schemathesis/transports/__init__.py +0 -5
- schemathesis/transports/auth.py +0 -15
- schemathesis/transports/headers.py +0 -35
- schemathesis/transports/responses.py +0 -52
- schemathesis/types.py +0 -35
- schemathesis/utils.py +0 -169
- schemathesis-3.25.6.dist-info/METADATA +0 -356
- schemathesis-3.25.6.dist-info/RECORD +0 -134
- /schemathesis/{extra → cli/ext}/__init__.py +0 -0
- {schemathesis-3.25.6.dist-info → schemathesis-4.0.0a1.dist-info}/licenses/LICENSE +0 -0
@@ -1,199 +0,0 @@
|
|
1
|
-
from __future__ import annotations
|
2
|
-
from dataclasses import asdict
|
3
|
-
from typing import Any, Callable, Dict, Optional, TypeVar, cast
|
4
|
-
|
5
|
-
from ..models import Response
|
6
|
-
from ..runner import events
|
7
|
-
from ..runner.serialization import SerializedCase
|
8
|
-
from ..internal.transformation import merge_recursively
|
9
|
-
|
10
|
-
S = TypeVar("S", bound=events.ExecutionEvent)
|
11
|
-
SerializeFunc = Callable[[S], Optional[Dict[str, Any]]]
|
12
|
-
|
13
|
-
|
14
|
-
def serialize_initialized(event: events.Initialized) -> dict[str, Any] | None:
|
15
|
-
return {
|
16
|
-
"operations_count": event.operations_count,
|
17
|
-
"location": event.location or "",
|
18
|
-
"base_url": event.base_url,
|
19
|
-
}
|
20
|
-
|
21
|
-
|
22
|
-
def serialize_before_probing(_: events.BeforeProbing) -> None:
|
23
|
-
return None
|
24
|
-
|
25
|
-
|
26
|
-
def serialize_after_probing(event: events.AfterProbing) -> dict[str, Any] | None:
|
27
|
-
probes = event.probes or []
|
28
|
-
return {"probes": [probe.serialize() for probe in probes]}
|
29
|
-
|
30
|
-
|
31
|
-
def serialize_before_execution(event: events.BeforeExecution) -> dict[str, Any] | None:
|
32
|
-
return {
|
33
|
-
"correlation_id": event.correlation_id,
|
34
|
-
"verbose_name": event.verbose_name,
|
35
|
-
"data_generation_method": event.data_generation_method,
|
36
|
-
}
|
37
|
-
|
38
|
-
|
39
|
-
def _serialize_case(case: SerializedCase) -> dict[str, Any]:
|
40
|
-
return {
|
41
|
-
"verbose_name": case.verbose_name,
|
42
|
-
"path_template": case.path_template,
|
43
|
-
"path_parameters": stringify_path_parameters(case.path_parameters),
|
44
|
-
"query": prepare_query(case.query),
|
45
|
-
"cookies": case.cookies,
|
46
|
-
"media_type": case.media_type,
|
47
|
-
}
|
48
|
-
|
49
|
-
|
50
|
-
def _serialize_response(response: Response) -> dict[str, Any]:
|
51
|
-
return {
|
52
|
-
"status_code": response.status_code,
|
53
|
-
"headers": response.headers,
|
54
|
-
"body": response.body,
|
55
|
-
"encoding": response.encoding,
|
56
|
-
"elapsed": response.elapsed,
|
57
|
-
}
|
58
|
-
|
59
|
-
|
60
|
-
def serialize_after_execution(event: events.AfterExecution) -> dict[str, Any] | None:
|
61
|
-
return {
|
62
|
-
"correlation_id": event.correlation_id,
|
63
|
-
"verbose_name": event.verbose_name,
|
64
|
-
"status": event.status,
|
65
|
-
"elapsed_time": event.elapsed_time,
|
66
|
-
"data_generation_method": event.data_generation_method,
|
67
|
-
"result": {
|
68
|
-
"checks": [
|
69
|
-
{
|
70
|
-
"name": check.name,
|
71
|
-
"value": check.value,
|
72
|
-
"request": {
|
73
|
-
"method": check.request.method,
|
74
|
-
"uri": check.request.uri,
|
75
|
-
"body": check.request.body,
|
76
|
-
"headers": check.request.headers,
|
77
|
-
},
|
78
|
-
"response": _serialize_response(check.response) if check.response is not None else None,
|
79
|
-
"example": _serialize_case(check.example),
|
80
|
-
"message": check.message,
|
81
|
-
"context": asdict(check.context) if check.context is not None else None, # type: ignore
|
82
|
-
"history": [
|
83
|
-
{"case": _serialize_case(entry.case), "response": _serialize_response(entry.response)}
|
84
|
-
for entry in check.history
|
85
|
-
],
|
86
|
-
}
|
87
|
-
for check in event.result.checks
|
88
|
-
],
|
89
|
-
"errors": [asdict(error) for error in event.result.errors],
|
90
|
-
"skip_reason": event.result.skip_reason,
|
91
|
-
},
|
92
|
-
}
|
93
|
-
|
94
|
-
|
95
|
-
def serialize_interrupted(_: events.Interrupted) -> dict[str, Any] | None:
|
96
|
-
return None
|
97
|
-
|
98
|
-
|
99
|
-
def serialize_internal_error(event: events.InternalError) -> dict[str, Any] | None:
|
100
|
-
return {
|
101
|
-
"type": event.type.value,
|
102
|
-
"subtype": event.subtype.value if event.subtype else event.subtype,
|
103
|
-
"title": event.title,
|
104
|
-
"message": event.message,
|
105
|
-
"extras": event.extras,
|
106
|
-
"exception_type": event.exception_type,
|
107
|
-
"exception": event.exception,
|
108
|
-
"exception_with_traceback": event.exception_with_traceback,
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
def serialize_finished(event: events.Finished) -> dict[str, Any] | None:
|
113
|
-
return {
|
114
|
-
"generic_errors": [
|
115
|
-
{
|
116
|
-
"exception": error.exception,
|
117
|
-
"exception_with_traceback": error.exception_with_traceback,
|
118
|
-
"title": error.title,
|
119
|
-
}
|
120
|
-
for error in event.generic_errors
|
121
|
-
],
|
122
|
-
"running_time": event.running_time,
|
123
|
-
}
|
124
|
-
|
125
|
-
|
126
|
-
SERIALIZER_MAP = {
|
127
|
-
events.Initialized: serialize_initialized,
|
128
|
-
events.BeforeProbing: serialize_before_probing,
|
129
|
-
events.AfterProbing: serialize_after_probing,
|
130
|
-
events.BeforeExecution: serialize_before_execution,
|
131
|
-
events.AfterExecution: serialize_after_execution,
|
132
|
-
events.Interrupted: serialize_interrupted,
|
133
|
-
events.InternalError: serialize_internal_error,
|
134
|
-
events.Finished: serialize_finished,
|
135
|
-
}
|
136
|
-
|
137
|
-
|
138
|
-
def serialize_event(
|
139
|
-
event: events.ExecutionEvent,
|
140
|
-
*,
|
141
|
-
on_initialized: SerializeFunc | None = None,
|
142
|
-
on_before_probing: SerializeFunc | None = None,
|
143
|
-
on_after_probing: SerializeFunc | None = None,
|
144
|
-
on_before_execution: SerializeFunc | None = None,
|
145
|
-
on_after_execution: SerializeFunc | None = None,
|
146
|
-
on_interrupted: SerializeFunc | None = None,
|
147
|
-
on_internal_error: SerializeFunc | None = None,
|
148
|
-
on_finished: SerializeFunc | None = None,
|
149
|
-
extra: dict[str, Any] | None = None,
|
150
|
-
) -> dict[str, dict[str, Any] | None]:
|
151
|
-
"""Turn an event into JSON-serializable structure."""
|
152
|
-
# Use the explicitly provided serializer for this event and fallback to default one if it is not provided
|
153
|
-
serializer = {
|
154
|
-
events.Initialized: on_initialized,
|
155
|
-
events.BeforeProbing: on_before_probing,
|
156
|
-
events.AfterProbing: on_after_probing,
|
157
|
-
events.BeforeExecution: on_before_execution,
|
158
|
-
events.AfterExecution: on_after_execution,
|
159
|
-
events.Interrupted: on_interrupted,
|
160
|
-
events.InternalError: on_internal_error,
|
161
|
-
events.Finished: on_finished,
|
162
|
-
}.get(event.__class__)
|
163
|
-
if serializer is None:
|
164
|
-
serializer = cast(SerializeFunc, SERIALIZER_MAP[event.__class__])
|
165
|
-
data = serializer(event)
|
166
|
-
if extra is not None:
|
167
|
-
# If `extra` is present, then merge it with the serialized data. If serialized data is empty, then replace it
|
168
|
-
# with `extra` value
|
169
|
-
if data is None:
|
170
|
-
data = extra
|
171
|
-
else:
|
172
|
-
data = merge_recursively(data, extra)
|
173
|
-
# Externally tagged structure
|
174
|
-
return {event.__class__.__name__: data}
|
175
|
-
|
176
|
-
|
177
|
-
def stringify_path_parameters(path_parameters: dict[str, Any] | None) -> dict[str, str]:
|
178
|
-
"""Cast all path parameter values to strings.
|
179
|
-
|
180
|
-
Path parameter values may be of arbitrary type, but to display them properly they should be casted to strings.
|
181
|
-
"""
|
182
|
-
return {key: str(value) for key, value in (path_parameters or {}).items()}
|
183
|
-
|
184
|
-
|
185
|
-
def prepare_query(query: dict[str, Any] | None) -> dict[str, list[str]]:
|
186
|
-
"""Convert all query values to list of strings.
|
187
|
-
|
188
|
-
Query parameters may be generated in different shapes, including integers, strings, list of strings, etc.
|
189
|
-
It can also be an object, if the schema contains an object, but `style` and `explode` combo is not applicable.
|
190
|
-
"""
|
191
|
-
|
192
|
-
def to_list_of_strings(value: Any) -> list[str]:
|
193
|
-
if isinstance(value, list):
|
194
|
-
return list(map(str, value))
|
195
|
-
if isinstance(value, str):
|
196
|
-
return [value]
|
197
|
-
return [str(value)]
|
198
|
-
|
199
|
-
return {key: to_list_of_strings(value) for key, value in (query or {}).items()}
|
schemathesis/service/usage.py
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
from __future__ import annotations
|
2
|
-
import sys
|
3
|
-
from typing import Any
|
4
|
-
|
5
|
-
import click
|
6
|
-
from click.types import StringParamType
|
7
|
-
|
8
|
-
from .. import cli, hooks
|
9
|
-
|
10
|
-
|
11
|
-
def collect(args: list[str] | None = None) -> dict[str, Any] | None:
|
12
|
-
"""Collect anonymized CLI usage data."""
|
13
|
-
context: click.Context | None = click.get_current_context(silent=True)
|
14
|
-
if context is not None and not sys.argv[0].endswith("pytest"):
|
15
|
-
args = args or sys.argv[2:]
|
16
|
-
parameters, _, types = parse_cli_args(context, args)
|
17
|
-
parameters_data: dict[str, dict[str, Any]] = {}
|
18
|
-
used_headers: list[str] = []
|
19
|
-
schema = parameters["schema"]
|
20
|
-
app = parameters.get("app")
|
21
|
-
if not schema:
|
22
|
-
schema_kind = None
|
23
|
-
else:
|
24
|
-
schema_kind = cli.callbacks.parse_schema_kind(schema, app).name
|
25
|
-
usage = {
|
26
|
-
"schema_kind": schema_kind,
|
27
|
-
"parameters": parameters_data,
|
28
|
-
"used_headers": used_headers,
|
29
|
-
"hooks": hooks.collect_statistic(),
|
30
|
-
}
|
31
|
-
types_iter = iter(types)
|
32
|
-
for option, value in parameters.items():
|
33
|
-
option_type = next(types_iter)
|
34
|
-
if isinstance(option_type, click.Argument):
|
35
|
-
continue
|
36
|
-
if option_type.multiple:
|
37
|
-
# Forward the iterator to the next option type
|
38
|
-
for _ in range(len(value) - 1):
|
39
|
-
next(types_iter)
|
40
|
-
entry = _collect_option(option, option_type, used_headers, value)
|
41
|
-
if entry:
|
42
|
-
parameters_data[option] = entry
|
43
|
-
return usage
|
44
|
-
return None
|
45
|
-
|
46
|
-
|
47
|
-
def _collect_option(option: str, option_type: click.Parameter, used_headers: list[str], value: Any) -> dict[str, Any]:
|
48
|
-
entry = {}
|
49
|
-
if isinstance(option_type.type, (StringParamType, click.types.File)):
|
50
|
-
if option == "headers" and value:
|
51
|
-
used_headers.extend(header.split(":", 1)[0] for header in value)
|
52
|
-
else:
|
53
|
-
# Free-form values are replaced with their number of occurrences, to avoid sending sensitive info
|
54
|
-
if option_type.multiple:
|
55
|
-
entry["count"] = len(value)
|
56
|
-
else:
|
57
|
-
entry["count"] = 1
|
58
|
-
else:
|
59
|
-
entry["value"] = value
|
60
|
-
return entry
|
61
|
-
|
62
|
-
|
63
|
-
def parse_cli_args(context: click.Context, args: list[str]) -> tuple[dict[str, Any], list, list[click.Parameter]]:
|
64
|
-
parser = cli.run.make_parser(context)
|
65
|
-
return parser.parse_args(args=args)
|
@@ -1,344 +0,0 @@
|
|
1
|
-
from __future__ import annotations
|
2
|
-
|
3
|
-
import json
|
4
|
-
import pathlib
|
5
|
-
from functools import lru_cache
|
6
|
-
from json import JSONDecodeError
|
7
|
-
from typing import IO, TYPE_CHECKING, Any, Callable, Dict, NoReturn, cast
|
8
|
-
|
9
|
-
from ...code_samples import CodeSampleStyle
|
10
|
-
from ...constants import WAIT_FOR_SCHEMA_INTERVAL
|
11
|
-
from ...exceptions import SchemaError, SchemaErrorType
|
12
|
-
from ...generation import (
|
13
|
-
DEFAULT_DATA_GENERATION_METHODS,
|
14
|
-
DataGenerationMethod,
|
15
|
-
DataGenerationMethodInput,
|
16
|
-
GenerationConfig,
|
17
|
-
)
|
18
|
-
from ...hooks import HookContext, dispatch
|
19
|
-
from ...internal.validation import require_relative_url
|
20
|
-
from ...loaders import load_schema_from_url
|
21
|
-
from ...throttling import build_limiter
|
22
|
-
from ...transports.headers import setup_default_headers
|
23
|
-
from ...types import PathLike
|
24
|
-
|
25
|
-
if TYPE_CHECKING:
|
26
|
-
from graphql import DocumentNode
|
27
|
-
from pyrate_limiter import Limiter
|
28
|
-
|
29
|
-
from ...transports.responses import GenericResponse
|
30
|
-
from .schemas import GraphQLSchema
|
31
|
-
|
32
|
-
|
33
|
-
@lru_cache
|
34
|
-
def get_introspection_query() -> str:
|
35
|
-
import graphql
|
36
|
-
|
37
|
-
return graphql.get_introspection_query()
|
38
|
-
|
39
|
-
|
40
|
-
@lru_cache
|
41
|
-
def get_introspection_query_ast() -> DocumentNode:
|
42
|
-
import graphql
|
43
|
-
|
44
|
-
query = get_introspection_query()
|
45
|
-
return graphql.parse(query)
|
46
|
-
|
47
|
-
|
48
|
-
def from_path(
|
49
|
-
path: PathLike,
|
50
|
-
*,
|
51
|
-
app: Any = None,
|
52
|
-
base_url: str | None = None,
|
53
|
-
data_generation_methods: DataGenerationMethodInput = DEFAULT_DATA_GENERATION_METHODS,
|
54
|
-
generation_config: GenerationConfig | None = None,
|
55
|
-
code_sample_style: str = CodeSampleStyle.default().name,
|
56
|
-
rate_limit: str | None = None,
|
57
|
-
encoding: str = "utf8",
|
58
|
-
sanitize_output: bool = True,
|
59
|
-
) -> GraphQLSchema:
|
60
|
-
"""Load GraphQL schema via a file from an OS path.
|
61
|
-
|
62
|
-
:param path: A path to the schema file.
|
63
|
-
:param encoding: The name of the encoding used to decode the file.
|
64
|
-
"""
|
65
|
-
with open(path, encoding=encoding) as fd:
|
66
|
-
return from_file(
|
67
|
-
fd,
|
68
|
-
app=app,
|
69
|
-
base_url=base_url,
|
70
|
-
data_generation_methods=data_generation_methods,
|
71
|
-
code_sample_style=code_sample_style,
|
72
|
-
location=pathlib.Path(path).absolute().as_uri(),
|
73
|
-
rate_limit=rate_limit,
|
74
|
-
sanitize_output=sanitize_output,
|
75
|
-
)
|
76
|
-
|
77
|
-
|
78
|
-
def extract_schema_from_response(response: GenericResponse) -> dict[str, Any]:
|
79
|
-
from requests import Response
|
80
|
-
|
81
|
-
try:
|
82
|
-
if isinstance(response, Response):
|
83
|
-
decoded = response.json()
|
84
|
-
else:
|
85
|
-
decoded = response.json
|
86
|
-
except JSONDecodeError as exc:
|
87
|
-
raise SchemaError(
|
88
|
-
SchemaErrorType.UNEXPECTED_CONTENT_TYPE,
|
89
|
-
"Received unsupported content while expecting a JSON payload for GraphQL",
|
90
|
-
) from exc
|
91
|
-
return decoded
|
92
|
-
|
93
|
-
|
94
|
-
def from_url(
|
95
|
-
url: str,
|
96
|
-
*,
|
97
|
-
app: Any = None,
|
98
|
-
base_url: str | None = None,
|
99
|
-
port: int | None = None,
|
100
|
-
data_generation_methods: DataGenerationMethodInput = DEFAULT_DATA_GENERATION_METHODS,
|
101
|
-
generation_config: GenerationConfig | None = None,
|
102
|
-
code_sample_style: str = CodeSampleStyle.default().name,
|
103
|
-
wait_for_schema: float | None = None,
|
104
|
-
rate_limit: str | None = None,
|
105
|
-
sanitize_output: bool = True,
|
106
|
-
**kwargs: Any,
|
107
|
-
) -> GraphQLSchema:
|
108
|
-
"""Load GraphQL schema from the network.
|
109
|
-
|
110
|
-
:param url: Schema URL.
|
111
|
-
:param Optional[str] base_url: Base URL to send requests to.
|
112
|
-
:param Optional[int] port: An optional port if you don't want to pass the ``base_url`` parameter, but only to change
|
113
|
-
port in ``url``.
|
114
|
-
:param app: A WSGI app instance.
|
115
|
-
:return: GraphQLSchema
|
116
|
-
"""
|
117
|
-
import backoff
|
118
|
-
import requests
|
119
|
-
|
120
|
-
setup_default_headers(kwargs)
|
121
|
-
kwargs.setdefault("json", {"query": get_introspection_query()})
|
122
|
-
if port:
|
123
|
-
from yarl import URL
|
124
|
-
|
125
|
-
url = str(URL(url).with_port(port))
|
126
|
-
if not base_url:
|
127
|
-
base_url = url
|
128
|
-
|
129
|
-
if wait_for_schema is not None:
|
130
|
-
|
131
|
-
@backoff.on_exception( # type: ignore
|
132
|
-
backoff.constant,
|
133
|
-
requests.exceptions.ConnectionError,
|
134
|
-
max_time=wait_for_schema,
|
135
|
-
interval=WAIT_FOR_SCHEMA_INTERVAL,
|
136
|
-
)
|
137
|
-
def _load_schema(_uri: str, **_kwargs: Any) -> requests.Response:
|
138
|
-
return requests.post(_uri, **kwargs)
|
139
|
-
|
140
|
-
else:
|
141
|
-
_load_schema = requests.post
|
142
|
-
|
143
|
-
response = load_schema_from_url(lambda: _load_schema(url, **kwargs))
|
144
|
-
raw_schema = extract_schema_from_response(response)
|
145
|
-
return from_dict(
|
146
|
-
raw_schema=raw_schema,
|
147
|
-
location=url,
|
148
|
-
base_url=base_url,
|
149
|
-
app=app,
|
150
|
-
data_generation_methods=data_generation_methods,
|
151
|
-
code_sample_style=code_sample_style,
|
152
|
-
rate_limit=rate_limit,
|
153
|
-
sanitize_output=sanitize_output,
|
154
|
-
)
|
155
|
-
|
156
|
-
|
157
|
-
def from_file(
|
158
|
-
file: IO[str] | str,
|
159
|
-
*,
|
160
|
-
app: Any = None,
|
161
|
-
base_url: str | None = None,
|
162
|
-
data_generation_methods: DataGenerationMethodInput = DEFAULT_DATA_GENERATION_METHODS,
|
163
|
-
generation_config: GenerationConfig | None = None,
|
164
|
-
code_sample_style: str = CodeSampleStyle.default().name,
|
165
|
-
location: str | None = None,
|
166
|
-
rate_limit: str | None = None,
|
167
|
-
sanitize_output: bool = True,
|
168
|
-
) -> GraphQLSchema:
|
169
|
-
"""Load GraphQL schema from a file descriptor or a string.
|
170
|
-
|
171
|
-
:param file: Could be a file descriptor, string or bytes.
|
172
|
-
"""
|
173
|
-
import graphql
|
174
|
-
|
175
|
-
if isinstance(file, str):
|
176
|
-
data = file
|
177
|
-
else:
|
178
|
-
data = file.read()
|
179
|
-
try:
|
180
|
-
document = graphql.build_schema(data)
|
181
|
-
result = graphql.execute(document, get_introspection_query_ast())
|
182
|
-
# TYPES: We don't pass `is_awaitable` above, therefore `result` is of the `ExecutionResult` type
|
183
|
-
result = cast(graphql.ExecutionResult, result)
|
184
|
-
# TYPES:
|
185
|
-
# - `document` is a valid schema, because otherwise `build_schema` will rise an error;
|
186
|
-
# - `INTROSPECTION_QUERY` is a valid query - it is known upfront;
|
187
|
-
# Therefore the execution result is always valid at this point and `result.data` is not `None`
|
188
|
-
raw_schema = cast(Dict[str, Any], result.data)
|
189
|
-
except Exception as exc:
|
190
|
-
try:
|
191
|
-
raw_schema = json.loads(data)
|
192
|
-
if not isinstance(raw_schema, dict) or "__schema" not in raw_schema:
|
193
|
-
_on_invalid_schema(exc)
|
194
|
-
except json.JSONDecodeError:
|
195
|
-
_on_invalid_schema(exc, extras=[entry for entry in str(exc).splitlines() if entry])
|
196
|
-
return from_dict(
|
197
|
-
raw_schema,
|
198
|
-
app=app,
|
199
|
-
base_url=base_url,
|
200
|
-
data_generation_methods=data_generation_methods,
|
201
|
-
code_sample_style=code_sample_style,
|
202
|
-
location=location,
|
203
|
-
rate_limit=rate_limit,
|
204
|
-
sanitize_output=sanitize_output,
|
205
|
-
)
|
206
|
-
|
207
|
-
|
208
|
-
def _on_invalid_schema(exc: Exception, extras: list[str] | None = None) -> NoReturn:
|
209
|
-
raise SchemaError(
|
210
|
-
SchemaErrorType.GRAPHQL_INVALID_SCHEMA,
|
211
|
-
"The provided API schema does not appear to be a valid GraphQL schema",
|
212
|
-
extras=extras or [],
|
213
|
-
) from exc
|
214
|
-
|
215
|
-
|
216
|
-
def from_dict(
|
217
|
-
raw_schema: dict[str, Any],
|
218
|
-
*,
|
219
|
-
app: Any = None,
|
220
|
-
base_url: str | None = None,
|
221
|
-
location: str | None = None,
|
222
|
-
data_generation_methods: DataGenerationMethodInput = DEFAULT_DATA_GENERATION_METHODS,
|
223
|
-
generation_config: GenerationConfig | None = None,
|
224
|
-
code_sample_style: str = CodeSampleStyle.default().name,
|
225
|
-
rate_limit: str | None = None,
|
226
|
-
sanitize_output: bool = True,
|
227
|
-
) -> GraphQLSchema:
|
228
|
-
"""Load GraphQL schema from a Python dictionary.
|
229
|
-
|
230
|
-
:param dict raw_schema: A schema to load.
|
231
|
-
:param Optional[str] location: Optional schema location. Either a full URL or a filesystem path.
|
232
|
-
:param Optional[str] base_url: Base URL to send requests to.
|
233
|
-
:param app: A WSGI app instance.
|
234
|
-
:return: GraphQLSchema
|
235
|
-
"""
|
236
|
-
from .schemas import GraphQLSchema
|
237
|
-
|
238
|
-
_code_sample_style = CodeSampleStyle.from_str(code_sample_style)
|
239
|
-
hook_context = HookContext()
|
240
|
-
if "data" in raw_schema:
|
241
|
-
raw_schema = raw_schema["data"]
|
242
|
-
dispatch("before_load_schema", hook_context, raw_schema)
|
243
|
-
rate_limiter: Limiter | None = None
|
244
|
-
if rate_limit is not None:
|
245
|
-
rate_limiter = build_limiter(rate_limit)
|
246
|
-
instance = GraphQLSchema(
|
247
|
-
raw_schema,
|
248
|
-
location=location,
|
249
|
-
base_url=base_url,
|
250
|
-
app=app,
|
251
|
-
data_generation_methods=DataGenerationMethod.ensure_list(data_generation_methods),
|
252
|
-
code_sample_style=_code_sample_style,
|
253
|
-
rate_limiter=rate_limiter,
|
254
|
-
sanitize_output=sanitize_output,
|
255
|
-
) # type: ignore
|
256
|
-
dispatch("after_load_schema", hook_context, instance)
|
257
|
-
return instance
|
258
|
-
|
259
|
-
|
260
|
-
def from_wsgi(
|
261
|
-
schema_path: str,
|
262
|
-
app: Any,
|
263
|
-
*,
|
264
|
-
base_url: str | None = None,
|
265
|
-
data_generation_methods: DataGenerationMethodInput = DEFAULT_DATA_GENERATION_METHODS,
|
266
|
-
generation_config: GenerationConfig | None = None,
|
267
|
-
code_sample_style: str = CodeSampleStyle.default().name,
|
268
|
-
rate_limit: str | None = None,
|
269
|
-
sanitize_output: bool = True,
|
270
|
-
**kwargs: Any,
|
271
|
-
) -> GraphQLSchema:
|
272
|
-
"""Load GraphQL schema from a WSGI app.
|
273
|
-
|
274
|
-
:param str schema_path: An in-app relative URL to the schema.
|
275
|
-
:param app: A WSGI app instance.
|
276
|
-
:param Optional[str] base_url: Base URL to send requests to.
|
277
|
-
:return: GraphQLSchema
|
278
|
-
"""
|
279
|
-
from werkzeug import Client
|
280
|
-
|
281
|
-
from ...transports.responses import WSGIResponse
|
282
|
-
|
283
|
-
require_relative_url(schema_path)
|
284
|
-
setup_default_headers(kwargs)
|
285
|
-
kwargs.setdefault("json", {"query": get_introspection_query()})
|
286
|
-
client = Client(app, WSGIResponse)
|
287
|
-
response = load_schema_from_url(lambda: client.post(schema_path, **kwargs))
|
288
|
-
raw_schema = extract_schema_from_response(response)
|
289
|
-
return from_dict(
|
290
|
-
raw_schema=raw_schema,
|
291
|
-
location=schema_path,
|
292
|
-
base_url=base_url,
|
293
|
-
app=app,
|
294
|
-
data_generation_methods=data_generation_methods,
|
295
|
-
code_sample_style=code_sample_style,
|
296
|
-
rate_limit=rate_limit,
|
297
|
-
sanitize_output=sanitize_output,
|
298
|
-
)
|
299
|
-
|
300
|
-
|
301
|
-
def from_asgi(
|
302
|
-
schema_path: str,
|
303
|
-
app: Any,
|
304
|
-
*,
|
305
|
-
base_url: str | None = None,
|
306
|
-
data_generation_methods: DataGenerationMethodInput = DEFAULT_DATA_GENERATION_METHODS,
|
307
|
-
generation_config: GenerationConfig | None = None,
|
308
|
-
code_sample_style: str = CodeSampleStyle.default().name,
|
309
|
-
rate_limit: str | None = None,
|
310
|
-
sanitize_output: bool = True,
|
311
|
-
**kwargs: Any,
|
312
|
-
) -> GraphQLSchema:
|
313
|
-
"""Load GraphQL schema from an ASGI app.
|
314
|
-
|
315
|
-
:param str schema_path: An in-app relative URL to the schema.
|
316
|
-
:param app: An ASGI app instance.
|
317
|
-
:param Optional[str] base_url: Base URL to send requests to.
|
318
|
-
"""
|
319
|
-
from starlette_testclient import TestClient as ASGIClient
|
320
|
-
|
321
|
-
require_relative_url(schema_path)
|
322
|
-
setup_default_headers(kwargs)
|
323
|
-
kwargs.setdefault("json", {"query": get_introspection_query()})
|
324
|
-
client = ASGIClient(app)
|
325
|
-
response = load_schema_from_url(lambda: client.post(schema_path, **kwargs))
|
326
|
-
raw_schema = extract_schema_from_response(response)
|
327
|
-
return from_dict(
|
328
|
-
raw_schema=raw_schema,
|
329
|
-
location=schema_path,
|
330
|
-
base_url=base_url,
|
331
|
-
app=app,
|
332
|
-
data_generation_methods=data_generation_methods,
|
333
|
-
code_sample_style=code_sample_style,
|
334
|
-
rate_limit=rate_limit,
|
335
|
-
sanitize_output=sanitize_output,
|
336
|
-
)
|
337
|
-
|
338
|
-
|
339
|
-
def get_loader_for_app(app: Any) -> Callable:
|
340
|
-
from starlette.applications import Starlette
|
341
|
-
|
342
|
-
if isinstance(app, Starlette):
|
343
|
-
return from_asgi
|
344
|
-
return from_wsgi
|
@@ -1,49 +0,0 @@
|
|
1
|
-
from __future__ import annotations
|
2
|
-
import re
|
3
|
-
|
4
|
-
from ...types import Filter
|
5
|
-
|
6
|
-
|
7
|
-
def should_skip_method(method: str, pattern: Filter | None) -> bool:
|
8
|
-
if pattern is None:
|
9
|
-
return False
|
10
|
-
patterns = _ensure_tuple(pattern)
|
11
|
-
return method.upper() not in map(str.upper, patterns)
|
12
|
-
|
13
|
-
|
14
|
-
def should_skip_endpoint(endpoint: str, pattern: Filter | None) -> bool:
|
15
|
-
if pattern is None:
|
16
|
-
return False
|
17
|
-
return not _match_any_pattern(endpoint, pattern)
|
18
|
-
|
19
|
-
|
20
|
-
def should_skip_by_tag(tags: list[str] | None, pattern: Filter | None) -> bool:
|
21
|
-
if pattern is None:
|
22
|
-
return False
|
23
|
-
if not tags:
|
24
|
-
return True
|
25
|
-
patterns = _ensure_tuple(pattern)
|
26
|
-
return not any(re.search(item, tag) for item in patterns for tag in tags)
|
27
|
-
|
28
|
-
|
29
|
-
def should_skip_by_operation_id(operation_id: str | None, pattern: Filter | None) -> bool:
|
30
|
-
if pattern is None:
|
31
|
-
return False
|
32
|
-
if not operation_id:
|
33
|
-
return True
|
34
|
-
return not _match_any_pattern(operation_id, pattern)
|
35
|
-
|
36
|
-
|
37
|
-
def should_skip_deprecated(is_deprecated: bool, skip_deprecated_operations: bool) -> bool:
|
38
|
-
return skip_deprecated_operations and is_deprecated
|
39
|
-
|
40
|
-
|
41
|
-
def _match_any_pattern(target: str, pattern: Filter) -> bool:
|
42
|
-
patterns = _ensure_tuple(pattern)
|
43
|
-
return any(re.search(item, target) for item in patterns)
|
44
|
-
|
45
|
-
|
46
|
-
def _ensure_tuple(item: Filter) -> list | set | tuple:
|
47
|
-
if not isinstance(item, (list, set, tuple)):
|
48
|
-
return (item,)
|
49
|
-
return item
|