-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewsletter.py
53 lines (42 loc) · 1.48 KB
/
newsletter.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
import os, json
import requests
from flask_cors import CORS, cross_origin
from apiflask import APIFlask, abort
import yaml
app = APIFlask('newsletter', title='Newsletter Service')
CORS(app)
cpmock_server = os.getenv("CAPTUREMOCK_SERVER")
if cpmock_server:
interceptor = "(req) => { req.url = req.url.replace(/http:..127.0.0.1:[0-9]+/, '" + cpmock_server + "'); return req; }"
app.config['SWAGGER_UI_CONFIG'] = {
'requestInterceptor': interceptor
}
@app.get("/sayHello/<string:name>")
@cross_origin()
def say_hello(name):
person = get_person(name)
resp = format_greeting(person)
return resp
def get_person(name):
users_url = os.getenv("USERS_URL", 'http://localhost:5001')
url = f'{users_url}/getPerson/{name}'
res = _get(url)
person = json.loads(res)
return person
def format_greeting(person):
greeting_url = os.getenv("GREETING_URL", 'http://localhost:5002')
url = greeting_url + '/formatGreeting'
return _get(url, params=person)
def _get(url, params=None):
r = requests.get(url, params=params)
if r.status_code != 200:
data = json.loads(r.text)
abort(r.status_code, data['message'])
return r.text
if __name__ == "__main__":
port = 0 if "DYNAMIC_PORTS" in os.environ else 5010
if "DUMP_SCHEMA" in os.environ:
print("Writing schema file")
with open(os.path.join(os.path.dirname(__file__), "newsletter-openapi.yaml"), "w") as f:
yaml.dump(app.spec, f)
app.run(port=port)