-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
executable file
·136 lines (108 loc) · 3.4 KB
/
example.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
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
#!/usr/bin/env python3
"""Used to validate that thing work as expected"""
from marshmallow import Schema, fields, post_load
from bors.app.strategy import IStrategy
from bors.app.config import AppConf
from bors.app.builder import AppBuilder
from bors.app.strategy import Strategy
from bors.algorithms.echo import echo
from bors.common.dotobj import DotObj
class MockItemSchema(Schema):
"""Mock item"""
test = fields.Str(required=True)
call = fields.Str(required=True)
class Print(IStrategy):
"""Print strategy implementation"""
def bind(self, context):
"""
Bind the strategy to the middleware pipeline returning the context
"""
echo(f"""PrintStrategy: {context['result'].data['callname']}""")
# just a pass-through
return context
class Request:
"""A bare request object"""
def __init__(self, **kwargs):
self.callname = kwargs.get('callname', None)
self.payload = kwargs.get('payload', None)
class Result(DotObj):
"""A bare result object"""
def __init__(self, **kwargs):
self.callname = kwargs.get('callname', None)
self.channel = kwargs.get('channel', None)
self.response_type = kwargs.get('response_type', None)
self.result = kwargs.get('result', None)
self.errors = kwargs.get('errors', None)
super().__init__(**kwargs)
class RequestSchema(Schema):
"""Schema defining the data structure the API can be called with"""
callname = fields.Str(required=True)
payload = fields.Dict()
@post_load
def make_request(self, data):
"""Parse the outgoing schema"""
sch = MockItemSchema()
return Request(**{
"callname": self.context.get("callname"),
"payload": sch.dump(data),
})
class Meta:
"""Strict"""
strict = True
class ResponseSchema(Schema):
"""Schema defining the data structure the API can be called with"""
@post_load
def populate_data(self, data):
"""Parse the outgoing schema"""
sch = MockItemSchema()
return Result(**{
"callname": self.context.get("callname"),
"result": sch.dump(data),
})
class Meta:
"""Strict"""
strict = True
class MyAPI:
"""
Mock API (doesn't do anything, but drop a message on the pipeline upon
request.
"""
name = "my_api"
def __init__(self, context):
self.request_schema = RequestSchema
self.result_schema = ResponseSchema
self.context = context
def call(self, callname, data=None, **args):
"""Mock call interface"""
return {"test": "Success", "call": callname}
def main():
"""MAIN"""
config = {
"api": {
"services": [
{
"name": "my_api",
"testkey": "testval",
},
],
"calls": {
"hello_world": {
"delay": 5,
"priority": 1,
"arguments": None,
},
"marco": {
"delay": 1,
"priority": 1,
},
"pollo": {
"delay": 1,
"priority": 1,
},
}
}
}
app = AppBuilder([MyAPI], Strategy(Print()), AppConf(config))
app.run()
if __name__ == "__main__":
main()