-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
52 lines (39 loc) · 1.34 KB
/
client.py
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
import apistar
import typesystem
from apistar.schemas.openapi import lookup
schema = open("petstore.yaml").read()
class MockTransport:
schemes = ["http", "https"]
def get_response_examples_from_endpoints(self, endpoint):
return lookup(
self.endpoint_definitions,
[
f"/{endpoint}",
"get",
"responses",
"200",
"content",
"application/json",
"examples",
"default",
"value",
],
)
def __init__(self, schema):
token = typesystem.tokenize_yaml(schema)
self.endpoint_definitions = token.value["paths"]
def send(self, method, url, query_params, content, encoding):
endpoint = url.split("/")[-1]
return self.get_response_examples_from_endpoints(endpoint)
class MockClient(apistar.Client):
"""
A mock rest client which a mock transport which will return examples
from the OpenAPI schema instead of real responses
"""
def __init__(self, schema, *args, **kwargs):
super().__init__(schema, *args, **kwargs)
self.transport = MockTransport(schema)
@property
def endpoints(self):
return tuple(x.name for x in self.document.walk_links())
mock_client = MockClient(schema=schema)